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