CTRE Phoenix 6 C++ 24.3.0
DynamicMotionMagicVoltage.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/voltage.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 voltage feedforward. This control
31 * 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 voltage-based, so relevant closed-loop gains will use Volts for the numerator.
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<DynamicMotionMagicVoltage *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<DynamicMotionMagicVoltage>(*this);
52 }
53 }
54
55 return c_ctre_phoenix6_RequestControlDynamicMotionMagicVoltage(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);
56 }
57
58public:
59 /**
60 * Position to drive toward in rotations.
61 */
62 units::angle::turn_t Position;
63 /**
64 * Cruise velocity for profiling. The signage does not matter as the device
65 * will use the absolute value for profile generation.
66 */
67 units::angular_velocity::turns_per_second_t Velocity;
68 /**
69 * Acceleration for profiling. The signage does not matter as the device will
70 * use the absolute value for profile generation.
71 */
72 units::angular_acceleration::turns_per_second_squared_t Acceleration;
73 /**
74 * Jerk for profiling. The signage does not matter as the device will use the
75 * absolute value for profile generation.
76 */
77 units::angular_jerk::turns_per_second_cubed_t Jerk;
78 /**
79 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
80 * peak power by ~15%. Set to false to use trapezoidal commutation.
81 *
82 * FOC improves motor performance by leveraging torque (current) control.
83 * However, this may be inconvenient for applications that require specifying
84 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
85 * combines the performances gains of FOC while still allowing applications to
86 * provide duty cycle or voltage demand. This not to be confused with simple
87 * sinusoidal control or phase voltage control which lacks the performance
88 * gains.
89 */
91 /**
92 * Feedforward to apply in volts
93 */
94 units::voltage::volt_t FeedForward;
95 /**
96 * Select which gains are applied by selecting the slot. Use the configuration
97 * api to set the gain values for the selected slot before enabling this
98 * feature. Slot must be within [0,2].
99 */
100 int Slot;
101 /**
102 * Set to true to static-brake the rotor when output is zero (or within
103 * deadband). Set to false to use the NeutralMode configuration setting
104 * (default). This flag exists to provide the fundamental behavior of this
105 * control when output is zero, which is to provide 0V to the motor.
106 */
108 /**
109 * Set to true to force forward limiting. This allows users to use other limit
110 * switch sensors connected to robot controller. This also allows use of active
111 * sensors that require external power.
112 */
114 /**
115 * Set to true to force reverse limiting. This allows users to use other limit
116 * switch sensors connected to robot controller. This also allows use of active
117 * sensors that require external power.
118 */
120
121 /**
122 * \brief The period at which this control will update at.
123 * This is designated in Hertz, with a minimum of 20 Hz
124 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
125 *
126 * If this field is set to 0 Hz, the control request will
127 * be sent immediately as a one-shot frame. This may be useful
128 * for advanced applications that require outputs to be
129 * synchronized with data acquisition. In this case, we
130 * recommend not exceeding 50 ms between control calls.
131 */
132 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
133
134 /**
135 * \brief Requires Phoenix Pro and CANivore;
136 * Requests Motion Magic® to target a final position using a motion
137 * profile. This dynamic request allows runtime changes to Cruise
138 * Velocity, Acceleration, and Jerk. Users can optionally provide a
139 * voltage feedforward. This control requires 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 * voltage-based, so relevant closed-loop gains will use Volts for the
147 * numerator.
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.
161 *
162 * FOC improves motor performance by leveraging torque
163 * (current) control. However, this may be inconvenient for
164 * applications that require specifying duty cycle or
165 * voltage. CTR-Electronics has developed a hybrid method
166 * that combines the performances gains of FOC while still
167 * allowing applications to provide duty cycle or voltage
168 * demand. This not to be confused with simple sinusoidal
169 * control or phase voltage control which lacks the
170 * performance gains.
171 * \param FeedForward Feedforward to apply in volts
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 DynamicMotionMagicVoltage(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::voltage::volt_t FeedForward = 0.0_V, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"DynamicMotionMagicVoltage"},
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 *
210 * Position to drive toward in rotations.
211 *
212 * \param newPosition Parameter to modify
213 * \returns Itself
214 */
215 DynamicMotionMagicVoltage& WithPosition(units::angle::turn_t newPosition)
216 {
217 Position = std::move(newPosition);
218 return *this;
219 }
220
221 /**
222 * \brief Modifies this Control Request's Velocity parameter and returns itself for
223 * method-chaining and easier to use request API.
224 *
225 * Cruise velocity for profiling. The signage does not matter as the device
226 * will use the absolute value for profile generation.
227 *
228 * \param newVelocity Parameter to modify
229 * \returns Itself
230 */
231 DynamicMotionMagicVoltage& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
232 {
233 Velocity = std::move(newVelocity);
234 return *this;
235 }
236
237 /**
238 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
239 * method-chaining and easier to use request API.
240 *
241 * Acceleration for profiling. The signage does not matter as the device will
242 * use the absolute value for profile generation.
243 *
244 * \param newAcceleration Parameter to modify
245 * \returns Itself
246 */
247 DynamicMotionMagicVoltage& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
248 {
249 Acceleration = std::move(newAcceleration);
250 return *this;
251 }
252
253 /**
254 * \brief Modifies this Control Request's Jerk parameter and returns itself for
255 * method-chaining and easier to use request API.
256 *
257 * Jerk for profiling. The signage does not matter as the device will use the
258 * absolute value for profile generation.
259 *
260 * \param newJerk Parameter to modify
261 * \returns Itself
262 */
263 DynamicMotionMagicVoltage& WithJerk(units::angular_jerk::turns_per_second_cubed_t newJerk)
264 {
265 Jerk = std::move(newJerk);
266 return *this;
267 }
268
269 /**
270 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
271 * method-chaining and easier to use request API.
272 *
273 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
274 * peak power by ~15%. Set to false to use trapezoidal commutation.
275 *
276 * FOC improves motor performance by leveraging torque (current) control.
277 * However, this may be inconvenient for applications that require specifying
278 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
279 * combines the performances gains of FOC while still allowing applications to
280 * provide duty cycle or voltage demand. This not to be confused with simple
281 * sinusoidal control or phase voltage control which lacks the performance
282 * gains.
283 *
284 * \param newEnableFOC Parameter to modify
285 * \returns Itself
286 */
288 {
289 EnableFOC = std::move(newEnableFOC);
290 return *this;
291 }
292
293 /**
294 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
295 * method-chaining and easier to use request API.
296 *
297 * Feedforward to apply in volts
298 *
299 * \param newFeedForward Parameter to modify
300 * \returns Itself
301 */
302 DynamicMotionMagicVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
303 {
304 FeedForward = std::move(newFeedForward);
305 return *this;
306 }
307
308 /**
309 * \brief Modifies this Control Request's Slot parameter and returns itself for
310 * method-chaining and easier to use request API.
311 *
312 * Select which gains are applied by selecting the slot. Use the configuration
313 * api to set the gain values for the selected slot before enabling this
314 * feature. Slot must be within [0,2].
315 *
316 * \param newSlot Parameter to modify
317 * \returns Itself
318 */
320 {
321 Slot = std::move(newSlot);
322 return *this;
323 }
324
325 /**
326 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
327 * method-chaining and easier to use request API.
328 *
329 * Set to true to static-brake the rotor when output is zero (or within
330 * deadband). Set to false to use the NeutralMode configuration setting
331 * (default). This flag exists to provide the fundamental behavior of this
332 * control when output is zero, which is to provide 0V to the motor.
333 *
334 * \param newOverrideBrakeDurNeutral Parameter to modify
335 * \returns Itself
336 */
338 {
339 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
340 return *this;
341 }
342
343 /**
344 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
345 * method-chaining and easier to use request API.
346 *
347 * Set to true to force forward limiting. This allows users to use other limit
348 * switch sensors connected to robot controller. This also allows use of active
349 * sensors that require external power.
350 *
351 * \param newLimitForwardMotion Parameter to modify
352 * \returns Itself
353 */
355 {
356 LimitForwardMotion = std::move(newLimitForwardMotion);
357 return *this;
358 }
359
360 /**
361 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
362 * method-chaining and easier to use request API.
363 *
364 * Set to true to force reverse limiting. This allows users to use other limit
365 * switch sensors connected to robot controller. This also allows use of active
366 * sensors that require external power.
367 *
368 * \param newLimitReverseMotion Parameter to modify
369 * \returns Itself
370 */
372 {
373 LimitReverseMotion = std::move(newLimitReverseMotion);
374 return *this;
375 }
376 /**
377 * \brief Sets the period at which this control will update at.
378 * This is designated in Hertz, with a minimum of 20 Hz
379 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
380 *
381 * If this field is set to 0 Hz, the control request will
382 * be sent immediately as a one-shot frame. This may be useful
383 * for advanced applications that require outputs to be
384 * synchronized with data acquisition. In this case, we
385 * recommend not exceeding 50 ms between control calls.
386 *
387 * \param newUpdateFreqHz Parameter to modify
388 * \returns Itself
389 */
390 DynamicMotionMagicVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
391 {
392 UpdateFreqHz = newUpdateFreqHz;
393 return *this;
394 }
395 /**
396 * Returns a string representation of the object.
397 *
398 * \returns a string representation of the object.
399 */
400 std::string ToString() const override
401 {
402 std::stringstream ss;
403 ss << "class: DynamicMotionMagicVoltage" << std::endl;
404 ss << "Position: " << Position.to<double>() << std::endl;
405 ss << "Velocity: " << Velocity.to<double>() << std::endl;
406 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
407 ss << "Jerk: " << Jerk.to<double>() << std::endl;
408 ss << "EnableFOC: " << EnableFOC << std::endl;
409 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
410 ss << "Slot: " << Slot << std::endl;
411 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
412 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
413 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
414 return ss.str();
415 }
416
417 /**
418 * \brief Gets information about this control request.
419 *
420 * \returns Map of control parameter names and corresponding applied values
421 */
422 std::map<std::string, std::string> GetControlInfo() const override
423 {
424 std::map<std::string, std::string> controlInfo;
425 std::stringstream ss;
426 controlInfo["Name"] = GetName();
427 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
428 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
429 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
430 ss << Jerk.to<double>(); controlInfo["Jerk"] = ss.str(); ss.str(std::string{});
431 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
432 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
433 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
434 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
435 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
436 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
437 return controlInfo;
438 }
439};
440
441}
442}
443}
444
CTREXPORT int c_ctre_phoenix6_RequestControlDynamicMotionMagicVoltage(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: DynamicMotionMagicVoltage.hpp:39
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration for profiling.
Definition: DynamicMotionMagicVoltage.hpp:72
DynamicMotionMagicVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DynamicMotionMagicVoltage.hpp:287
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: DynamicMotionMagicVoltage.hpp:94
DynamicMotionMagicVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DynamicMotionMagicVoltage.hpp:354
DynamicMotionMagicVoltage & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: DynamicMotionMagicVoltage.hpp:215
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: DynamicMotionMagicVoltage.hpp:119
DynamicMotionMagicVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DynamicMotionMagicVoltage.hpp:371
units::angular_jerk::turns_per_second_cubed_t Jerk
Jerk for profiling.
Definition: DynamicMotionMagicVoltage.hpp:77
DynamicMotionMagicVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DynamicMotionMagicVoltage.hpp:337
std::string ToString() const override
Returns a string representation of the object.
Definition: DynamicMotionMagicVoltage.hpp:400
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DynamicMotionMagicVoltage.hpp:132
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DynamicMotionMagicVoltage.hpp:107
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DynamicMotionMagicVoltage.hpp:90
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DynamicMotionMagicVoltage.hpp:113
DynamicMotionMagicVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: DynamicMotionMagicVoltage.hpp:302
DynamicMotionMagicVoltage & 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: DynamicMotionMagicVoltage.hpp:247
DynamicMotionMagicVoltage & 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: DynamicMotionMagicVoltage.hpp:263
DynamicMotionMagicVoltage(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::voltage::volt_t FeedForward=0.0_V, 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: DynamicMotionMagicVoltage.hpp:193
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: DynamicMotionMagicVoltage.hpp:62
DynamicMotionMagicVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DynamicMotionMagicVoltage.hpp:390
int Slot
Select which gains are applied by selecting the slot.
Definition: DynamicMotionMagicVoltage.hpp:100
DynamicMotionMagicVoltage & 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: DynamicMotionMagicVoltage.hpp:231
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DynamicMotionMagicVoltage.hpp:422
DynamicMotionMagicVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: DynamicMotionMagicVoltage.hpp:319
units::angular_velocity::turns_per_second_t Velocity
Cruise velocity for profiling.
Definition: DynamicMotionMagicVoltage.hpp:67
Definition: string_util.hpp:15