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