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