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