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