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