CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
MotionMagicDutyCycle.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
10#include <units/frequency.h>
11#include <units/time.h>
12#include <units/angle.h>
13#include <units/dimensionless.h>
14
15namespace ctre {
16namespace phoenix6 {
17namespace controls {
18
19/**
20 * Requests Motion Magic® to target a final position using a motion profile.
21 * Users can optionally provide a duty cycle feedforward.
22 *
23 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
24 * Acceleration, and (optional) Jerk specified via the Motion Magic® configuration values. This control mode
25 * does not use the Expo_kV or Expo_kA configs.
26 *
27 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile. This
28 * control mode is duty cycle based, so relevant closed-loop gains will use fractional duty cycle for the
29 * numerator: +1.0 represents full forward output.
30 */
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override;
33
34public:
35 /**
36 * \brief Position to drive toward in rotations.
37 *
38 * - Units: rotations
39 *
40 */
41 units::angle::turn_t Position;
42 /**
43 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
44 * increases peak power by ~15% on supported devices (see
45 * hardware#traits#SupportsFOC). Set to false to use trapezoidal commutation.
46 *
47 * FOC improves motor performance by leveraging torque (current) control.
48 * However, this may be inconvenient for applications that require specifying
49 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
50 * combines the performances gains of FOC while still allowing applications to
51 * provide duty cycle or voltage demand. This not to be confused with simple
52 * sinusoidal control or phase voltage control which lacks the performance
53 * gains.
54 */
55 bool EnableFOC = true;
56 /**
57 * \brief Feedforward to apply in fractional units between -1 and +1. This is
58 * added to the output of the onboard feedforward terms.
59 *
60 * - Units: fractional
61 *
62 */
63 units::dimensionless::scalar_t FeedForward = 0.0;
64 /**
65 * \brief Select which gains are applied by selecting the slot. Use the
66 * configuration api to set the gain values for the selected slot before
67 * enabling this feature. Slot must be within [0,2].
68 */
69 int Slot = 0;
70 /**
71 * \brief Set to true to static-brake the rotor when output is zero (or within
72 * deadband). Set to false to use the NeutralMode configuration setting
73 * (default). This flag exists to provide the fundamental behavior of this
74 * control when output is zero, which is to provide 0V to the motor.
75 */
77 /**
78 * \brief Set to true to force forward limiting. This allows users to use other
79 * limit switch sensors connected to robot controller. This also allows use of
80 * active sensors that require external power.
81 */
82 bool LimitForwardMotion = false;
83 /**
84 * \brief Set to true to force reverse limiting. This allows users to use other
85 * limit switch sensors connected to robot controller. This also allows use of
86 * active sensors that require external power.
87 */
88 bool LimitReverseMotion = false;
89 /**
90 * \brief Set to true to ignore hardware limit switches and the
91 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
92 * motion.
93 *
94 * This can be useful on mechanisms such as an intake/feeder, where a limit
95 * switch stops motion while intaking but should be ignored when feeding to a
96 * shooter.
97 *
98 * The hardware limit faults and Forward/ReverseLimit signals will still report
99 * the values of the limit switches regardless of this parameter.
100 */
102 /**
103 * \brief Set to true to ignore software limits, instead allowing motion.
104 *
105 * This can be useful when calibrating the zero point of a mechanism such as an
106 * elevator.
107 *
108 * The software limit faults will still report the values of the software limits
109 * regardless of this parameter.
110 */
112 /**
113 * \brief Set to true to delay applying this control request until a timesync
114 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
115 * nondeterministic network delays in exchange for a larger but deterministic
116 * control latency.
117 *
118 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
119 * Additionally, when this is enabled, the UpdateFreqHz of this request should
120 * be set to 0 Hz.
121 */
122 bool UseTimesync = false;
123
124 /**
125 * \brief The frequency at which this control will update.
126 * This is designated in Hertz, with a minimum of 20 Hz
127 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
128 * Some update frequencies are not supported and will be
129 * promoted up to the next highest supported frequency.
130 *
131 * If this field is set to 0 Hz, the control request will
132 * be sent immediately as a one-shot frame. This may be useful
133 * for advanced applications that require outputs to be
134 * synchronized with data acquisition. In this case, we
135 * recommend not exceeding 50 ms between control calls.
136 */
137 units::frequency::hertz_t UpdateFreqHz{100_Hz};
138
139 /**
140 * \brief Requests Motion Magic® to target a final position using a motion
141 * profile. Users can optionally provide a duty cycle feedforward.
142 *
143 * \details Motion Magic® produces a motion profile in real-time while
144 * attempting to honor the Cruise Velocity, Acceleration, and
145 * (optional) Jerk specified via the Motion Magic® configuration
146 * values. This control mode does not use the Expo_kV or Expo_kA
147 * configs.
148 *
149 * Target position can be changed on-the-fly and Motion Magic® will do
150 * its best to adjust the profile. This control mode is duty cycle
151 * based, so relevant closed-loop gains will use fractional duty cycle
152 * for the numerator: +1.0 represents full forward output.
153 *
154 * \param Position Position to drive toward in rotations.
155 */
156 constexpr MotionMagicDutyCycle(units::angle::turn_t Position) : ControlRequest{},
157 Position{std::move(Position)}
158 {}
159
160 constexpr ~MotionMagicDutyCycle() override {}
161
162 /**
163 * \brief Gets the name of this control request.
164 *
165 * \returns Name of the control request
166 */
167 constexpr std::string_view GetName() const override
168 {
169 return "MotionMagicDutyCycle";
170 }
171
172 /**
173 * \brief Modifies this Control Request's Position parameter and returns itself for
174 * method-chaining and easier to use request API.
175 *
176 * Position to drive toward in rotations.
177 *
178 * - Units: rotations
179 *
180 *
181 * \param newPosition Parameter to modify
182 * \returns Itself
183 */
184 constexpr MotionMagicDutyCycle &WithPosition(units::angle::turn_t newPosition)
185 {
186 Position = std::move(newPosition);
187 return *this;
188 }
189
190 /**
191 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
192 * method-chaining and easier to use request API.
193 *
194 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
195 * peak power by ~15% on supported devices (see hardware#traits#SupportsFOC).
196 * Set to false to use trapezoidal commutation.
197 *
198 * FOC improves motor performance by leveraging torque (current) control.
199 * However, this may be inconvenient for applications that require specifying
200 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
201 * combines the performances gains of FOC while still allowing applications to
202 * provide duty cycle or voltage demand. This not to be confused with simple
203 * sinusoidal control or phase voltage control which lacks the performance
204 * gains.
205 *
206 * \param newEnableFOC Parameter to modify
207 * \returns Itself
208 */
209 constexpr MotionMagicDutyCycle &WithEnableFOC(bool newEnableFOC)
210 {
211 EnableFOC = std::move(newEnableFOC);
212 return *this;
213 }
214
215 /**
216 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
217 * method-chaining and easier to use request API.
218 *
219 * Feedforward to apply in fractional units between -1 and +1. This is added to
220 * the output of the onboard feedforward terms.
221 *
222 * - Units: fractional
223 *
224 *
225 * \param newFeedForward Parameter to modify
226 * \returns Itself
227 */
228 constexpr MotionMagicDutyCycle &WithFeedForward(units::dimensionless::scalar_t newFeedForward)
229 {
230 FeedForward = std::move(newFeedForward);
231 return *this;
232 }
233
234 /**
235 * \brief Modifies this Control Request's Slot parameter and returns itself for
236 * method-chaining and easier to use request API.
237 *
238 * Select which gains are applied by selecting the slot. Use the configuration
239 * api to set the gain values for the selected slot before enabling this
240 * feature. Slot must be within [0,2].
241 *
242 * \param newSlot Parameter to modify
243 * \returns Itself
244 */
245 constexpr MotionMagicDutyCycle &WithSlot(int newSlot)
246 {
247 Slot = std::move(newSlot);
248 return *this;
249 }
250
251 /**
252 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
253 * method-chaining and easier to use request API.
254 *
255 * Set to true to static-brake the rotor when output is zero (or within
256 * deadband). Set to false to use the NeutralMode configuration setting
257 * (default). This flag exists to provide the fundamental behavior of this
258 * control when output is zero, which is to provide 0V to the motor.
259 *
260 * \param newOverrideBrakeDurNeutral Parameter to modify
261 * \returns Itself
262 */
263 constexpr MotionMagicDutyCycle &WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
264 {
265 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
266 return *this;
267 }
268
269 /**
270 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
271 * method-chaining and easier to use request API.
272 *
273 * Set to true to force forward limiting. This allows users to use other limit
274 * switch sensors connected to robot controller. This also allows use of active
275 * sensors that require external power.
276 *
277 * \param newLimitForwardMotion Parameter to modify
278 * \returns Itself
279 */
280 constexpr MotionMagicDutyCycle &WithLimitForwardMotion(bool newLimitForwardMotion)
281 {
282 LimitForwardMotion = std::move(newLimitForwardMotion);
283 return *this;
284 }
285
286 /**
287 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
288 * method-chaining and easier to use request API.
289 *
290 * Set to true to force reverse limiting. This allows users to use other limit
291 * switch sensors connected to robot controller. This also allows use of active
292 * sensors that require external power.
293 *
294 * \param newLimitReverseMotion Parameter to modify
295 * \returns Itself
296 */
297 constexpr MotionMagicDutyCycle &WithLimitReverseMotion(bool newLimitReverseMotion)
298 {
299 LimitReverseMotion = std::move(newLimitReverseMotion);
300 return *this;
301 }
302
303 /**
304 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
305 * method-chaining and easier to use request API.
306 *
307 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
308 * LimitReverseMotion parameters, instead allowing motion.
309 *
310 * This can be useful on mechanisms such as an intake/feeder, where a limit
311 * switch stops motion while intaking but should be ignored when feeding to a
312 * shooter.
313 *
314 * The hardware limit faults and Forward/ReverseLimit signals will still report
315 * the values of the limit switches regardless of this parameter.
316 *
317 * \param newIgnoreHardwareLimits Parameter to modify
318 * \returns Itself
319 */
320 constexpr MotionMagicDutyCycle &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
321 {
322 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
323 return *this;
324 }
325
326 /**
327 * \brief Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for
328 * method-chaining and easier to use request API.
329 *
330 * Set to true to ignore software limits, instead allowing motion.
331 *
332 * This can be useful when calibrating the zero point of a mechanism such as an
333 * elevator.
334 *
335 * The software limit faults will still report the values of the software limits
336 * regardless of this parameter.
337 *
338 * \param newIgnoreSoftwareLimits Parameter to modify
339 * \returns Itself
340 */
341 constexpr MotionMagicDutyCycle &WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
342 {
343 IgnoreSoftwareLimits = std::move(newIgnoreSoftwareLimits);
344 return *this;
345 }
346
347 /**
348 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
349 * method-chaining and easier to use request API.
350 *
351 * Set to true to delay applying this control request until a timesync boundary
352 * (requires Phoenix Pro and CANivore). This eliminates the impact of
353 * nondeterministic network delays in exchange for a larger but deterministic
354 * control latency.
355 *
356 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
357 * Additionally, when this is enabled, the UpdateFreqHz of this request should
358 * be set to 0 Hz.
359 *
360 * \param newUseTimesync Parameter to modify
361 * \returns Itself
362 */
363 constexpr MotionMagicDutyCycle &WithUseTimesync(bool newUseTimesync)
364 {
365 UseTimesync = std::move(newUseTimesync);
366 return *this;
367 }
368
369 /**
370 * \brief Sets the frequency at which this control will update.
371 * This is designated in Hertz, with a minimum of 20 Hz
372 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
373 * Some update frequencies are not supported and will be
374 * promoted up to the next highest supported frequency.
375 *
376 * If this field is set to 0 Hz, the control request will
377 * be sent immediately as a one-shot frame. This may be useful
378 * for advanced applications that require outputs to be
379 * synchronized with data acquisition. In this case, we
380 * recommend not exceeding 50 ms between control calls.
381 *
382 * \param newUpdateFreqHz Parameter to modify
383 * \returns Itself
384 */
385 constexpr MotionMagicDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
386 {
387 UpdateFreqHz = newUpdateFreqHz;
388 return *this;
389 }
390
391 /**
392 * \brief Returns a string representation of the object.
393 *
394 * \returns a string representation of the object.
395 */
396 std::string ToString() const override;
397
398 /**
399 * \brief Gets information about this control request.
400 *
401 * \returns Map of control parameter names and corresponding applied values
402 */
403 std::map<std::string, std::string> GetControlInfo() const override;
404};
405
406}
407}
408}
409
Common interface implemented by all control requests.
Definition ControlRequest.hpp:27
Requests Motion Magic® to target a final position using a motion profile.
Definition MotionMagicDutyCycle.hpp:31
int Slot
Select which gains are applied by selecting the slot.
Definition MotionMagicDutyCycle.hpp:69
constexpr MotionMagicDutyCycle & WithFeedForward(units::dimensionless::scalar_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition MotionMagicDutyCycle.hpp:228
constexpr MotionMagicDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition MotionMagicDutyCycle.hpp:297
constexpr MotionMagicDutyCycle & WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicDutyCycle.hpp:341
std::string ToString() const override
Returns a string representation of the object.
constexpr MotionMagicDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the frequency at which this control will update.
Definition MotionMagicDutyCycle.hpp:385
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition MotionMagicDutyCycle.hpp:76
constexpr MotionMagicDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition MotionMagicDutyCycle.hpp:209
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
constexpr MotionMagicDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition MotionMagicDutyCycle.hpp:263
constexpr ~MotionMagicDutyCycle() override
Definition MotionMagicDutyCycle.hpp:160
constexpr MotionMagicDutyCycle & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition MotionMagicDutyCycle.hpp:363
constexpr MotionMagicDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition MotionMagicDutyCycle.hpp:280
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition MotionMagicDutyCycle.hpp:101
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition MotionMagicDutyCycle.hpp:63
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition MotionMagicDutyCycle.hpp:122
units::angle::turn_t Position
Position to drive toward in rotations.
Definition MotionMagicDutyCycle.hpp:41
constexpr MotionMagicDutyCycle(units::angle::turn_t Position)
Requests Motion Magic® to target a final position using a motion profile.
Definition MotionMagicDutyCycle.hpp:156
bool LimitForwardMotion
Set to true to force forward limiting.
Definition MotionMagicDutyCycle.hpp:82
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15% on supp...
Definition MotionMagicDutyCycle.hpp:55
constexpr MotionMagicDutyCycle & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicDutyCycle.hpp:320
bool IgnoreSoftwareLimits
Set to true to ignore software limits, instead allowing motion.
Definition MotionMagicDutyCycle.hpp:111
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition MotionMagicDutyCycle.hpp:88
units::frequency::hertz_t UpdateFreqHz
The frequency at which this control will update.
Definition MotionMagicDutyCycle.hpp:137
constexpr MotionMagicDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition MotionMagicDutyCycle.hpp:245
constexpr MotionMagicDutyCycle & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition MotionMagicDutyCycle.hpp:184
constexpr std::string_view GetName() const override
Gets the name of this control request.
Definition MotionMagicDutyCycle.hpp:167
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14