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