CTRE Phoenix 6 C++ 24.3.0
VelocityTorqueCurrentFOC.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
12#include <sstream>
13#include <units/angular_velocity.h>
14#include <units/angular_acceleration.h>
15#include <units/current.h>
16#include <units/frequency.h>
17#include <units/time.h>
18
19
20namespace ctre {
21namespace phoenix6 {
22namespace controls {
23
24/**
25 * Requires Phoenix Pro;
26 * Request PID to target velocity with torque current feedforward.
27 *
28 * This control mode will set the motor's velocity setpoint to the velocity specified by the user. In
29 * addition, it will apply an additional torque current as an arbitrary feedforward value.
30 */
32{
33 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
34 {
35 if (req.get() != this)
36 {
37 auto const reqCast = dynamic_cast<VelocityTorqueCurrentFOC *>(req.get());
38 if (reqCast != nullptr)
39 {
40 *reqCast = *this;
41 }
42 else
43 {
44 req = std::make_shared<VelocityTorqueCurrentFOC>(*this);
45 }
46 }
47
48 return c_ctre_phoenix6_RequestControlVelocityTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Velocity.to<double>(), Acceleration.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral, LimitForwardMotion, LimitReverseMotion);
49 }
50
51public:
52 /**
53 * Velocity to drive toward in rotations per second.
54 */
55 units::angular_velocity::turns_per_second_t Velocity;
56 /**
57 * Acceleration to drive toward in rotations per second squared. This is
58 * typically used for motion profiles generated by the robot program.
59 */
60 units::angular_acceleration::turns_per_second_squared_t Acceleration;
61 /**
62 * Feedforward to apply in torque current in Amperes. User can use motor's kT
63 * to scale Newton-meter to Amperes.
64 */
65 units::current::ampere_t FeedForward;
66 /**
67 * Select which gains are applied by selecting the slot. Use the configuration
68 * api to set the gain values for the selected slot before enabling this
69 * feature. Slot must be within [0,2].
70 */
71 int Slot;
72 /**
73 * Set to true to coast the rotor when output is zero (or within deadband). Set
74 * to false to use the NeutralMode configuration setting (default). This flag
75 * exists to provide the fundamental behavior of this control when output is
76 * zero, which is to provide 0A (zero torque).
77 */
79 /**
80 * Set to true to force forward limiting. This allows users to use other limit
81 * switch sensors connected to robot controller. This also allows use of active
82 * sensors that require external power.
83 */
85 /**
86 * Set to true to force reverse limiting. This allows users to use other limit
87 * switch sensors connected to robot controller. This also allows use of active
88 * sensors that require external power.
89 */
91
92 /**
93 * \brief The period at which this control will update at.
94 * This is designated in Hertz, with a minimum of 20 Hz
95 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
96 *
97 * If this field is set to 0 Hz, the control request will
98 * be sent immediately as a one-shot frame. This may be useful
99 * for advanced applications that require outputs to be
100 * synchronized with data acquisition. In this case, we
101 * recommend not exceeding 50 ms between control calls.
102 */
103 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
104
105 /**
106 * \brief Requires Phoenix Pro;
107 * Request PID to target velocity with torque current feedforward.
108 *
109 * \details This control mode will set the motor's velocity setpoint to the
110 * velocity specified by the user. In addition, it will apply an
111 * additional torque current as an arbitrary feedforward value.
112 *
113 * \param Velocity Velocity to drive toward in rotations per second.
114 * \param Acceleration Acceleration to drive toward in rotations per second
115 * squared. This is typically used for motion profiles
116 * generated by the robot program.
117 * \param FeedForward Feedforward to apply in torque current in Amperes.
118 * User can use motor's kT to scale Newton-meter to
119 * Amperes.
120 * \param Slot Select which gains are applied by selecting the slot. Use the
121 * configuration api to set the gain values for the selected slot
122 * before enabling this feature. Slot must be within [0,2].
123 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
124 * is zero (or within deadband). Set to false
125 * to use the NeutralMode configuration
126 * setting (default). This flag exists to
127 * provide the fundamental behavior of this
128 * control when output is zero, which is to
129 * provide 0A (zero torque).
130 * \param LimitForwardMotion Set to true to force forward limiting. This
131 * allows users to use other limit switch sensors
132 * connected to robot controller. This also allows
133 * use of active sensors that require external
134 * power.
135 * \param LimitReverseMotion Set to true to force reverse limiting. This
136 * allows users to use other limit switch sensors
137 * connected to robot controller. This also allows
138 * use of active sensors that require external
139 * power.
140 */
141 VelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq, units::current::ampere_t FeedForward = 0.0_A, int Slot = 0, bool OverrideCoastDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"VelocityTorqueCurrentFOC"},
142 Velocity{std::move(Velocity)},
143 Acceleration{std::move(Acceleration)},
144 FeedForward{std::move(FeedForward)},
145 Slot{std::move(Slot)},
149 {}
150
151 /**
152 * \brief Modifies this Control Request's Velocity parameter and returns itself for
153 * method-chaining and easier to use request API.
154 *
155 * Velocity to drive toward in rotations per second.
156 *
157 * \param newVelocity Parameter to modify
158 * \returns Itself
159 */
160 VelocityTorqueCurrentFOC& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
161 {
162 Velocity = std::move(newVelocity);
163 return *this;
164 }
165
166 /**
167 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
168 * method-chaining and easier to use request API.
169 *
170 * Acceleration to drive toward in rotations per second squared. This is
171 * typically used for motion profiles generated by the robot program.
172 *
173 * \param newAcceleration Parameter to modify
174 * \returns Itself
175 */
176 VelocityTorqueCurrentFOC& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
177 {
178 Acceleration = std::move(newAcceleration);
179 return *this;
180 }
181
182 /**
183 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
184 * method-chaining and easier to use request API.
185 *
186 * Feedforward to apply in torque current in Amperes. User can use motor's kT
187 * to scale Newton-meter to Amperes.
188 *
189 * \param newFeedForward Parameter to modify
190 * \returns Itself
191 */
192 VelocityTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
193 {
194 FeedForward = std::move(newFeedForward);
195 return *this;
196 }
197
198 /**
199 * \brief Modifies this Control Request's Slot parameter and returns itself for
200 * method-chaining and easier to use request API.
201 *
202 * Select which gains are applied by selecting the slot. Use the configuration
203 * api to set the gain values for the selected slot before enabling this
204 * feature. Slot must be within [0,2].
205 *
206 * \param newSlot Parameter to modify
207 * \returns Itself
208 */
210 {
211 Slot = std::move(newSlot);
212 return *this;
213 }
214
215 /**
216 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
217 * method-chaining and easier to use request API.
218 *
219 * Set to true to coast the rotor when output is zero (or within deadband). Set
220 * to false to use the NeutralMode configuration setting (default). This flag
221 * exists to provide the fundamental behavior of this control when output is
222 * zero, which is to provide 0A (zero torque).
223 *
224 * \param newOverrideCoastDurNeutral Parameter to modify
225 * \returns Itself
226 */
228 {
229 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
230 return *this;
231 }
232
233 /**
234 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
235 * method-chaining and easier to use request API.
236 *
237 * Set to true to force forward limiting. This allows users to use other limit
238 * switch sensors connected to robot controller. This also allows use of active
239 * sensors that require external power.
240 *
241 * \param newLimitForwardMotion Parameter to modify
242 * \returns Itself
243 */
245 {
246 LimitForwardMotion = std::move(newLimitForwardMotion);
247 return *this;
248 }
249
250 /**
251 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
252 * method-chaining and easier to use request API.
253 *
254 * Set to true to force reverse limiting. This allows users to use other limit
255 * switch sensors connected to robot controller. This also allows use of active
256 * sensors that require external power.
257 *
258 * \param newLimitReverseMotion Parameter to modify
259 * \returns Itself
260 */
262 {
263 LimitReverseMotion = std::move(newLimitReverseMotion);
264 return *this;
265 }
266 /**
267 * \brief Sets the period at which this control will update at.
268 * This is designated in Hertz, with a minimum of 20 Hz
269 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
270 *
271 * If this field is set to 0 Hz, the control request will
272 * be sent immediately as a one-shot frame. This may be useful
273 * for advanced applications that require outputs to be
274 * synchronized with data acquisition. In this case, we
275 * recommend not exceeding 50 ms between control calls.
276 *
277 * \param newUpdateFreqHz Parameter to modify
278 * \returns Itself
279 */
280 VelocityTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
281 {
282 UpdateFreqHz = newUpdateFreqHz;
283 return *this;
284 }
285 /**
286 * Returns a string representation of the object.
287 *
288 * \returns a string representation of the object.
289 */
290 std::string ToString() const override
291 {
292 std::stringstream ss;
293 ss << "class: VelocityTorqueCurrentFOC" << std::endl;
294 ss << "Velocity: " << Velocity.to<double>() << std::endl;
295 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
296 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
297 ss << "Slot: " << Slot << std::endl;
298 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
299 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
300 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
301 return ss.str();
302 }
303
304 /**
305 * \brief Gets information about this control request.
306 *
307 * \returns Map of control parameter names and corresponding applied values
308 */
309 std::map<std::string, std::string> GetControlInfo() const override
310 {
311 std::map<std::string, std::string> controlInfo;
312 std::stringstream ss;
313 controlInfo["Name"] = GetName();
314 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
315 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
316 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
317 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
318 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
319 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
320 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
321 return controlInfo;
322 }
323};
324
325}
326}
327}
328
CTREXPORT int c_ctre_phoenix6_RequestControlVelocityTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Velocity, double Acceleration, double FeedForward, int Slot, bool OverrideCoastDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requires Phoenix Pro; Request PID to target velocity with torque current feedforward.
Definition: VelocityTorqueCurrentFOC.hpp:32
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration to drive toward in rotations per second squared.
Definition: VelocityTorqueCurrentFOC.hpp:60
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VelocityTorqueCurrentFOC.hpp:309
VelocityTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: VelocityTorqueCurrentFOC.hpp:227
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: VelocityTorqueCurrentFOC.hpp:78
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: VelocityTorqueCurrentFOC.hpp:55
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: VelocityTorqueCurrentFOC.hpp:65
VelocityTorqueCurrentFOC & WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
Modifies this Control Request's Velocity parameter and returns itself for method-chaining and easier ...
Definition: VelocityTorqueCurrentFOC.hpp:160
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VelocityTorqueCurrentFOC.hpp:103
VelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration=0.0_tr_per_s_sq, units::current::ampere_t FeedForward=0.0_A, int Slot=0, bool OverrideCoastDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requires Phoenix Pro; Request PID to target velocity with torque current feedforward.
Definition: VelocityTorqueCurrentFOC.hpp:141
VelocityTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: VelocityTorqueCurrentFOC.hpp:192
VelocityTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: VelocityTorqueCurrentFOC.hpp:209
VelocityTorqueCurrentFOC & WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
Modifies this Control Request's Acceleration parameter and returns itself for method-chaining and eas...
Definition: VelocityTorqueCurrentFOC.hpp:176
int Slot
Select which gains are applied by selecting the slot.
Definition: VelocityTorqueCurrentFOC.hpp:71
VelocityTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: VelocityTorqueCurrentFOC.hpp:244
std::string ToString() const override
Returns a string representation of the object.
Definition: VelocityTorqueCurrentFOC.hpp:290
VelocityTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: VelocityTorqueCurrentFOC.hpp:261
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: VelocityTorqueCurrentFOC.hpp:90
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: VelocityTorqueCurrentFOC.hpp:84
VelocityTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VelocityTorqueCurrentFOC.hpp:280
Definition: string_util.hpp:15