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