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