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