CTRE Phoenix 6 C++ 24.50.0-alpha-2
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
12#include <sstream>
13#include <units/angle.h>
14#include <units/current.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Requires Phoenix Pro;
25 * Requests Motion Magic® to target a final position using an exponential motion
26 * profile. Users can optionally provide a torque current feedforward.
27 *
28 * Motion Magic® Expo produces a motion profile in real-time while attempting to honor the Cruise Velocity
29 * (optional) and the mechanism kV and kA, specified via the Motion Magic® configuration values. Note that
30 * unlike the slot gains, the Expo_kV and Expo_kA configs are always in output units of Volts.
31 *
32 * Setting Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV.
33 * This control mode does not use the Acceleration or Jerk configs.
34 *
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, std::shared_ptr<ControlRequest> &req) override
41 {
42 if (req.get() != this)
43 {
44 auto const reqCast = dynamic_cast<MotionMagicExpoTorqueCurrentFOC *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<MotionMagicExpoTorqueCurrentFOC>(*this);
52 }
53 }
54
56 }
57
58public:
59 /**
60 * Position to drive toward in rotations.
61 */
62 units::angle::turn_t Position;
63 /**
64 * Feedforward to apply in torque current in Amperes. User can use motor's kT
65 * to scale Newton-meter to Amperes.
66 */
67 units::current::ampere_t FeedForward;
68 /**
69 * Select which gains are applied by selecting the slot. Use the configuration
70 * api to set the gain values for the selected slot before enabling this
71 * feature. Slot must be within [0,2].
72 */
73 int Slot;
74 /**
75 * Set to true to coast the rotor when output is zero (or within deadband). Set
76 * to false to use the NeutralMode configuration setting (default). This flag
77 * exists to provide the fundamental behavior of this control when output is
78 * zero, which is to provide 0A (zero torque).
79 */
81 /**
82 * Set to true to force forward limiting. This allows users to use other limit
83 * switch sensors connected to robot controller. This also allows use of active
84 * sensors that require external power.
85 */
87 /**
88 * Set to true to force reverse limiting. This allows users to use other limit
89 * switch sensors connected to robot controller. This also allows use of active
90 * sensors that require external power.
91 */
93 /**
94 * Set to true to delay applying this control request until a timesync boundary
95 * (requires Phoenix Pro and CANivore). This eliminates the impact of
96 * nondeterministic network delays in exchange for a larger but deterministic
97 * control latency.
98 */
100
101 /**
102 * \brief The period at which this control will update at.
103 * This is designated in Hertz, with a minimum of 20 Hz
104 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
105 *
106 * If this field is set to 0 Hz, the control request will
107 * be sent immediately as a one-shot frame. This may be useful
108 * for advanced applications that require outputs to be
109 * synchronized with data acquisition. In this case, we
110 * recommend not exceeding 50 ms between control calls.
111 */
112 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
113
114 /**
115 * \brief Requires Phoenix Pro;
116 * Requests Motion Magic® to target a final position using an exponential
117 * motion profile. Users can optionally provide a torque current
118 * feedforward.
119 *
120 * \details Motion Magic® Expo produces a motion profile in real-time while
121 * attempting to honor the Cruise Velocity (optional) and the mechanism
122 * kV and kA, specified via the Motion Magic® configuration values.
123 * Note that unlike the slot gains, the Expo_kV and Expo_kA configs are
124 * always in output units of Volts.
125 *
126 * Setting Cruise Velocity to 0 will allow the profile to run to the
127 * max possible velocity based on Expo_kV. This control mode does not
128 * use the Acceleration or Jerk configs.
129 *
130 * Target position can be changed on-the-fly and Motion Magic® will do
131 * its best to adjust the profile. This control mode is based on
132 * torque current, so relevant closed-loop gains will use Amperes for
133 * the numerator.
134 *
135 * \param Position Position to drive toward in rotations.
136 * \param FeedForward Feedforward to apply in torque current in Amperes.
137 * User can use motor's kT to scale Newton-meter to
138 * Amperes.
139 * \param Slot Select which gains are applied by selecting the slot. Use the
140 * configuration api to set the gain values for the selected slot
141 * before enabling this feature. Slot must be within [0,2].
142 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
143 * is zero (or within deadband). Set to false
144 * to use the NeutralMode configuration
145 * setting (default). This flag exists to
146 * provide the fundamental behavior of this
147 * control when output is zero, which is to
148 * provide 0A (zero torque).
149 * \param LimitForwardMotion Set to true to force forward limiting. This
150 * allows users to use other limit switch sensors
151 * connected to robot controller. This also allows
152 * use of active sensors that require external
153 * power.
154 * \param LimitReverseMotion Set to true to force reverse limiting. This
155 * allows users to use other limit switch sensors
156 * connected to robot controller. This also allows
157 * use of active sensors that require external
158 * power.
159 * \param UseTimesync Set to true to delay applying this control request
160 * until a timesync boundary (requires Phoenix Pro and
161 * CANivore). This eliminates the impact of
162 * nondeterministic network delays in exchange for a
163 * larger but deterministic control latency.
164 */
165 MotionMagicExpoTorqueCurrentFOC(units::angle::turn_t Position, units::current::ampere_t FeedForward = 0.0_A, int Slot = 0, bool OverrideCoastDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false, bool UseTimesync = false) : ControlRequest{"MotionMagicExpoTorqueCurrentFOC"},
166 Position{std::move(Position)},
168 Slot{std::move(Slot)},
173 {}
174
175 /**
176 * \brief Modifies this Control Request's Position parameter and returns itself for
177 * method-chaining and easier to use request API.
178 *
179 * Position to drive toward in rotations.
180 *
181 * \param newPosition Parameter to modify
182 * \returns Itself
183 */
184 MotionMagicExpoTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
185 {
186 Position = std::move(newPosition);
187 return *this;
188 }
189
190 /**
191 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
192 * method-chaining and easier to use request API.
193 *
194 * Feedforward to apply in torque current in Amperes. User can use motor's kT
195 * to scale Newton-meter to Amperes.
196 *
197 * \param newFeedForward Parameter to modify
198 * \returns Itself
199 */
200 MotionMagicExpoTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
201 {
202 FeedForward = std::move(newFeedForward);
203 return *this;
204 }
205
206 /**
207 * \brief Modifies this Control Request's Slot parameter and returns itself for
208 * method-chaining and easier to use request API.
209 *
210 * Select which gains are applied by selecting the slot. Use the configuration
211 * api to set the gain values for the selected slot before enabling this
212 * feature. Slot must be within [0,2].
213 *
214 * \param newSlot Parameter to modify
215 * \returns Itself
216 */
218 {
219 Slot = std::move(newSlot);
220 return *this;
221 }
222
223 /**
224 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
225 * method-chaining and easier to use request API.
226 *
227 * Set to true to coast the rotor when output is zero (or within deadband). Set
228 * to false to use the NeutralMode configuration setting (default). This flag
229 * exists to provide the fundamental behavior of this control when output is
230 * zero, which is to provide 0A (zero torque).
231 *
232 * \param newOverrideCoastDurNeutral Parameter to modify
233 * \returns Itself
234 */
236 {
237 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
238 return *this;
239 }
240
241 /**
242 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
243 * method-chaining and easier to use request API.
244 *
245 * Set to true to force forward limiting. This allows users to use other limit
246 * switch sensors connected to robot controller. This also allows use of active
247 * sensors that require external power.
248 *
249 * \param newLimitForwardMotion Parameter to modify
250 * \returns Itself
251 */
253 {
254 LimitForwardMotion = std::move(newLimitForwardMotion);
255 return *this;
256 }
257
258 /**
259 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
260 * method-chaining and easier to use request API.
261 *
262 * Set to true to force reverse limiting. This allows users to use other limit
263 * switch sensors connected to robot controller. This also allows use of active
264 * sensors that require external power.
265 *
266 * \param newLimitReverseMotion Parameter to modify
267 * \returns Itself
268 */
270 {
271 LimitReverseMotion = std::move(newLimitReverseMotion);
272 return *this;
273 }
274
275 /**
276 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
277 * method-chaining and easier to use request API.
278 *
279 * Set to true to delay applying this control request until a timesync boundary
280 * (requires Phoenix Pro and CANivore). This eliminates the impact of
281 * nondeterministic network delays in exchange for a larger but deterministic
282 * control latency.
283 *
284 * \param newUseTimesync Parameter to modify
285 * \returns Itself
286 */
288 {
289 UseTimesync = std::move(newUseTimesync);
290 return *this;
291 }
292 /**
293 * \brief Sets the period at which this control will update at.
294 * This is designated in Hertz, with a minimum of 20 Hz
295 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
296 *
297 * If this field is set to 0 Hz, the control request will
298 * be sent immediately as a one-shot frame. This may be useful
299 * for advanced applications that require outputs to be
300 * synchronized with data acquisition. In this case, we
301 * recommend not exceeding 50 ms between control calls.
302 *
303 * \param newUpdateFreqHz Parameter to modify
304 * \returns Itself
305 */
306 MotionMagicExpoTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
307 {
308 UpdateFreqHz = newUpdateFreqHz;
309 return *this;
310 }
311 /**
312 * Returns a string representation of the object.
313 *
314 * \returns a string representation of the object.
315 */
316 std::string ToString() const override
317 {
318 std::stringstream ss;
319 ss << "Control: MotionMagicExpoTorqueCurrentFOC" << std::endl;
320 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
321 ss << " FeedForward: " << FeedForward.to<double>() << " A" << std::endl;
322 ss << " Slot: " << Slot << std::endl;
323 ss << " OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
324 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
325 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
326 ss << " UseTimesync: " << UseTimesync << 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 << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
342 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
343 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
344 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
345 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
346 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
347 return controlInfo;
348 }
349};
350
351}
352}
353}
354
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 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; Requests Motion Magic® to target a final position using an exponential motion p...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:39
MotionMagicExpoTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:217
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:335
MotionMagicExpoTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:306
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:316
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:62
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:99
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:92
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:184
MotionMagicExpoTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:235
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:73
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:200
MotionMagicExpoTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:269
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:86
MotionMagicExpoTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:252
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:67
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:80
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:112
MotionMagicExpoTorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:287
MotionMagicExpoTorqueCurrentFOC(units::angle::turn_t Position, 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; Requests Motion Magic® to target a final position using an exponential motion p...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:165
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