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