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