CTRE Phoenix 6 C++ 24.2.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. FOC
64 * improves motor performance by leveraging torque (current) control. However,
65 * this may be inconvenient for applications that require specifying duty cycle
66 * or voltage. CTR-Electronics has developed a hybrid method that combines the
67 * performances gains of FOC while still allowing applications to provide duty
68 * cycle or voltage demand. This not to be confused with simple sinusoidal
69 * control or phase voltage control which lacks the performance gains.
70 */
72 /**
73 * Select which gains are applied to the primary controller by selecting the
74 * slot. Use the configuration api to set the gain values for the selected slot
75 * before enabling this feature. Slot must be within [0,2].
76 */
78 /**
79 * Select which gains are applied to the differential controller by selecting
80 * the slot. Use the configuration api to set the gain values for the selected
81 * slot before enabling this feature. Slot must be within [0,2].
82 */
84 /**
85 * Set to true to static-brake the rotor when output is zero (or within
86 * deadband). Set to false to use the NeutralMode configuration setting
87 * (default). This flag exists to provide the fundamental behavior of this
88 * control when output is zero, which is to provide 0V to the motor.
89 */
91 /**
92 * Set to true to force forward limiting. This allows users to use other limit
93 * switch sensors connected to robot controller. This also allows use of active
94 * sensors that require external power.
95 */
97 /**
98 * Set to true to force reverse limiting. This allows users to use other limit
99 * switch sensors connected to robot controller. This also allows use of active
100 * sensors that require external power.
101 */
103
104 /**
105 * \brief The period at which this control will update at.
106 * This is designated in Hertz, with a minimum of 20 Hz
107 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
108 *
109 * If this field is set to 0 Hz, the control request will
110 * be sent immediately as a one-shot frame. This may be useful
111 * for advanced applications that require outputs to be
112 * synchronized with data acquisition. In this case, we
113 * recommend not exceeding 50 ms between control calls.
114 */
115 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
116
117 /**
118 * \brief Requests Motion Magic® to target a final position using a motion
119 * profile, and PID to a differential position setpoint.
120 *
121 * \details Motion Magic® produces a motion profile in real-time while
122 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
123 * value specified via the Motion Magic® configuration values. This
124 * control mode does not use the Expo_kV or Expo_kA configs. Target
125 * position can be changed on-the-fly and Motion Magic® will do its
126 * best to adjust the profile. This control mode is duty cycle based,
127 * so relevant closed-loop gains will use fractional duty cycle for the
128 * numerator: +1.0 represents full forward output.
129 *
130 * \param TargetPosition Average position to drive toward in rotations.
131 * \param DifferentialPosition Differential position to drive toward in
132 * rotations.
133 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
134 * Pro), which increases peak power by ~15%. Set to false to
135 * use trapezoidal commutation. FOC improves motor
136 * performance by leveraging torque (current) control.
137 * However, this may be inconvenient for applications that
138 * require specifying duty cycle or voltage.
139 * CTR-Electronics has developed a hybrid method that
140 * combines the performances gains of FOC while still
141 * allowing applications to provide duty cycle or voltage
142 * demand. This not to be confused with simple sinusoidal
143 * control or phase voltage control which lacks the
144 * performance gains.
145 * \param TargetSlot Select which gains are applied to the primary controller
146 * by selecting the slot. Use the configuration api to set
147 * the gain values for the selected slot before enabling
148 * this feature. Slot must be within [0,2].
149 * \param DifferentialSlot Select which gains are applied to the differential
150 * controller by selecting the slot. Use the
151 * configuration api to set the gain values for the
152 * selected slot before enabling this feature. Slot
153 * must be within [0,2].
154 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
155 * output is zero (or within deadband). Set
156 * to false to use the NeutralMode
157 * configuration setting (default). This flag
158 * exists to provide the fundamental behavior
159 * of this control when output is zero, which
160 * is to provide 0V to the motor.
161 * \param LimitForwardMotion Set to true to force forward limiting. This
162 * allows users to use other limit switch sensors
163 * connected to robot controller. This also allows
164 * use of active sensors that require external
165 * power.
166 * \param LimitReverseMotion Set to true to force reverse limiting. This
167 * allows users to use other limit switch sensors
168 * connected to robot controller. This also allows
169 * use of active sensors that require external
170 * power.
171 */
172 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"},
173 TargetPosition{std::move(TargetPosition)},
175 EnableFOC{std::move(EnableFOC)},
176 TargetSlot{std::move(TargetSlot)},
181 {}
182
183 /**
184 * \brief Modifies this Control Request's TargetPosition parameter and returns itself for
185 * method-chaining and easier to use request API.
186 * \param newTargetPosition Parameter to modify
187 * \returns Itself
188 */
189 DifferentialMotionMagicDutyCycle& WithTargetPosition(units::angle::turn_t newTargetPosition)
190 {
191 TargetPosition = std::move(newTargetPosition);
192 return *this;
193 }
194
195 /**
196 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
197 * method-chaining and easier to use request API.
198 * \param newDifferentialPosition Parameter to modify
199 * \returns Itself
200 */
201 DifferentialMotionMagicDutyCycle& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
202 {
203 DifferentialPosition = std::move(newDifferentialPosition);
204 return *this;
205 }
206
207 /**
208 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
209 * method-chaining and easier to use request API.
210 * \param newEnableFOC Parameter to modify
211 * \returns Itself
212 */
214 {
215 EnableFOC = std::move(newEnableFOC);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
221 * method-chaining and easier to use request API.
222 * \param newTargetSlot Parameter to modify
223 * \returns Itself
224 */
226 {
227 TargetSlot = std::move(newTargetSlot);
228 return *this;
229 }
230
231 /**
232 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
233 * method-chaining and easier to use request API.
234 * \param newDifferentialSlot Parameter to modify
235 * \returns Itself
236 */
238 {
239 DifferentialSlot = std::move(newDifferentialSlot);
240 return *this;
241 }
242
243 /**
244 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
245 * method-chaining and easier to use request API.
246 * \param newOverrideBrakeDurNeutral Parameter to modify
247 * \returns Itself
248 */
250 {
251 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
252 return *this;
253 }
254
255 /**
256 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
257 * method-chaining and easier to use request API.
258 * \param newLimitForwardMotion Parameter to modify
259 * \returns Itself
260 */
262 {
263 LimitForwardMotion = std::move(newLimitForwardMotion);
264 return *this;
265 }
266
267 /**
268 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
269 * method-chaining and easier to use request API.
270 * \param newLimitReverseMotion Parameter to modify
271 * \returns Itself
272 */
274 {
275 LimitReverseMotion = std::move(newLimitReverseMotion);
276 return *this;
277 }
278 /**
279 * \brief Sets the period at which this control will update at.
280 * This is designated in Hertz, with a minimum of 20 Hz
281 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
282 *
283 * If this field is set to 0 Hz, the control request will
284 * be sent immediately as a one-shot frame. This may be useful
285 * for advanced applications that require outputs to be
286 * synchronized with data acquisition. In this case, we
287 * recommend not exceeding 50 ms between control calls.
288 *
289 * \param newUpdateFreqHz Parameter to modify
290 * \returns Itself
291 */
292 DifferentialMotionMagicDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
293 {
294 UpdateFreqHz = newUpdateFreqHz;
295 return *this;
296 }
297 /**
298 * Returns a string representation of the object.
299 *
300 * \returns a string representation of the object.
301 */
302 std::string ToString() const override
303 {
304 std::stringstream ss;
305 ss << "class: DifferentialMotionMagicDutyCycle" << std::endl;
306 ss << "TargetPosition: " << TargetPosition.to<double>() << std::endl;
307 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
308 ss << "EnableFOC: " << EnableFOC << std::endl;
309 ss << "TargetSlot: " << TargetSlot << std::endl;
310 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
311 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
312 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
313 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
314 return ss.str();
315 }
316
317 /**
318 * \brief Gets information about this control request.
319 *
320 * \returns Map of control parameter names and corresponding applied values
321 */
322 std::map<std::string, std::string> GetControlInfo() const override
323 {
324 std::map<std::string, std::string> controlInfo;
325 std::stringstream ss;
326 controlInfo["Name"] = GetName();
327 ss << TargetPosition.to<double>(); controlInfo["TargetPosition"] = ss.str(); ss.str(std::string{});
328 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
329 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
330 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
331 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
332 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
333 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
334 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
335 return controlInfo;
336 }
337};
338
339}
340}
341}
342
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:102
DifferentialMotionMagicDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialMotionMagicDutyCycle.hpp:261
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialMotionMagicDutyCycle.hpp:77
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:189
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialMotionMagicDutyCycle.hpp:90
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialMotionMagicDutyCycle.hpp:83
DifferentialMotionMagicDutyCycle & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialMotionMagicDutyCycle.hpp:225
DifferentialMotionMagicDutyCycle & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialMotionMagicDutyCycle.hpp:237
DifferentialMotionMagicDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialMotionMagicDutyCycle.hpp:249
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:71
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:172
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialMotionMagicDutyCycle.hpp:302
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DifferentialMotionMagicDutyCycle.hpp:96
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialMotionMagicDutyCycle.hpp:322
DifferentialMotionMagicDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialMotionMagicDutyCycle.hpp:292
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialMotionMagicDutyCycle.hpp:115
DifferentialMotionMagicDutyCycle & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialMotionMagicDutyCycle.hpp:201
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:213
DifferentialMotionMagicDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DifferentialMotionMagicDutyCycle.hpp:273
Definition: RcManualEvent.hpp:12