CTRE Phoenix 6 C++ 24.2.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
125 * final position using a motion profile. This dynamic request allows
126 * runtime changes to Cruise Velocity, Acceleration, and Jerk. Users can
127 * optionally provide a torque current feedforward. This control
128 * requires use of a CANivore.
129 *
130 * \details Motion Magic® produces a motion profile in real-time while
131 * attempting to honor the specified Cruise Velocity, Acceleration, and
132 * Jerk value. This control mode does not use the Expo_kV or Expo_kA
133 * configs. Target position can be changed on-the-fly and Motion
134 * Magic® will do its best to adjust the profile. This control mode is
135 * based on torque current, so relevant closed-loop gains will use
136 * Amperes for the numerator.
137 *
138 * \param Position Position to drive toward in rotations.
139 * \param Velocity Cruise velocity for profiling. The signage does not
140 * matter as the device will use the absolute value for
141 * profile generation.
142 * \param Acceleration Acceleration for profiling. The signage does not
143 * matter as the device will use the absolute value for
144 * profile generation.
145 * \param Jerk Jerk for profiling. The signage does not matter as the device
146 * will use the absolute value for profile generation.
147 * \param FeedForward Feedforward to apply in torque current in Amperes.
148 * User can use motor's kT to scale Newton-meter to
149 * Amperes.
150 * \param Slot Select which gains are applied by selecting the slot. Use the
151 * configuration api to set the gain values for the selected slot
152 * before enabling this feature. Slot must be within [0,2].
153 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
154 * is zero (or within deadband). Set to false
155 * to use the NeutralMode configuration
156 * setting (default). This flag exists to
157 * provide the fundamental behavior of this
158 * control when output is zero, which is to
159 * provide 0A (zero torque).
160 * \param LimitForwardMotion Set to true to force forward limiting. This
161 * allows users to use other limit switch sensors
162 * connected to robot controller. This also allows
163 * use of active sensors that require external
164 * power.
165 * \param LimitReverseMotion Set to true to force reverse limiting. This
166 * allows users to use other limit switch sensors
167 * connected to robot controller. This also allows
168 * use of active sensors that require external
169 * power.
170 */
171 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"},
172 Position{std::move(Position)},
173 Velocity{std::move(Velocity)},
174 Acceleration{std::move(Acceleration)},
175 Jerk{std::move(Jerk)},
176 FeedForward{std::move(FeedForward)},
177 Slot{std::move(Slot)},
181 {}
182
183 /**
184 * \brief Modifies this Control Request's Position parameter and returns itself for
185 * method-chaining and easier to use request API.
186 * \param newPosition Parameter to modify
187 * \returns Itself
188 */
189 DynamicMotionMagicTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
190 {
191 Position = std::move(newPosition);
192 return *this;
193 }
194
195 /**
196 * \brief Modifies this Control Request's Velocity parameter and returns itself for
197 * method-chaining and easier to use request API.
198 * \param newVelocity Parameter to modify
199 * \returns Itself
200 */
201 DynamicMotionMagicTorqueCurrentFOC& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
202 {
203 Velocity = std::move(newVelocity);
204 return *this;
205 }
206
207 /**
208 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
209 * method-chaining and easier to use request API.
210 * \param newAcceleration Parameter to modify
211 * \returns Itself
212 */
213 DynamicMotionMagicTorqueCurrentFOC& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
214 {
215 Acceleration = std::move(newAcceleration);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's Jerk parameter and returns itself for
221 * method-chaining and easier to use request API.
222 * \param newJerk Parameter to modify
223 * \returns Itself
224 */
225 DynamicMotionMagicTorqueCurrentFOC& WithJerk(units::angular_jerk::turns_per_second_cubed_t newJerk)
226 {
227 Jerk = std::move(newJerk);
228 return *this;
229 }
230
231 /**
232 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
233 * method-chaining and easier to use request API.
234 * \param newFeedForward Parameter to modify
235 * \returns Itself
236 */
237 DynamicMotionMagicTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
238 {
239 FeedForward = std::move(newFeedForward);
240 return *this;
241 }
242
243 /**
244 * \brief Modifies this Control Request's Slot parameter and returns itself for
245 * method-chaining and easier to use request API.
246 * \param newSlot Parameter to modify
247 * \returns Itself
248 */
250 {
251 Slot = std::move(newSlot);
252 return *this;
253 }
254
255 /**
256 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
257 * method-chaining and easier to use request API.
258 * \param newOverrideCoastDurNeutral Parameter to modify
259 * \returns Itself
260 */
262 {
263 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
264 return *this;
265 }
266
267 /**
268 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
269 * method-chaining and easier to use request API.
270 * \param newLimitForwardMotion Parameter to modify
271 * \returns Itself
272 */
274 {
275 LimitForwardMotion = std::move(newLimitForwardMotion);
276 return *this;
277 }
278
279 /**
280 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
281 * method-chaining and easier to use request API.
282 * \param newLimitReverseMotion Parameter to modify
283 * \returns Itself
284 */
286 {
287 LimitReverseMotion = std::move(newLimitReverseMotion);
288 return *this;
289 }
290 /**
291 * \brief Sets the period at which this control will update at.
292 * This is designated in Hertz, with a minimum of 20 Hz
293 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
294 *
295 * If this field is set to 0 Hz, the control request will
296 * be sent immediately as a one-shot frame. This may be useful
297 * for advanced applications that require outputs to be
298 * synchronized with data acquisition. In this case, we
299 * recommend not exceeding 50 ms between control calls.
300 *
301 * \param newUpdateFreqHz Parameter to modify
302 * \returns Itself
303 */
304 DynamicMotionMagicTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
305 {
306 UpdateFreqHz = newUpdateFreqHz;
307 return *this;
308 }
309 /**
310 * Returns a string representation of the object.
311 *
312 * \returns a string representation of the object.
313 */
314 std::string ToString() const override
315 {
316 std::stringstream ss;
317 ss << "class: DynamicMotionMagicTorqueCurrentFOC" << std::endl;
318 ss << "Position: " << Position.to<double>() << std::endl;
319 ss << "Velocity: " << Velocity.to<double>() << std::endl;
320 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
321 ss << "Jerk: " << Jerk.to<double>() << std::endl;
322 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
323 ss << "Slot: " << Slot << std::endl;
324 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
325 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
326 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
327 return ss.str();
328 }
329
330 /**
331 * \brief Gets information about this control request.
332 *
333 * \returns Map of control parameter names and corresponding applied values
334 */
335 std::map<std::string, std::string> GetControlInfo() const override
336 {
337 std::map<std::string, std::string> controlInfo;
338 std::stringstream ss;
339 controlInfo["Name"] = GetName();
340 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
341 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
342 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
343 ss << Jerk.to<double>(); controlInfo["Jerk"] = ss.str(); ss.str(std::string{});
344 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
345 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
346 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
347 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
348 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
349 return controlInfo;
350 }
351};
352
353}
354}
355}
356
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:189
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:171
DynamicMotionMagicTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: DynamicMotionMagicTorqueCurrentFOC.hpp:249
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:261
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:201
std::string ToString() const override
Returns a string representation of the object.
Definition: DynamicMotionMagicTorqueCurrentFOC.hpp:314
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:213
DynamicMotionMagicTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DynamicMotionMagicTorqueCurrentFOC.hpp:273
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:237
DynamicMotionMagicTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DynamicMotionMagicTorqueCurrentFOC.hpp:304
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:285
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:225
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:335
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration for profiling.
Definition: DynamicMotionMagicTorqueCurrentFOC.hpp:72
Definition: RcManualEvent.hpp:12