CTRE Phoenix 6 C++ 24.2.0
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 Jerk value. This control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
31 * configs. If the specified acceleration is zero, the Acceleration under Motion Magic® configuration
32 * parameter is used instead. This allows for runtime adjustment of acceleration for advanced users. Jerk is
33 * also specified in the Motion Magic® persistent configuration values. If Jerk is set to zero, Motion Magic®
34 * will produce a trapezoidal acceleration profile. Target velocity can also be changed on-the-fly and Motion
35 * Magic® will do its best to adjust the profile. This control mode is duty cycle based, so relevant
36 * closed-loop gains will use fractional duty cycle for the numerator: +1.0 represents full forward output.
37 */
39{
40 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
41 {
42 if (req.get() != this)
43 {
44 auto const reqCast = dynamic_cast<MotionMagicVelocityDutyCycle *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<MotionMagicVelocityDutyCycle>(*this);
52 }
53 }
54
55 return c_ctre_phoenix6_RequestControlMotionMagicVelocityDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Velocity.to<double>(), Acceleration.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
56 }
57
58public:
59 /**
60 * Target velocity to drive toward in rotations per second. This can be changed
61 * on-the fly.
62 */
63 units::angular_velocity::turns_per_second_t Velocity;
64 /**
65 * This is the absolute Acceleration to use generating the profile. If this
66 * parameter is zero, the Acceleration persistent configuration parameter is
67 * used instead. Acceleration is in rotations per second squared. If nonzero,
68 * the signage does not matter as the absolute value is used.
69 */
70 units::angular_acceleration::turns_per_second_squared_t Acceleration;
71 /**
72 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
73 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
74 * improves motor performance by leveraging torque (current) control. However,
75 * this may be inconvenient for applications that require specifying duty cycle
76 * or voltage. CTR-Electronics has developed a hybrid method that combines the
77 * performances gains of FOC while still allowing applications to provide duty
78 * cycle or voltage demand. This not to be confused with simple sinusoidal
79 * control or phase voltage control which lacks the performance gains.
80 */
82 /**
83 * Feedforward to apply in fractional units between -1 and +1.
84 */
85 units::dimensionless::scalar_t FeedForward;
86 /**
87 * Select which gains are applied by selecting the slot. Use the configuration
88 * api to set the gain values for the selected slot before enabling this
89 * feature. Slot must be within [0,2].
90 */
91 int Slot;
92 /**
93 * Set to true to static-brake the rotor when output is zero (or within
94 * deadband). Set to false to use the NeutralMode configuration setting
95 * (default). This flag exists to provide the fundamental behavior of this
96 * control when output is zero, which is to provide 0V to the motor.
97 */
99 /**
100 * Set to true to force forward limiting. This allows users to use other limit
101 * switch sensors connected to robot controller. This also allows use of active
102 * sensors that require external power.
103 */
105 /**
106 * Set to true to force reverse limiting. This allows users to use other limit
107 * switch sensors connected to robot controller. This also allows use of active
108 * sensors that require external power.
109 */
111
112 /**
113 * \brief The period at which this control will update at.
114 * This is designated in Hertz, with a minimum of 20 Hz
115 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
116 *
117 * If this field is set to 0 Hz, the control request will
118 * be sent immediately as a one-shot frame. This may be useful
119 * for advanced applications that require outputs to be
120 * synchronized with data acquisition. In this case, we
121 * recommend not exceeding 50 ms between control calls.
122 */
123 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
124
125 /**
126 * \brief Requests Motion Magic® to target a final velocity using a motion
127 * profile. This allows smooth transitions between velocity set points.
128 * Users can optionally provide a duty cycle feedforward.
129 *
130 * \details Motion Magic® Velocity produces a motion profile in real-time while
131 * attempting to honor the specified Acceleration and Jerk value. This
132 * control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
133 * configs. If the specified acceleration is zero, the Acceleration
134 * under Motion Magic® configuration parameter is used instead. This
135 * allows for runtime adjustment of acceleration for advanced users.
136 * Jerk is also specified in the Motion Magic® persistent configuration
137 * values. If Jerk is set to zero, Motion Magic® will produce a
138 * trapezoidal acceleration profile. Target velocity can also be
139 * changed on-the-fly and Motion Magic® will do its best to adjust the
140 * profile. This control mode is duty cycle based, so relevant
141 * closed-loop gains will use fractional duty cycle for the numerator:
142 * +1.0 represents full forward output.
143 *
144 * \param Velocity Target velocity to drive toward in rotations per second.
145 * This can be changed on-the fly.
146 * \param Acceleration This is the absolute Acceleration to use generating
147 * the profile. If this parameter is zero, the
148 * Acceleration persistent configuration parameter is
149 * used instead. Acceleration is in rotations per second
150 * squared. If nonzero, the signage does not matter as
151 * the absolute value is used.
152 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
153 * Pro), which increases peak power by ~15%. Set to false to
154 * use trapezoidal commutation. FOC improves motor
155 * performance by leveraging torque (current) control.
156 * However, this may be inconvenient for applications that
157 * require specifying duty cycle or voltage.
158 * CTR-Electronics has developed a hybrid method that
159 * combines the performances gains of FOC while still
160 * allowing applications to provide duty cycle or voltage
161 * demand. This not to be confused with simple sinusoidal
162 * control or phase voltage control which lacks the
163 * performance gains.
164 * \param FeedForward Feedforward to apply in fractional units between -1 and
165 * +1.
166 * \param Slot Select which gains are applied by selecting the slot. Use the
167 * configuration api to set the gain values for the selected slot
168 * before enabling this feature. Slot must be within [0,2].
169 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
170 * output is zero (or within deadband). Set
171 * to false to use the NeutralMode
172 * configuration setting (default). This flag
173 * exists to provide the fundamental behavior
174 * of this control when output is zero, which
175 * is to provide 0V to the motor.
176 * \param LimitForwardMotion Set to true to force forward limiting. This
177 * allows users to use other limit switch sensors
178 * connected to robot controller. This also allows
179 * use of active sensors that require external
180 * power.
181 * \param LimitReverseMotion Set to true to force reverse limiting. This
182 * allows users to use other limit switch sensors
183 * connected to robot controller. This also allows
184 * use of active sensors that require external
185 * power.
186 */
187 MotionMagicVelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq, bool EnableFOC = true, units::dimensionless::scalar_t FeedForward = 0.0, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicVelocityDutyCycle"},
188 Velocity{std::move(Velocity)},
189 Acceleration{std::move(Acceleration)},
190 EnableFOC{std::move(EnableFOC)},
191 FeedForward{std::move(FeedForward)},
192 Slot{std::move(Slot)},
196 {}
197
198 /**
199 * \brief Modifies this Control Request's Velocity parameter and returns itself for
200 * method-chaining and easier to use request API.
201 * \param newVelocity Parameter to modify
202 * \returns Itself
203 */
204 MotionMagicVelocityDutyCycle& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
205 {
206 Velocity = std::move(newVelocity);
207 return *this;
208 }
209
210 /**
211 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
212 * method-chaining and easier to use request API.
213 * \param newAcceleration Parameter to modify
214 * \returns Itself
215 */
216 MotionMagicVelocityDutyCycle& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
217 {
218 Acceleration = std::move(newAcceleration);
219 return *this;
220 }
221
222 /**
223 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
224 * method-chaining and easier to use request API.
225 * \param newEnableFOC Parameter to modify
226 * \returns Itself
227 */
229 {
230 EnableFOC = std::move(newEnableFOC);
231 return *this;
232 }
233
234 /**
235 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
236 * method-chaining and easier to use request API.
237 * \param newFeedForward Parameter to modify
238 * \returns Itself
239 */
240 MotionMagicVelocityDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
241 {
242 FeedForward = std::move(newFeedForward);
243 return *this;
244 }
245
246 /**
247 * \brief Modifies this Control Request's Slot parameter and returns itself for
248 * method-chaining and easier to use request API.
249 * \param newSlot Parameter to modify
250 * \returns Itself
251 */
253 {
254 Slot = std::move(newSlot);
255 return *this;
256 }
257
258 /**
259 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
260 * method-chaining and easier to use request API.
261 * \param newOverrideBrakeDurNeutral Parameter to modify
262 * \returns Itself
263 */
265 {
266 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
267 return *this;
268 }
269
270 /**
271 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
272 * method-chaining and easier to use request API.
273 * \param newLimitForwardMotion Parameter to modify
274 * \returns Itself
275 */
277 {
278 LimitForwardMotion = std::move(newLimitForwardMotion);
279 return *this;
280 }
281
282 /**
283 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
284 * method-chaining and easier to use request API.
285 * \param newLimitReverseMotion Parameter to modify
286 * \returns Itself
287 */
289 {
290 LimitReverseMotion = std::move(newLimitReverseMotion);
291 return *this;
292 }
293 /**
294 * \brief Sets the period at which this control will update at.
295 * This is designated in Hertz, with a minimum of 20 Hz
296 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
297 *
298 * If this field is set to 0 Hz, the control request will
299 * be sent immediately as a one-shot frame. This may be useful
300 * for advanced applications that require outputs to be
301 * synchronized with data acquisition. In this case, we
302 * recommend not exceeding 50 ms between control calls.
303 *
304 * \param newUpdateFreqHz Parameter to modify
305 * \returns Itself
306 */
307 MotionMagicVelocityDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
308 {
309 UpdateFreqHz = newUpdateFreqHz;
310 return *this;
311 }
312 /**
313 * Returns a string representation of the object.
314 *
315 * \returns a string representation of the object.
316 */
317 std::string ToString() const override
318 {
319 std::stringstream ss;
320 ss << "class: MotionMagicVelocityDutyCycle" << std::endl;
321 ss << "Velocity: " << Velocity.to<double>() << std::endl;
322 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
323 ss << "EnableFOC: " << EnableFOC << std::endl;
324 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
325 ss << "Slot: " << Slot << std::endl;
326 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
327 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
328 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
329 return ss.str();
330 }
331
332 /**
333 * \brief Gets information about this control request.
334 *
335 * \returns Map of control parameter names and corresponding applied values
336 */
337 std::map<std::string, std::string> GetControlInfo() const override
338 {
339 std::map<std::string, std::string> controlInfo;
340 std::stringstream ss;
341 controlInfo["Name"] = GetName();
342 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
343 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
344 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
345 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
346 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
347 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
348 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
349 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
350 return controlInfo;
351 }
352};
353
354}
355}
356}
357
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicVelocityDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Velocity, double Acceleration, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityDutyCycle.hpp:39
MotionMagicVelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration=0.0_tr_per_s_sq, bool EnableFOC=true, units::dimensionless::scalar_t FeedForward=0.0, int Slot=0, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityDutyCycle.hpp:187
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: MotionMagicVelocityDutyCycle.hpp:85
MotionMagicVelocityDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityDutyCycle.hpp:276
MotionMagicVelocityDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicVelocityDutyCycle.hpp:264
MotionMagicVelocityDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicVelocityDutyCycle.hpp:307
MotionMagicVelocityDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicVelocityDutyCycle.hpp:228
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:216
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:240
units::angular_velocity::turns_per_second_t Velocity
Target velocity to drive toward in rotations per second.
Definition: MotionMagicVelocityDutyCycle.hpp:63
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicVelocityDutyCycle.hpp:110
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicVelocityDutyCycle.hpp:317
MotionMagicVelocityDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicVelocityDutyCycle.hpp:252
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicVelocityDutyCycle.hpp:91
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicVelocityDutyCycle.hpp:123
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: MotionMagicVelocityDutyCycle.hpp:81
units::angular_acceleration::turns_per_second_squared_t Acceleration
This is the absolute Acceleration to use generating the profile.
Definition: MotionMagicVelocityDutyCycle.hpp:70
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: MotionMagicVelocityDutyCycle.hpp:98
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicVelocityDutyCycle.hpp:337
MotionMagicVelocityDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityDutyCycle.hpp:288
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicVelocityDutyCycle.hpp:104
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:204
Definition: RcManualEvent.hpp:12