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