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