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