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