CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
MotionMagicTorqueCurrentFOC.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
10#include <units/frequency.h>
11#include <units/time.h>
12#include <units/angle.h>
13#include <units/current.h>
14
15namespace ctre {
16namespace phoenix6 {
17namespace controls {
18
19/**
20 * Requires Phoenix Pro;
21 * Requests Motion Magic® to target a final position using a motion profile.
22 * Users can optionally provide a torque current feedforward.
23 *
24 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
25 * Acceleration, and (optional) Jerk specified via the Motion Magic® configuration values. This control mode
26 * does not use the Expo_kV or Expo_kA configs.
27 *
28 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile. This
29 * control mode is based on torque current, so relevant closed-loop gains will use Amperes for the numerator.
30 */
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override;
33
34public:
35 /**
36 * \brief Position to drive toward in rotations.
37 *
38 * - Units: rotations
39 *
40 */
41 units::angle::turn_t Position;
42 /**
43 * \brief Feedforward to apply in torque current in Amperes. This is added to
44 * the output of the onboard feedforward terms.
45 *
46 * User can use motor's kT to scale Newton-meter to Amperes.
47 *
48 * - Units: A
49 *
50 */
51 units::current::ampere_t FeedForward = 0.0_A;
52 /**
53 * \brief Select which gains are applied by selecting the slot. Use the
54 * configuration api to set the gain values for the selected slot before
55 * enabling this feature. Slot must be within [0,2].
56 */
57 int Slot = 0;
58 /**
59 * \brief Set to true to coast the rotor when output is zero (or within
60 * deadband). Set to false to use the NeutralMode configuration setting
61 * (default). This flag exists to provide the fundamental behavior of this
62 * control when output is zero, which is to provide 0A (zero torque).
63 */
65 /**
66 * \brief Set to true to force forward limiting. This allows users to use other
67 * limit switch sensors connected to robot controller. This also allows use of
68 * active sensors that require external power.
69 */
70 bool LimitForwardMotion = false;
71 /**
72 * \brief Set to true to force reverse limiting. This allows users to use other
73 * limit switch sensors connected to robot controller. This also allows use of
74 * active sensors that require external power.
75 */
76 bool LimitReverseMotion = false;
77 /**
78 * \brief Set to true to ignore hardware limit switches and the
79 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
80 * motion.
81 *
82 * This can be useful on mechanisms such as an intake/feeder, where a limit
83 * switch stops motion while intaking but should be ignored when feeding to a
84 * shooter.
85 *
86 * The hardware limit faults and Forward/ReverseLimit signals will still report
87 * the values of the limit switches regardless of this parameter.
88 */
90 /**
91 * \brief Set to true to ignore software limits, instead allowing motion.
92 *
93 * This can be useful when calibrating the zero point of a mechanism such as an
94 * elevator.
95 *
96 * The software limit faults will still report the values of the software limits
97 * regardless of this parameter.
98 */
100 /**
101 * \brief Set to true to delay applying this control request until a timesync
102 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
103 * nondeterministic network delays in exchange for a larger but deterministic
104 * control latency.
105 *
106 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
107 * Additionally, when this is enabled, the UpdateFreqHz of this request should
108 * be set to 0 Hz.
109 */
110 bool UseTimesync = false;
111
112 /**
113 * \brief The frequency at which this control will update.
114 * This is designated in Hertz, with a minimum of 20 Hz
115 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
116 * Some update frequencies are not supported and will be
117 * promoted up to the next highest supported frequency.
118 *
119 * If this field is set to 0 Hz, the control request will
120 * be sent immediately as a one-shot frame. This may be useful
121 * for advanced applications that require outputs to be
122 * synchronized with data acquisition. In this case, we
123 * recommend not exceeding 50 ms between control calls.
124 */
125 units::frequency::hertz_t UpdateFreqHz{100_Hz};
126
127 /**
128 * \brief Requires Phoenix Pro;
129 * Requests Motion Magic® to target a final position using a motion
130 * profile. Users can optionally provide a torque current feedforward.
131 *
132 * \details Motion Magic® produces a motion profile in real-time while
133 * attempting to honor the Cruise Velocity, Acceleration, and
134 * (optional) Jerk specified via the Motion Magic® configuration
135 * values. This control mode does not use the Expo_kV or Expo_kA
136 * configs.
137 *
138 * Target position can be changed on-the-fly and Motion Magic® will do
139 * its best to adjust the profile. This control mode is based on
140 * torque current, so relevant closed-loop gains will use Amperes for
141 * the numerator.
142 *
143 * \param Position Position to drive toward in rotations.
144 */
145 constexpr MotionMagicTorqueCurrentFOC(units::angle::turn_t Position) : ControlRequest{},
146 Position{std::move(Position)}
147 {}
148
149 constexpr ~MotionMagicTorqueCurrentFOC() override {}
150
151 /**
152 * \brief Gets the name of this control request.
153 *
154 * \returns Name of the control request
155 */
156 constexpr std::string_view GetName() const override
157 {
158 return "MotionMagicTorqueCurrentFOC";
159 }
160
161 /**
162 * \brief Modifies this Control Request's Position parameter and returns itself for
163 * method-chaining and easier to use request API.
164 *
165 * Position to drive toward in rotations.
166 *
167 * - Units: rotations
168 *
169 *
170 * \param newPosition Parameter to modify
171 * \returns Itself
172 */
173 constexpr MotionMagicTorqueCurrentFOC &WithPosition(units::angle::turn_t newPosition)
174 {
175 Position = std::move(newPosition);
176 return *this;
177 }
178
179 /**
180 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
181 * method-chaining and easier to use request API.
182 *
183 * Feedforward to apply in torque current in Amperes. This is added to the
184 * output of the onboard feedforward terms.
185 *
186 * User can use motor's kT to scale Newton-meter to Amperes.
187 *
188 * - Units: A
189 *
190 *
191 * \param newFeedForward Parameter to modify
192 * \returns Itself
193 */
194 constexpr MotionMagicTorqueCurrentFOC &WithFeedForward(units::current::ampere_t newFeedForward)
195 {
196 FeedForward = std::move(newFeedForward);
197 return *this;
198 }
199
200 /**
201 * \brief Modifies this Control Request's Slot parameter and returns itself for
202 * method-chaining and easier to use request API.
203 *
204 * Select which gains are applied by selecting the slot. Use the configuration
205 * api to set the gain values for the selected slot before enabling this
206 * feature. Slot must be within [0,2].
207 *
208 * \param newSlot Parameter to modify
209 * \returns Itself
210 */
211 constexpr MotionMagicTorqueCurrentFOC &WithSlot(int newSlot)
212 {
213 Slot = std::move(newSlot);
214 return *this;
215 }
216
217 /**
218 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
219 * method-chaining and easier to use request API.
220 *
221 * Set to true to coast the rotor when output is zero (or within deadband). Set
222 * to false to use the NeutralMode configuration setting (default). This flag
223 * exists to provide the fundamental behavior of this control when output is
224 * zero, which is to provide 0A (zero torque).
225 *
226 * \param newOverrideCoastDurNeutral Parameter to modify
227 * \returns Itself
228 */
229 constexpr MotionMagicTorqueCurrentFOC &WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
230 {
231 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
232 return *this;
233 }
234
235 /**
236 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
237 * method-chaining and easier to use request API.
238 *
239 * Set to true to force forward limiting. This allows users to use other limit
240 * switch sensors connected to robot controller. This also allows use of active
241 * sensors that require external power.
242 *
243 * \param newLimitForwardMotion Parameter to modify
244 * \returns Itself
245 */
246 constexpr MotionMagicTorqueCurrentFOC &WithLimitForwardMotion(bool newLimitForwardMotion)
247 {
248 LimitForwardMotion = std::move(newLimitForwardMotion);
249 return *this;
250 }
251
252 /**
253 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
254 * method-chaining and easier to use request API.
255 *
256 * Set to true to force reverse limiting. This allows users to use other limit
257 * switch sensors connected to robot controller. This also allows use of active
258 * sensors that require external power.
259 *
260 * \param newLimitReverseMotion Parameter to modify
261 * \returns Itself
262 */
263 constexpr MotionMagicTorqueCurrentFOC &WithLimitReverseMotion(bool newLimitReverseMotion)
264 {
265 LimitReverseMotion = std::move(newLimitReverseMotion);
266 return *this;
267 }
268
269 /**
270 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
271 * method-chaining and easier to use request API.
272 *
273 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
274 * LimitReverseMotion parameters, instead allowing motion.
275 *
276 * This can be useful on mechanisms such as an intake/feeder, where a limit
277 * switch stops motion while intaking but should be ignored when feeding to a
278 * shooter.
279 *
280 * The hardware limit faults and Forward/ReverseLimit signals will still report
281 * the values of the limit switches regardless of this parameter.
282 *
283 * \param newIgnoreHardwareLimits Parameter to modify
284 * \returns Itself
285 */
286 constexpr MotionMagicTorqueCurrentFOC &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
287 {
288 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
289 return *this;
290 }
291
292 /**
293 * \brief Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for
294 * method-chaining and easier to use request API.
295 *
296 * Set to true to ignore software limits, instead allowing motion.
297 *
298 * This can be useful when calibrating the zero point of a mechanism such as an
299 * elevator.
300 *
301 * The software limit faults will still report the values of the software limits
302 * regardless of this parameter.
303 *
304 * \param newIgnoreSoftwareLimits Parameter to modify
305 * \returns Itself
306 */
307 constexpr MotionMagicTorqueCurrentFOC &WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
308 {
309 IgnoreSoftwareLimits = std::move(newIgnoreSoftwareLimits);
310 return *this;
311 }
312
313 /**
314 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
315 * method-chaining and easier to use request API.
316 *
317 * Set to true to delay applying this control request until a timesync boundary
318 * (requires Phoenix Pro and CANivore). This eliminates the impact of
319 * nondeterministic network delays in exchange for a larger but deterministic
320 * control latency.
321 *
322 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
323 * Additionally, when this is enabled, the UpdateFreqHz of this request should
324 * be set to 0 Hz.
325 *
326 * \param newUseTimesync Parameter to modify
327 * \returns Itself
328 */
329 constexpr MotionMagicTorqueCurrentFOC &WithUseTimesync(bool newUseTimesync)
330 {
331 UseTimesync = std::move(newUseTimesync);
332 return *this;
333 }
334
335 /**
336 * \brief Sets the frequency at which this control will update.
337 * This is designated in Hertz, with a minimum of 20 Hz
338 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
339 * Some update frequencies are not supported and will be
340 * promoted up to the next highest supported frequency.
341 *
342 * If this field is set to 0 Hz, the control request will
343 * be sent immediately as a one-shot frame. This may be useful
344 * for advanced applications that require outputs to be
345 * synchronized with data acquisition. In this case, we
346 * recommend not exceeding 50 ms between control calls.
347 *
348 * \param newUpdateFreqHz Parameter to modify
349 * \returns Itself
350 */
351 constexpr MotionMagicTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
352 {
353 UpdateFreqHz = newUpdateFreqHz;
354 return *this;
355 }
356
357 /**
358 * \brief Returns a string representation of the object.
359 *
360 * \returns a string representation of the object.
361 */
362 std::string ToString() const override;
363
364 /**
365 * \brief Gets information about this control request.
366 *
367 * \returns Map of control parameter names and corresponding applied values
368 */
369 std::map<std::string, std::string> GetControlInfo() const override;
370};
371
372}
373}
374}
375
Common interface implemented by all control requests.
Definition ControlRequest.hpp:27
Requires Phoenix Pro; Requests Motion Magic® to target a final position using a motion profile.
Definition MotionMagicTorqueCurrentFOC.hpp:31
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition MotionMagicTorqueCurrentFOC.hpp:89
constexpr std::string_view GetName() const override
Gets the name of this control request.
Definition MotionMagicTorqueCurrentFOC.hpp:156
bool IgnoreSoftwareLimits
Set to true to ignore software limits, instead allowing motion.
Definition MotionMagicTorqueCurrentFOC.hpp:99
units::frequency::hertz_t UpdateFreqHz
The frequency at which this control will update.
Definition MotionMagicTorqueCurrentFOC.hpp:125
constexpr MotionMagicTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition MotionMagicTorqueCurrentFOC.hpp:194
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition MotionMagicTorqueCurrentFOC.hpp:51
units::angle::turn_t Position
Position to drive toward in rotations.
Definition MotionMagicTorqueCurrentFOC.hpp:41
constexpr MotionMagicTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition MotionMagicTorqueCurrentFOC.hpp:173
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition MotionMagicTorqueCurrentFOC.hpp:110
int Slot
Select which gains are applied by selecting the slot.
Definition MotionMagicTorqueCurrentFOC.hpp:57
constexpr MotionMagicTorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition MotionMagicTorqueCurrentFOC.hpp:329
constexpr MotionMagicTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition MotionMagicTorqueCurrentFOC.hpp:263
constexpr MotionMagicTorqueCurrentFOC & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicTorqueCurrentFOC.hpp:286
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition MotionMagicTorqueCurrentFOC.hpp:64
constexpr MotionMagicTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition MotionMagicTorqueCurrentFOC.hpp:211
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition MotionMagicTorqueCurrentFOC.hpp:76
std::string ToString() const override
Returns a string representation of the object.
bool LimitForwardMotion
Set to true to force forward limiting.
Definition MotionMagicTorqueCurrentFOC.hpp:70
constexpr MotionMagicTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the frequency at which this control will update.
Definition MotionMagicTorqueCurrentFOC.hpp:351
constexpr MotionMagicTorqueCurrentFOC(units::angle::turn_t Position)
Requires Phoenix Pro; Requests Motion Magic® to target a final position using a motion profile.
Definition MotionMagicTorqueCurrentFOC.hpp:145
constexpr MotionMagicTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition MotionMagicTorqueCurrentFOC.hpp:229
constexpr MotionMagicTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition MotionMagicTorqueCurrentFOC.hpp:246
constexpr MotionMagicTorqueCurrentFOC & WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicTorqueCurrentFOC.hpp:307
constexpr ~MotionMagicTorqueCurrentFOC() override
Definition MotionMagicTorqueCurrentFOC.hpp:149
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14