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