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