CTRE Phoenix 6 C++ 24.3.0
MotionMagicVelocityDutyCycle.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/dimensionless.h>
16#include <units/frequency.h>
17#include <units/time.h>
18
19
20namespace ctre {
21namespace phoenix6 {
22namespace controls {
23
24/**
25 * Requests Motion Magic® to target a final velocity using a motion profile.
26 * This allows smooth transitions between velocity set points. Users can
27 * optionally provide a duty cycle feedforward.
28 *
29 * Motion Magic® Velocity produces a motion profile in real-time while attempting to honor the specified
30 * Acceleration and Jerk value. This control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
31 * configs. If the specified acceleration is zero, the Acceleration under Motion Magic® configuration
32 * parameter is used instead. This allows for runtime adjustment of acceleration for advanced users. Jerk is
33 * also specified in the Motion Magic® persistent configuration values. If Jerk is set to zero, Motion Magic®
34 * will produce a trapezoidal acceleration profile. Target velocity can also be changed on-the-fly and Motion
35 * Magic® will do its best to adjust the profile. This control mode is duty cycle based, so relevant
36 * closed-loop gains will use fractional duty cycle for the numerator: +1.0 represents full forward output.
37 */
39{
40 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
41 {
42 if (req.get() != this)
43 {
44 auto const reqCast = dynamic_cast<MotionMagicVelocityDutyCycle *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<MotionMagicVelocityDutyCycle>(*this);
52 }
53 }
54
55 return c_ctre_phoenix6_RequestControlMotionMagicVelocityDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Velocity.to<double>(), Acceleration.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
56 }
57
58public:
59 /**
60 * Target velocity to drive toward in rotations per second. This can be changed
61 * on-the fly.
62 */
63 units::angular_velocity::turns_per_second_t Velocity;
64 /**
65 * This is the absolute Acceleration to use generating the profile. If this
66 * parameter is zero, the Acceleration persistent configuration parameter is
67 * used instead. Acceleration is in rotations per second squared. If nonzero,
68 * the signage does not matter as the absolute value is used.
69 */
70 units::angular_acceleration::turns_per_second_squared_t Acceleration;
71 /**
72 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
73 * peak power by ~15%. Set to false to use trapezoidal commutation.
74 *
75 * FOC improves motor performance by leveraging torque (current) control.
76 * However, this may be inconvenient for applications that require specifying
77 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
78 * combines the performances gains of FOC while still allowing applications to
79 * provide duty cycle or voltage demand. This not to be confused with simple
80 * sinusoidal control or phase voltage control which lacks the performance
81 * gains.
82 */
84 /**
85 * Feedforward to apply in fractional units between -1 and +1.
86 */
87 units::dimensionless::scalar_t FeedForward;
88 /**
89 * Select which gains are applied by selecting the slot. Use the configuration
90 * api to set the gain values for the selected slot before enabling this
91 * feature. Slot must be within [0,2].
92 */
93 int Slot;
94 /**
95 * Set to true to static-brake the rotor when output is zero (or within
96 * deadband). Set to false to use the NeutralMode configuration setting
97 * (default). This flag exists to provide the fundamental behavior of this
98 * control when output is zero, which is to provide 0V to the motor.
99 */
101 /**
102 * Set to true to force forward limiting. This allows users to use other limit
103 * switch sensors connected to robot controller. This also allows use of active
104 * sensors that require external power.
105 */
107 /**
108 * Set to true to force reverse limiting. This allows users to use other limit
109 * switch sensors connected to robot controller. This also allows use of active
110 * sensors that require external power.
111 */
113
114 /**
115 * \brief The period at which this control will update at.
116 * This is designated in Hertz, with a minimum of 20 Hz
117 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
118 *
119 * If this field is set to 0 Hz, the control request will
120 * be sent immediately as a one-shot frame. This may be useful
121 * for advanced applications that require outputs to be
122 * synchronized with data acquisition. In this case, we
123 * recommend not exceeding 50 ms between control calls.
124 */
125 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
126
127 /**
128 * \brief Requests Motion Magic® to target a final velocity using a motion
129 * profile. This allows smooth transitions between velocity set points.
130 * Users can optionally provide a duty cycle feedforward.
131 *
132 * \details Motion Magic® Velocity produces a motion profile in real-time while
133 * attempting to honor the specified Acceleration and Jerk value. This
134 * control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
135 * configs. If the specified acceleration is zero, the Acceleration
136 * under Motion Magic® configuration parameter is used instead. This
137 * allows for runtime adjustment of acceleration for advanced users.
138 * Jerk is also specified in the Motion Magic® persistent configuration
139 * values. If Jerk is set to zero, Motion Magic® will produce a
140 * trapezoidal acceleration profile. Target velocity can also be
141 * changed on-the-fly and Motion Magic® will do its best to adjust the
142 * profile. This control mode is duty cycle based, so relevant
143 * closed-loop gains will use fractional duty cycle for the numerator:
144 * +1.0 represents full forward output.
145 *
146 * \param Velocity Target velocity to drive toward in rotations per second.
147 * This can be changed on-the fly.
148 * \param Acceleration This is the absolute Acceleration to use generating
149 * the profile. If this parameter is zero, the
150 * Acceleration persistent configuration parameter is
151 * used instead. Acceleration is in rotations per second
152 * squared. If nonzero, the signage does not matter as
153 * the absolute value is used.
154 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
155 * Pro), which increases peak power by ~15%. Set to false to
156 * use trapezoidal commutation.
157 *
158 * FOC improves motor performance by leveraging torque
159 * (current) control. However, this may be inconvenient for
160 * applications that require specifying duty cycle or
161 * voltage. CTR-Electronics has developed a hybrid method
162 * that combines the performances gains of FOC while still
163 * allowing applications to provide duty cycle or voltage
164 * demand. This not to be confused with simple sinusoidal
165 * control or phase voltage control which lacks the
166 * performance gains.
167 * \param FeedForward Feedforward to apply in fractional units between -1 and
168 * +1.
169 * \param Slot Select which gains are applied by selecting the slot. Use the
170 * configuration api to set the gain values for the selected slot
171 * before enabling this feature. Slot must be within [0,2].
172 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
173 * output is zero (or within deadband). Set
174 * to false to use the NeutralMode
175 * configuration setting (default). This flag
176 * exists to provide the fundamental behavior
177 * of this control when output is zero, which
178 * is to provide 0V to the motor.
179 * \param LimitForwardMotion Set to true to force forward limiting. This
180 * allows users to use other limit switch sensors
181 * connected to robot controller. This also allows
182 * use of active sensors that require external
183 * power.
184 * \param LimitReverseMotion Set to true to force reverse limiting. This
185 * allows users to use other limit switch sensors
186 * connected to robot controller. This also allows
187 * use of active sensors that require external
188 * power.
189 */
190 MotionMagicVelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq, bool EnableFOC = true, units::dimensionless::scalar_t FeedForward = 0.0, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicVelocityDutyCycle"},
191 Velocity{std::move(Velocity)},
192 Acceleration{std::move(Acceleration)},
193 EnableFOC{std::move(EnableFOC)},
194 FeedForward{std::move(FeedForward)},
195 Slot{std::move(Slot)},
199 {}
200
201 /**
202 * \brief Modifies this Control Request's Velocity parameter and returns itself for
203 * method-chaining and easier to use request API.
204 *
205 * Target velocity to drive toward in rotations per second. This can be changed
206 * on-the fly.
207 *
208 * \param newVelocity Parameter to modify
209 * \returns Itself
210 */
211 MotionMagicVelocityDutyCycle& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
212 {
213 Velocity = std::move(newVelocity);
214 return *this;
215 }
216
217 /**
218 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
219 * method-chaining and easier to use request API.
220 *
221 * This is the absolute Acceleration to use generating the profile. If this
222 * parameter is zero, the Acceleration persistent configuration parameter is
223 * used instead. Acceleration is in rotations per second squared. If nonzero,
224 * the signage does not matter as the absolute value is used.
225 *
226 * \param newAcceleration Parameter to modify
227 * \returns Itself
228 */
229 MotionMagicVelocityDutyCycle& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
230 {
231 Acceleration = std::move(newAcceleration);
232 return *this;
233 }
234
235 /**
236 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
237 * method-chaining and easier to use request API.
238 *
239 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
240 * peak power by ~15%. Set to false to use trapezoidal commutation.
241 *
242 * FOC improves motor performance by leveraging torque (current) control.
243 * However, this may be inconvenient for applications that require specifying
244 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
245 * combines the performances gains of FOC while still allowing applications to
246 * provide duty cycle or voltage demand. This not to be confused with simple
247 * sinusoidal control or phase voltage control which lacks the performance
248 * gains.
249 *
250 * \param newEnableFOC Parameter to modify
251 * \returns Itself
252 */
254 {
255 EnableFOC = std::move(newEnableFOC);
256 return *this;
257 }
258
259 /**
260 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
261 * method-chaining and easier to use request API.
262 *
263 * Feedforward to apply in fractional units between -1 and +1.
264 *
265 * \param newFeedForward Parameter to modify
266 * \returns Itself
267 */
268 MotionMagicVelocityDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
269 {
270 FeedForward = std::move(newFeedForward);
271 return *this;
272 }
273
274 /**
275 * \brief Modifies this Control Request's Slot parameter and returns itself for
276 * method-chaining and easier to use request API.
277 *
278 * Select which gains are applied by selecting the slot. Use the configuration
279 * api to set the gain values for the selected slot before enabling this
280 * feature. Slot must be within [0,2].
281 *
282 * \param newSlot Parameter to modify
283 * \returns Itself
284 */
286 {
287 Slot = std::move(newSlot);
288 return *this;
289 }
290
291 /**
292 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
293 * method-chaining and easier to use request API.
294 *
295 * Set to true to static-brake the rotor when output is zero (or within
296 * deadband). Set to false to use the NeutralMode configuration setting
297 * (default). This flag exists to provide the fundamental behavior of this
298 * control when output is zero, which is to provide 0V to the motor.
299 *
300 * \param newOverrideBrakeDurNeutral Parameter to modify
301 * \returns Itself
302 */
304 {
305 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
306 return *this;
307 }
308
309 /**
310 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
311 * method-chaining and easier to use request API.
312 *
313 * Set to true to force forward limiting. This allows users to use other limit
314 * switch sensors connected to robot controller. This also allows use of active
315 * sensors that require external power.
316 *
317 * \param newLimitForwardMotion Parameter to modify
318 * \returns Itself
319 */
321 {
322 LimitForwardMotion = std::move(newLimitForwardMotion);
323 return *this;
324 }
325
326 /**
327 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
328 * method-chaining and easier to use request API.
329 *
330 * Set to true to force reverse limiting. This allows users to use other limit
331 * switch sensors connected to robot controller. This also allows use of active
332 * sensors that require external power.
333 *
334 * \param newLimitReverseMotion Parameter to modify
335 * \returns Itself
336 */
338 {
339 LimitReverseMotion = std::move(newLimitReverseMotion);
340 return *this;
341 }
342 /**
343 * \brief Sets the period at which this control will update at.
344 * This is designated in Hertz, with a minimum of 20 Hz
345 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
346 *
347 * If this field is set to 0 Hz, the control request will
348 * be sent immediately as a one-shot frame. This may be useful
349 * for advanced applications that require outputs to be
350 * synchronized with data acquisition. In this case, we
351 * recommend not exceeding 50 ms between control calls.
352 *
353 * \param newUpdateFreqHz Parameter to modify
354 * \returns Itself
355 */
356 MotionMagicVelocityDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
357 {
358 UpdateFreqHz = newUpdateFreqHz;
359 return *this;
360 }
361 /**
362 * Returns a string representation of the object.
363 *
364 * \returns a string representation of the object.
365 */
366 std::string ToString() const override
367 {
368 std::stringstream ss;
369 ss << "class: MotionMagicVelocityDutyCycle" << std::endl;
370 ss << "Velocity: " << Velocity.to<double>() << std::endl;
371 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
372 ss << "EnableFOC: " << EnableFOC << std::endl;
373 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
374 ss << "Slot: " << Slot << std::endl;
375 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
376 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
377 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
378 return ss.str();
379 }
380
381 /**
382 * \brief Gets information about this control request.
383 *
384 * \returns Map of control parameter names and corresponding applied values
385 */
386 std::map<std::string, std::string> GetControlInfo() const override
387 {
388 std::map<std::string, std::string> controlInfo;
389 std::stringstream ss;
390 controlInfo["Name"] = GetName();
391 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
392 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
393 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
394 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
395 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
396 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
397 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
398 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
399 return controlInfo;
400 }
401};
402
403}
404}
405}
406
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicVelocityDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Velocity, double Acceleration, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, 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
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityDutyCycle.hpp:39
MotionMagicVelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration=0.0_tr_per_s_sq, bool EnableFOC=true, units::dimensionless::scalar_t FeedForward=0.0, int Slot=0, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityDutyCycle.hpp:190
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: MotionMagicVelocityDutyCycle.hpp:87
MotionMagicVelocityDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityDutyCycle.hpp:320
MotionMagicVelocityDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicVelocityDutyCycle.hpp:303
MotionMagicVelocityDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicVelocityDutyCycle.hpp:356
MotionMagicVelocityDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicVelocityDutyCycle.hpp:253
MotionMagicVelocityDutyCycle & 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: MotionMagicVelocityDutyCycle.hpp:229
MotionMagicVelocityDutyCycle & WithFeedForward(units::dimensionless::scalar_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: MotionMagicVelocityDutyCycle.hpp:268
units::angular_velocity::turns_per_second_t Velocity
Target velocity to drive toward in rotations per second.
Definition: MotionMagicVelocityDutyCycle.hpp:63
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicVelocityDutyCycle.hpp:112
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicVelocityDutyCycle.hpp:366
MotionMagicVelocityDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicVelocityDutyCycle.hpp:285
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicVelocityDutyCycle.hpp:93
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicVelocityDutyCycle.hpp:125
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: MotionMagicVelocityDutyCycle.hpp:83
units::angular_acceleration::turns_per_second_squared_t Acceleration
This is the absolute Acceleration to use generating the profile.
Definition: MotionMagicVelocityDutyCycle.hpp:70
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: MotionMagicVelocityDutyCycle.hpp:100
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicVelocityDutyCycle.hpp:386
MotionMagicVelocityDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityDutyCycle.hpp:337
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicVelocityDutyCycle.hpp:106
MotionMagicVelocityDutyCycle & 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: MotionMagicVelocityDutyCycle.hpp:211
Definition: string_util.hpp:15