CTRE Phoenix 6 C++ 24.3.0
DifferentialMotionMagicVoltage.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 voltage-based, so relevant closed-loop gains will
30 * use Volts for the numerator.
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<DifferentialMotionMagicVoltage *>(req.get());
39 if (reqCast != nullptr)
40 {
41 *reqCast = *this;
42 }
43 else
44 {
45 req = std::make_shared<DifferentialMotionMagicVoltage>(*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 voltage-based, so
129 * relevant closed-loop gains will use Volts for the numerator.
130 *
131 * \param TargetPosition Average position to drive toward in rotations.
132 * \param DifferentialPosition Differential position to drive toward in
133 * rotations.
134 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
135 * Pro), which increases peak power by ~15%. Set to false to
136 * use trapezoidal commutation.
137 *
138 * FOC improves motor performance by leveraging torque
139 * (current) control. However, this may be inconvenient for
140 * applications that require specifying duty cycle or
141 * voltage. CTR-Electronics has developed a hybrid method
142 * that combines the performances gains of FOC while still
143 * allowing applications to provide duty cycle or voltage
144 * demand. This not to be confused with simple sinusoidal
145 * control or phase voltage control which lacks the
146 * performance gains.
147 * \param TargetSlot Select which gains are applied to the primary controller
148 * by selecting the slot. Use the configuration api to set
149 * the gain values for the selected slot before enabling
150 * this feature. Slot must be within [0,2].
151 * \param DifferentialSlot Select which gains are applied to the differential
152 * controller by selecting the slot. Use the
153 * configuration api to set the gain values for the
154 * selected slot before enabling this feature. Slot
155 * must be within [0,2].
156 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
157 * output is zero (or within deadband). Set
158 * to false to use the NeutralMode
159 * configuration setting (default). This flag
160 * exists to provide the fundamental behavior
161 * of this control when output is zero, which
162 * is to provide 0V to the motor.
163 * \param LimitForwardMotion Set to true to force forward limiting. This
164 * allows users to use other limit switch sensors
165 * connected to robot controller. This also allows
166 * use of active sensors that require external
167 * power.
168 * \param LimitReverseMotion Set to true to force reverse limiting. This
169 * allows users to use other limit switch sensors
170 * connected to robot controller. This also allows
171 * use of active sensors that require external
172 * power.
173 */
174 DifferentialMotionMagicVoltage(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{"DifferentialMotionMagicVoltage"},
175 TargetPosition{std::move(TargetPosition)},
177 EnableFOC{std::move(EnableFOC)},
178 TargetSlot{std::move(TargetSlot)},
183 {}
184
185 /**
186 * \brief Modifies this Control Request's TargetPosition parameter and returns itself for
187 * method-chaining and easier to use request API.
188 *
189 * Average position to drive toward in rotations.
190 *
191 * \param newTargetPosition Parameter to modify
192 * \returns Itself
193 */
194 DifferentialMotionMagicVoltage& WithTargetPosition(units::angle::turn_t newTargetPosition)
195 {
196 TargetPosition = std::move(newTargetPosition);
197 return *this;
198 }
199
200 /**
201 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
202 * method-chaining and easier to use request API.
203 *
204 * Differential position to drive toward in rotations.
205 *
206 * \param newDifferentialPosition Parameter to modify
207 * \returns Itself
208 */
209 DifferentialMotionMagicVoltage& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
210 {
211 DifferentialPosition = std::move(newDifferentialPosition);
212 return *this;
213 }
214
215 /**
216 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
217 * method-chaining and easier to use request API.
218 *
219 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
220 * peak power by ~15%. Set to false to use trapezoidal commutation.
221 *
222 * FOC improves motor performance by leveraging torque (current) control.
223 * However, this may be inconvenient for applications that require specifying
224 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
225 * combines the performances gains of FOC while still allowing applications to
226 * provide duty cycle or voltage demand. This not to be confused with simple
227 * sinusoidal control or phase voltage control which lacks the performance
228 * gains.
229 *
230 * \param newEnableFOC Parameter to modify
231 * \returns Itself
232 */
234 {
235 EnableFOC = std::move(newEnableFOC);
236 return *this;
237 }
238
239 /**
240 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
241 * method-chaining and easier to use request API.
242 *
243 * Select which gains are applied to the primary controller by selecting the
244 * slot. Use the configuration api to set the gain values for the selected slot
245 * before enabling this feature. Slot must be within [0,2].
246 *
247 * \param newTargetSlot Parameter to modify
248 * \returns Itself
249 */
251 {
252 TargetSlot = std::move(newTargetSlot);
253 return *this;
254 }
255
256 /**
257 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
258 * method-chaining and easier to use request API.
259 *
260 * Select which gains are applied to the differential controller by selecting
261 * the slot. Use the configuration api to set the gain values for the selected
262 * slot before enabling this feature. Slot must be within [0,2].
263 *
264 * \param newDifferentialSlot Parameter to modify
265 * \returns Itself
266 */
268 {
269 DifferentialSlot = std::move(newDifferentialSlot);
270 return *this;
271 }
272
273 /**
274 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
275 * method-chaining and easier to use request API.
276 *
277 * Set to true to static-brake the rotor when output is zero (or within
278 * deadband). Set to false to use the NeutralMode configuration setting
279 * (default). This flag exists to provide the fundamental behavior of this
280 * control when output is zero, which is to provide 0V to the motor.
281 *
282 * \param newOverrideBrakeDurNeutral Parameter to modify
283 * \returns Itself
284 */
286 {
287 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
288 return *this;
289 }
290
291 /**
292 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
293 * method-chaining and easier to use request API.
294 *
295 * Set to true to force forward limiting. This allows users to use other limit
296 * switch sensors connected to robot controller. This also allows use of active
297 * sensors that require external power.
298 *
299 * \param newLimitForwardMotion Parameter to modify
300 * \returns Itself
301 */
303 {
304 LimitForwardMotion = std::move(newLimitForwardMotion);
305 return *this;
306 }
307
308 /**
309 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
310 * method-chaining and easier to use request API.
311 *
312 * Set to true to force reverse limiting. This allows users to use other limit
313 * switch sensors connected to robot controller. This also allows use of active
314 * sensors that require external power.
315 *
316 * \param newLimitReverseMotion Parameter to modify
317 * \returns Itself
318 */
320 {
321 LimitReverseMotion = std::move(newLimitReverseMotion);
322 return *this;
323 }
324 /**
325 * \brief Sets the period at which this control will update at.
326 * This is designated in Hertz, with a minimum of 20 Hz
327 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
328 *
329 * If this field is set to 0 Hz, the control request will
330 * be sent immediately as a one-shot frame. This may be useful
331 * for advanced applications that require outputs to be
332 * synchronized with data acquisition. In this case, we
333 * recommend not exceeding 50 ms between control calls.
334 *
335 * \param newUpdateFreqHz Parameter to modify
336 * \returns Itself
337 */
338 DifferentialMotionMagicVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
339 {
340 UpdateFreqHz = newUpdateFreqHz;
341 return *this;
342 }
343 /**
344 * Returns a string representation of the object.
345 *
346 * \returns a string representation of the object.
347 */
348 std::string ToString() const override
349 {
350 std::stringstream ss;
351 ss << "class: DifferentialMotionMagicVoltage" << std::endl;
352 ss << "TargetPosition: " << TargetPosition.to<double>() << std::endl;
353 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
354 ss << "EnableFOC: " << EnableFOC << std::endl;
355 ss << "TargetSlot: " << TargetSlot << std::endl;
356 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
357 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
358 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
359 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
360 return ss.str();
361 }
362
363 /**
364 * \brief Gets information about this control request.
365 *
366 * \returns Map of control parameter names and corresponding applied values
367 */
368 std::map<std::string, std::string> GetControlInfo() const override
369 {
370 std::map<std::string, std::string> controlInfo;
371 std::stringstream ss;
372 controlInfo["Name"] = GetName();
373 ss << TargetPosition.to<double>(); controlInfo["TargetPosition"] = ss.str(); ss.str(std::string{});
374 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
375 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
376 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
377 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
378 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
379 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
380 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
381 return controlInfo;
382 }
383};
384
385}
386}
387}
388
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialMotionMagicVoltage(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: DifferentialMotionMagicVoltage.hpp:33
DifferentialMotionMagicVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialMotionMagicVoltage.hpp:209
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialMotionMagicVoltage.hpp:117
units::angle::turn_t DifferentialPosition
Differential position to drive toward in rotations.
Definition: DifferentialMotionMagicVoltage.hpp:60
DifferentialMotionMagicVoltage(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: DifferentialMotionMagicVoltage.hpp:174
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialMotionMagicVoltage.hpp:85
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialMotionMagicVoltage.hpp:92
units::angle::turn_t TargetPosition
Average position to drive toward in rotations.
Definition: DifferentialMotionMagicVoltage.hpp:56
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialMotionMagicVoltage.hpp:348
DifferentialMotionMagicVoltage & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialMotionMagicVoltage.hpp:250
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialMotionMagicVoltage.hpp:79
DifferentialMotionMagicVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialMotionMagicVoltage.hpp:233
DifferentialMotionMagicVoltage & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialMotionMagicVoltage.hpp:267
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: DifferentialMotionMagicVoltage.hpp:104
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialMotionMagicVoltage.hpp:73
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DifferentialMotionMagicVoltage.hpp:98
DifferentialMotionMagicVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialMotionMagicVoltage.hpp:285
DifferentialMotionMagicVoltage & WithTargetPosition(units::angle::turn_t newTargetPosition)
Modifies this Control Request's TargetPosition parameter and returns itself for method-chaining and e...
Definition: DifferentialMotionMagicVoltage.hpp:194
DifferentialMotionMagicVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialMotionMagicVoltage.hpp:338
DifferentialMotionMagicVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DifferentialMotionMagicVoltage.hpp:319
DifferentialMotionMagicVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialMotionMagicVoltage.hpp:302
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialMotionMagicVoltage.hpp:368
Definition: string_util.hpp:15