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