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