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