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