CTRE Phoenix 6 C++ 25.3.1
Loading...
Searching...
No Matches
MotionMagicExpoTorqueCurrentFOC.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/current.h>
14#include <units/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Requires Phoenix Pro;
24 * Requests Motion Magic® to target a final position using an exponential motion
25 * profile. Users can optionally provide a torque current feedforward.
26 *
27 * Motion Magic® Expo produces a motion profile in real-time while attempting to honor the Cruise Velocity
28 * (optional) and the mechanism kV and kA, specified via the Motion Magic® configuration values. Note that
29 * unlike the slot gains, the Expo_kV and Expo_kA configs are always in output units of Volts.
30 *
31 * Setting Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV.
32 * This control mode does not use the Acceleration or Jerk configs.
33 *
34 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile. This
35 * control mode is based on torque current, so relevant closed-loop gains will use Amperes for the numerator.
36 */
38{
39 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
40 {
41 if (req.get() != this)
42 {
43 auto const reqCast = dynamic_cast<MotionMagicExpoTorqueCurrentFOC *>(req.get());
44 if (reqCast != nullptr)
45 {
46 *reqCast = *this;
47 }
48 else
49 {
50 req = std::make_shared<MotionMagicExpoTorqueCurrentFOC>(*this);
51 }
52 }
53
55 }
56
57public:
58 /**
59 * \brief Position to drive toward in rotations.
60 *
61 * - Units: rotations
62 *
63 */
64 units::angle::turn_t Position;
65 /**
66 * \brief Feedforward to apply in torque current in Amperes. User can use
67 * motor's kT to scale Newton-meter to Amperes.
68 *
69 * - Units: A
70 *
71 */
72 units::current::ampere_t FeedForward = 0.0_A;
73 /**
74 * \brief Select which gains are applied by selecting the slot. Use the
75 * configuration api to set the gain values for the selected slot before
76 * enabling this feature. Slot must be within [0,2].
77 */
78 int Slot = 0;
79 /**
80 * \brief Set to true to coast the rotor when output is zero (or within
81 * deadband). Set to false to use the NeutralMode configuration setting
82 * (default). This flag exists to provide the fundamental behavior of this
83 * control when output is zero, which is to provide 0A (zero torque).
84 */
86 /**
87 * \brief Set to true to force forward limiting. This allows users to use other
88 * limit switch sensors connected to robot controller. This also allows use of
89 * active sensors that require external power.
90 */
91 bool LimitForwardMotion = false;
92 /**
93 * \brief Set to true to force reverse limiting. This allows users to use other
94 * limit switch sensors connected to robot controller. This also allows use of
95 * active sensors that require external power.
96 */
97 bool LimitReverseMotion = false;
98 /**
99 * \brief Set to true to ignore hardware limit switches and the
100 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
101 * motion.
102 *
103 * This can be useful on mechanisms such as an intake/feeder, where a limit
104 * switch stops motion while intaking but should be ignored when feeding to a
105 * shooter.
106 *
107 * The hardware limit faults and Forward/ReverseLimit signals will still report
108 * the values of the limit switches regardless of this parameter.
109 */
111 /**
112 * \brief Set to true to delay applying this control request until a timesync
113 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
114 * nondeterministic network delays in exchange for a larger but deterministic
115 * control latency.
116 *
117 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
118 * Additionally, when this is enabled, the UpdateFreqHz of this request should
119 * be set to 0 Hz.
120 */
121 bool UseTimesync = false;
122
123 /**
124 * \brief The period at which this control will update at.
125 * This is designated in Hertz, with a minimum of 20 Hz
126 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
127 *
128 * If this field is set to 0 Hz, the control request will
129 * be sent immediately as a one-shot frame. This may be useful
130 * for advanced applications that require outputs to be
131 * synchronized with data acquisition. In this case, we
132 * recommend not exceeding 50 ms between control calls.
133 */
134 units::frequency::hertz_t UpdateFreqHz{100_Hz};
135
136 /**
137 * \brief Requires Phoenix Pro;
138 * Requests Motion Magic® to target a final position using an exponential
139 * motion profile. Users can optionally provide a torque current
140 * feedforward.
141 *
142 * \details Motion Magic® Expo produces a motion profile in real-time while
143 * attempting to honor the Cruise Velocity (optional) and the mechanism
144 * kV and kA, specified via the Motion Magic® configuration values.
145 * Note that unlike the slot gains, the Expo_kV and Expo_kA configs are
146 * always in output units of Volts.
147 *
148 * Setting Cruise Velocity to 0 will allow the profile to run to the
149 * max possible velocity based on Expo_kV. This control mode does not
150 * use the Acceleration or Jerk configs.
151 *
152 * Target position can be changed on-the-fly and Motion Magic® will do
153 * its best to adjust the profile. This control mode is based on
154 * torque current, so relevant closed-loop gains will use Amperes for
155 * the numerator.
156 *
157 * \param Position Position to drive toward in rotations.
158 */
159 MotionMagicExpoTorqueCurrentFOC(units::angle::turn_t Position) : ControlRequest{"MotionMagicExpoTorqueCurrentFOC"},
160 Position{std::move(Position)}
161 {}
162
163 /**
164 * \brief Modifies this Control Request's Position parameter and returns itself for
165 * method-chaining and easier to use request API.
166 *
167 * Position to drive toward in rotations.
168 *
169 * - Units: rotations
170 *
171 *
172 * \param newPosition Parameter to modify
173 * \returns Itself
174 */
175 MotionMagicExpoTorqueCurrentFOC &WithPosition(units::angle::turn_t newPosition)
176 {
177 Position = std::move(newPosition);
178 return *this;
179 }
180
181 /**
182 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
183 * method-chaining and easier to use request API.
184 *
185 * Feedforward to apply in torque current in Amperes. User can use motor's kT
186 * to scale Newton-meter to Amperes.
187 *
188 * - Units: A
189 *
190 *
191 * \param newFeedForward Parameter to modify
192 * \returns Itself
193 */
194 MotionMagicExpoTorqueCurrentFOC &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 */
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 */
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 */
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 */
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 */
287 {
288 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
289 return *this;
290 }
291
292 /**
293 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
294 * method-chaining and easier to use request API.
295 *
296 * Set to true to delay applying this control request until a timesync boundary
297 * (requires Phoenix Pro and CANivore). This eliminates the impact of
298 * nondeterministic network delays in exchange for a larger but deterministic
299 * control latency.
300 *
301 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
302 * Additionally, when this is enabled, the UpdateFreqHz of this request should
303 * be set to 0 Hz.
304 *
305 * \param newUseTimesync Parameter to modify
306 * \returns Itself
307 */
309 {
310 UseTimesync = std::move(newUseTimesync);
311 return *this;
312 }
313 /**
314 * \brief Sets the period at which this control will update at.
315 * This is designated in Hertz, with a minimum of 20 Hz
316 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
317 *
318 * If this field is set to 0 Hz, the control request will
319 * be sent immediately as a one-shot frame. This may be useful
320 * for advanced applications that require outputs to be
321 * synchronized with data acquisition. In this case, we
322 * recommend not exceeding 50 ms between control calls.
323 *
324 * \param newUpdateFreqHz Parameter to modify
325 * \returns Itself
326 */
327 MotionMagicExpoTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
328 {
329 UpdateFreqHz = newUpdateFreqHz;
330 return *this;
331 }
332 /**
333 * \brief Returns a string representation of the object.
334 *
335 * \returns a string representation of the object.
336 */
337 std::string ToString() const override
338 {
339 std::stringstream ss;
340 ss << "Control: MotionMagicExpoTorqueCurrentFOC" << std::endl;
341 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
342 ss << " FeedForward: " << FeedForward.to<double>() << " A" << std::endl;
343 ss << " Slot: " << Slot << std::endl;
344 ss << " OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
345 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
346 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
347 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
348 ss << " UseTimesync: " << UseTimesync << std::endl;
349 return ss.str();
350 }
351
352 /**
353 * \brief Gets information about this control request.
354 *
355 * \returns Map of control parameter names and corresponding applied values
356 */
357 std::map<std::string, std::string> GetControlInfo() const override
358 {
359 std::map<std::string, std::string> controlInfo;
360 std::stringstream ss;
361 controlInfo["Name"] = GetName();
362 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
363 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
364 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
365 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
366 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
367 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
368 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
369 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
370 return controlInfo;
371 }
372};
373
374}
375}
376}
377
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicExpoTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, double Position, 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; Requests Motion Magic® to target a final position using an exponential motion p...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:38
MotionMagicExpoTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:211
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:357
MotionMagicExpoTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:327
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:110
std::string ToString() const override
Returns a string representation of the object.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:337
units::angle::turn_t Position
Position to drive toward in rotations.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:64
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:121
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:97
MotionMagicExpoTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:175
MotionMagicExpoTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:229
int Slot
Select which gains are applied by selecting the slot.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:78
MotionMagicExpoTorqueCurrentFOC(units::angle::turn_t Position)
Requires Phoenix Pro; Requests Motion Magic® to target a final position using an exponential motion p...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:159
MotionMagicExpoTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:194
MotionMagicExpoTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:263
bool LimitForwardMotion
Set to true to force forward limiting.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:91
MotionMagicExpoTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:246
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:72
MotionMagicExpoTorqueCurrentFOC & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:286
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition MotionMagicExpoTorqueCurrentFOC.hpp:85
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:134
MotionMagicExpoTorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition MotionMagicExpoTorqueCurrentFOC.hpp:308
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401