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