CTRE Phoenix 6 C++ 24.2.0
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
12#include <sstream>
13#include <units/angular_velocity.h>
14#include <units/angular_acceleration.h>
15#include <units/current.h>
16#include <units/frequency.h>
17#include <units/time.h>
18
19
20namespace ctre {
21namespace phoenix6 {
22namespace controls {
23
24/**
25 * Requests Motion Magic® to target a final velocity using a motion profile.
26 * This allows smooth transitions between velocity set points. Users can
27 * optionally provide a torque feedforward.
28 *
29 * Motion Magic® Velocity produces a motion profile in real-time while attempting to honor the specified
30 * Acceleration and Jerk value. This control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
31 * configs. If the specified acceleration is zero, the Acceleration under Motion Magic® configuration
32 * parameter is used instead. This allows for runtime adjustment of acceleration for advanced users. Jerk is
33 * also specified in the Motion Magic® persistent configuration values. If Jerk is set to zero, Motion Magic®
34 * will produce a trapezoidal acceleration profile. Target velocity can also be changed on-the-fly and Motion
35 * Magic® will do its best to adjust the profile. This control mode is based on torque current, so relevant
36 * closed-loop gains will use Amperes for the numerator.
37 */
39{
40 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
41 {
42 if (req.get() != this)
43 {
44 auto const reqCast = dynamic_cast<MotionMagicVelocityTorqueCurrentFOC *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<MotionMagicVelocityTorqueCurrentFOC>(*this);
52 }
53 }
54
55 return c_ctre_phoenix6_RequestControlMotionMagicVelocityTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Velocity.to<double>(), Acceleration.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideCoastDurNeutral, LimitForwardMotion, LimitReverseMotion);
56 }
57
58public:
59 /**
60 * Target velocity to drive toward in rotations per second. This can be changed
61 * on-the fly.
62 */
63 units::angular_velocity::turns_per_second_t Velocity;
64 /**
65 * This is the absolute Acceleration to use generating the profile. If this
66 * parameter is zero, the Acceleration persistent configuration parameter is
67 * used instead. Acceleration is in rotations per second squared. If nonzero,
68 * the signage does not matter as the absolute value is used.
69 */
70 units::angular_acceleration::turns_per_second_squared_t Acceleration;
71 /**
72 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
73 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
74 * improves motor performance by leveraging torque (current) control. However,
75 * this may be inconvenient for applications that require specifying duty cycle
76 * or voltage. CTR-Electronics has developed a hybrid method that combines the
77 * performances gains of FOC while still allowing applications to provide duty
78 * cycle or voltage demand. This not to be confused with simple sinusoidal
79 * control or phase voltage control which lacks the performance gains.
80 */
82 /**
83 * Feedforward to apply in torque current in Amperes. User can use motor's kT
84 * to scale Newton-meter to Amperes.
85 */
86 units::current::ampere_t FeedForward;
87 /**
88 * Select which gains are applied by selecting the slot. Use the configuration
89 * api to set the gain values for the selected slot before enabling this
90 * feature. Slot must be within [0,2].
91 */
92 int Slot;
93 /**
94 * Set to true to coast the rotor when output is zero (or within deadband). Set
95 * to false to use the NeutralMode configuration setting (default). This flag
96 * exists to provide the fundamental behavior of this control when output is
97 * zero, which is to provide 0A (zero torque).
98 */
100 /**
101 * Set to true to force forward limiting. This allows users to use other limit
102 * switch sensors connected to robot controller. This also allows use of active
103 * sensors that require external power.
104 */
106 /**
107 * Set to true to force reverse limiting. This allows users to use other limit
108 * switch sensors connected to robot controller. This also allows use of active
109 * sensors that require external power.
110 */
112
113 /**
114 * \brief The period at which this control will update at.
115 * This is designated in Hertz, with a minimum of 20 Hz
116 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
117 *
118 * If this field is set to 0 Hz, the control request will
119 * be sent immediately as a one-shot frame. This may be useful
120 * for advanced applications that require outputs to be
121 * synchronized with data acquisition. In this case, we
122 * recommend not exceeding 50 ms between control calls.
123 */
124 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
125
126 /**
127 * \brief Requests Motion Magic® to target a final velocity using a motion
128 * profile. This allows smooth transitions between velocity set points.
129 * Users can optionally provide a torque feedforward.
130 *
131 * \details Motion Magic® Velocity produces a motion profile in real-time while
132 * attempting to honor the specified Acceleration and Jerk value. This
133 * control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA
134 * configs. If the specified acceleration is zero, the Acceleration
135 * under Motion Magic® configuration parameter is used instead. This
136 * allows for runtime adjustment of acceleration for advanced users.
137 * Jerk is also specified in the Motion Magic® persistent configuration
138 * values. If Jerk is set to zero, Motion Magic® will produce a
139 * trapezoidal acceleration profile. Target velocity can also be
140 * changed on-the-fly and Motion Magic® will do its best to adjust the
141 * profile. This control mode is based on torque current, so relevant
142 * closed-loop gains will use Amperes for the numerator.
143 *
144 * \param Velocity Target velocity to drive toward in rotations per second.
145 * This can be changed on-the fly.
146 * \param Acceleration This is the absolute Acceleration to use generating
147 * the profile. If this parameter is zero, the
148 * Acceleration persistent configuration parameter is
149 * used instead. Acceleration is in rotations per second
150 * squared. If nonzero, the signage does not matter as
151 * the absolute value is used.
152 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
153 * Pro), which increases peak power by ~15%. Set to false to
154 * use trapezoidal commutation. FOC improves motor
155 * performance by leveraging torque (current) control.
156 * However, this may be inconvenient for applications that
157 * require specifying duty cycle or voltage.
158 * CTR-Electronics has developed a hybrid method that
159 * combines the performances gains of FOC while still
160 * allowing applications to provide duty cycle or voltage
161 * demand. This not to be confused with simple sinusoidal
162 * control or phase voltage control which lacks the
163 * performance gains.
164 * \param FeedForward Feedforward to apply in torque current in Amperes.
165 * User can use motor's kT to scale Newton-meter to
166 * Amperes.
167 * \param Slot Select which gains are applied by selecting the slot. Use the
168 * configuration api to set the gain values for the selected slot
169 * before enabling this feature. Slot must be within [0,2].
170 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
171 * is zero (or within deadband). Set to false
172 * to use the NeutralMode configuration
173 * setting (default). This flag exists to
174 * provide the fundamental behavior of this
175 * control when output is zero, which is to
176 * provide 0A (zero torque).
177 * \param LimitForwardMotion Set to true to force forward limiting. This
178 * allows users to use other limit switch sensors
179 * connected to robot controller. This also allows
180 * use of active sensors that require external
181 * power.
182 * \param LimitReverseMotion Set to true to force reverse limiting. This
183 * allows users to use other limit switch sensors
184 * connected to robot controller. This also allows
185 * use of active sensors that require external
186 * power.
187 */
188 MotionMagicVelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq, bool EnableFOC = true, units::current::ampere_t FeedForward = 0.0_A, int Slot = 0, bool OverrideCoastDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicVelocityTorqueCurrentFOC"},
189 Velocity{std::move(Velocity)},
190 Acceleration{std::move(Acceleration)},
191 EnableFOC{std::move(EnableFOC)},
192 FeedForward{std::move(FeedForward)},
193 Slot{std::move(Slot)},
197 {}
198
199 /**
200 * \brief Modifies this Control Request's Velocity parameter and returns itself for
201 * method-chaining and easier to use request API.
202 * \param newVelocity Parameter to modify
203 * \returns Itself
204 */
205 MotionMagicVelocityTorqueCurrentFOC& 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 * \param newAcceleration Parameter to modify
215 * \returns Itself
216 */
217 MotionMagicVelocityTorqueCurrentFOC& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
218 {
219 Acceleration = std::move(newAcceleration);
220 return *this;
221 }
222
223 /**
224 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
225 * method-chaining and easier to use request API.
226 * \param newEnableFOC Parameter to modify
227 * \returns Itself
228 */
230 {
231 EnableFOC = std::move(newEnableFOC);
232 return *this;
233 }
234
235 /**
236 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
237 * method-chaining and easier to use request API.
238 * \param newFeedForward Parameter to modify
239 * \returns Itself
240 */
241 MotionMagicVelocityTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
242 {
243 FeedForward = std::move(newFeedForward);
244 return *this;
245 }
246
247 /**
248 * \brief Modifies this Control Request's Slot parameter and returns itself for
249 * method-chaining and easier to use request API.
250 * \param newSlot Parameter to modify
251 * \returns Itself
252 */
254 {
255 Slot = std::move(newSlot);
256 return *this;
257 }
258
259 /**
260 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
261 * method-chaining and easier to use request API.
262 * \param newOverrideCoastDurNeutral Parameter to modify
263 * \returns Itself
264 */
266 {
267 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
268 return *this;
269 }
270
271 /**
272 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
273 * method-chaining and easier to use request API.
274 * \param newLimitForwardMotion Parameter to modify
275 * \returns Itself
276 */
278 {
279 LimitForwardMotion = std::move(newLimitForwardMotion);
280 return *this;
281 }
282
283 /**
284 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
285 * method-chaining and easier to use request API.
286 * \param newLimitReverseMotion Parameter to modify
287 * \returns Itself
288 */
290 {
291 LimitReverseMotion = std::move(newLimitReverseMotion);
292 return *this;
293 }
294 /**
295 * \brief Sets the period at which this control will update at.
296 * This is designated in Hertz, with a minimum of 20 Hz
297 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
298 *
299 * If this field is set to 0 Hz, the control request will
300 * be sent immediately as a one-shot frame. This may be useful
301 * for advanced applications that require outputs to be
302 * synchronized with data acquisition. In this case, we
303 * recommend not exceeding 50 ms between control calls.
304 *
305 * \param newUpdateFreqHz Parameter to modify
306 * \returns Itself
307 */
308 MotionMagicVelocityTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
309 {
310 UpdateFreqHz = newUpdateFreqHz;
311 return *this;
312 }
313 /**
314 * Returns a string representation of the object.
315 *
316 * \returns a string representation of the object.
317 */
318 std::string ToString() const override
319 {
320 std::stringstream ss;
321 ss << "class: MotionMagicVelocityTorqueCurrentFOC" << std::endl;
322 ss << "Velocity: " << Velocity.to<double>() << std::endl;
323 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
324 ss << "EnableFOC: " << EnableFOC << std::endl;
325 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
326 ss << "Slot: " << Slot << std::endl;
327 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
328 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
329 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
330 return ss.str();
331 }
332
333 /**
334 * \brief Gets information about this control request.
335 *
336 * \returns Map of control parameter names and corresponding applied values
337 */
338 std::map<std::string, std::string> GetControlInfo() const override
339 {
340 std::map<std::string, std::string> controlInfo;
341 std::stringstream ss;
342 controlInfo["Name"] = GetName();
343 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
344 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
345 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
346 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
347 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
348 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
349 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
350 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
351 return controlInfo;
352 }
353};
354
355}
356}
357}
358
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicVelocityTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Velocity, double Acceleration, bool EnableFOC, double FeedForward, int Slot, bool OverrideCoastDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:39
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:241
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:92
MotionMagicVelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration=0.0_tr_per_s_sq, bool EnableFOC=true, units::current::ampere_t FeedForward=0.0_A, int Slot=0, bool OverrideCoastDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requests Motion Magic® to target a final velocity using a motion profile.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:188
MotionMagicVelocityTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:265
MotionMagicVelocityTorqueCurrentFOC & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:229
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:86
MotionMagicVelocityTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:308
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:205
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:338
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:105
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:111
MotionMagicVelocityTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:277
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:124
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:99
MotionMagicVelocityTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:253
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:318
MotionMagicVelocityTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:289
units::angular_acceleration::turns_per_second_squared_t Acceleration
This is the absolute Acceleration to use generating the profile.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:70
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:217
units::angular_velocity::turns_per_second_t Velocity
Target velocity to drive toward in rotations per second.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:63
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: MotionMagicVelocityTorqueCurrentFOC.hpp:81
Definition: RcManualEvent.hpp:12