CTRE Phoenix 6 C++ 24.3.0
MotionMagicExpoDutyCycle.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/dimensionless.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Requests Motion Magic® to target a final position using an exponential motion
25 * profile. Users can optionally provide a duty cycle 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. Setting
29 * Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV. This
30 * control mode does not use the Acceleration or Jerk configs. Target position can be changed on-the-fly and
31 * Motion Magic® will do its best to adjust the profile. This control mode is duty cycle based, so relevant
32 * closed-loop gains will use fractional duty cycle for the numerator: +1.0 represents full forward output.
33 */
35{
36 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
37 {
38 if (req.get() != this)
39 {
40 auto const reqCast = dynamic_cast<MotionMagicExpoDutyCycle *>(req.get());
41 if (reqCast != nullptr)
42 {
43 *reqCast = *this;
44 }
45 else
46 {
47 req = std::make_shared<MotionMagicExpoDutyCycle>(*this);
48 }
49 }
50
51 return c_ctre_phoenix6_RequestControlMotionMagicExpoDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
52 }
53
54public:
55 /**
56 * Position to drive toward in rotations.
57 */
58 units::angle::turn_t Position;
59 /**
60 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
61 * peak power by ~15%. Set to false to use trapezoidal commutation.
62 *
63 * FOC improves motor performance by leveraging torque (current) control.
64 * However, this may be inconvenient for applications that require specifying
65 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
66 * combines the performances gains of FOC while still allowing applications to
67 * provide duty cycle or voltage demand. This not to be confused with simple
68 * sinusoidal control or phase voltage control which lacks the performance
69 * gains.
70 */
72 /**
73 * Feedforward to apply in fractional units between -1 and +1.
74 */
75 units::dimensionless::scalar_t FeedForward;
76 /**
77 * Select which gains are applied by selecting the slot. Use the configuration
78 * api to set the gain values for the selected slot before enabling this
79 * feature. Slot must be within [0,2].
80 */
81 int Slot;
82 /**
83 * Set to true to static-brake the rotor when output is zero (or within
84 * deadband). Set to false to use the NeutralMode configuration setting
85 * (default). This flag exists to provide the fundamental behavior of this
86 * control when output is zero, which is to provide 0V to the motor.
87 */
89 /**
90 * Set to true to force forward limiting. This allows users to use other limit
91 * switch sensors connected to robot controller. This also allows use of active
92 * sensors that require external power.
93 */
95 /**
96 * Set to true to force reverse limiting. This allows users to use other limit
97 * switch sensors connected to robot controller. This also allows use of active
98 * sensors that require external power.
99 */
101
102 /**
103 * \brief The period at which this control will update at.
104 * This is designated in Hertz, with a minimum of 20 Hz
105 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
106 *
107 * If this field is set to 0 Hz, the control request will
108 * be sent immediately as a one-shot frame. This may be useful
109 * for advanced applications that require outputs to be
110 * synchronized with data acquisition. In this case, we
111 * recommend not exceeding 50 ms between control calls.
112 */
113 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
114
115 /**
116 * \brief Requests Motion Magic® to target a final position using an exponential
117 * motion profile. Users can optionally provide a duty cycle
118 * feedforward.
119 *
120 * \details Motion Magic® Expo produces a motion profile in real-time while
121 * attempting to honor the Cruise Velocity (optional) and the mechanism
122 * kV and kA, specified via the Motion Magic® configuration values.
123 * Setting Cruise Velocity to 0 will allow the profile to run to the
124 * max possible velocity based on Expo_kV. This control mode does not
125 * use the Acceleration or Jerk configs. Target position can be
126 * changed on-the-fly and Motion Magic® will do its best to adjust the
127 * profile. This control mode is duty cycle based, so relevant
128 * closed-loop gains will use fractional duty cycle for the numerator:
129 * +1.0 represents full forward output.
130 *
131 * \param Position Position to drive toward in rotations.
132 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
133 * Pro), which increases peak power by ~15%. Set to false to
134 * use trapezoidal commutation.
135 *
136 * FOC improves motor performance by leveraging torque
137 * (current) control. However, this may be inconvenient for
138 * applications that require specifying duty cycle or
139 * voltage. CTR-Electronics has developed a hybrid method
140 * that combines the performances gains of FOC while still
141 * allowing applications to provide duty cycle or voltage
142 * demand. This not to be confused with simple sinusoidal
143 * control or phase voltage control which lacks the
144 * performance gains.
145 * \param FeedForward Feedforward to apply in fractional units between -1 and
146 * +1.
147 * \param Slot Select which gains are applied by selecting the slot. Use the
148 * configuration api to set the gain values for the selected slot
149 * before enabling this feature. Slot must be within [0,2].
150 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
151 * output is zero (or within deadband). Set
152 * to false to use the NeutralMode
153 * configuration setting (default). This flag
154 * exists to provide the fundamental behavior
155 * of this control when output is zero, which
156 * is to provide 0V to the motor.
157 * \param LimitForwardMotion Set to true to force forward limiting. This
158 * allows users to use other limit switch sensors
159 * connected to robot controller. This also allows
160 * use of active sensors that require external
161 * power.
162 * \param LimitReverseMotion Set to true to force reverse limiting. This
163 * allows users to use other limit switch sensors
164 * connected to robot controller. This also allows
165 * use of active sensors that require external
166 * power.
167 */
168 MotionMagicExpoDutyCycle(units::angle::turn_t Position, bool EnableFOC = true, units::dimensionless::scalar_t FeedForward = 0.0, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicExpoDutyCycle"},
169 Position{std::move(Position)},
170 EnableFOC{std::move(EnableFOC)},
171 FeedForward{std::move(FeedForward)},
172 Slot{std::move(Slot)},
176 {}
177
178 /**
179 * \brief Modifies this Control Request's Position parameter and returns itself for
180 * method-chaining and easier to use request API.
181 *
182 * Position to drive toward in rotations.
183 *
184 * \param newPosition Parameter to modify
185 * \returns Itself
186 */
187 MotionMagicExpoDutyCycle& WithPosition(units::angle::turn_t newPosition)
188 {
189 Position = std::move(newPosition);
190 return *this;
191 }
192
193 /**
194 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
195 * method-chaining and easier to use request API.
196 *
197 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
198 * peak power by ~15%. Set to false to use trapezoidal commutation.
199 *
200 * FOC improves motor performance by leveraging torque (current) control.
201 * However, this may be inconvenient for applications that require specifying
202 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
203 * combines the performances gains of FOC while still allowing applications to
204 * provide duty cycle or voltage demand. This not to be confused with simple
205 * sinusoidal control or phase voltage control which lacks the performance
206 * gains.
207 *
208 * \param newEnableFOC Parameter to modify
209 * \returns Itself
210 */
212 {
213 EnableFOC = std::move(newEnableFOC);
214 return *this;
215 }
216
217 /**
218 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
219 * method-chaining and easier to use request API.
220 *
221 * Feedforward to apply in fractional units between -1 and +1.
222 *
223 * \param newFeedForward Parameter to modify
224 * \returns Itself
225 */
226 MotionMagicExpoDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
227 {
228 FeedForward = std::move(newFeedForward);
229 return *this;
230 }
231
232 /**
233 * \brief Modifies this Control Request's Slot parameter and returns itself for
234 * method-chaining and easier to use request API.
235 *
236 * Select which gains are applied by selecting the slot. Use the configuration
237 * api to set the gain values for the selected slot before enabling this
238 * feature. Slot must be within [0,2].
239 *
240 * \param newSlot Parameter to modify
241 * \returns Itself
242 */
244 {
245 Slot = std::move(newSlot);
246 return *this;
247 }
248
249 /**
250 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
251 * method-chaining and easier to use request API.
252 *
253 * Set to true to static-brake the rotor when output is zero (or within
254 * deadband). Set to false to use the NeutralMode configuration setting
255 * (default). This flag exists to provide the fundamental behavior of this
256 * control when output is zero, which is to provide 0V to the motor.
257 *
258 * \param newOverrideBrakeDurNeutral Parameter to modify
259 * \returns Itself
260 */
262 {
263 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
264 return *this;
265 }
266
267 /**
268 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
269 * method-chaining and easier to use request API.
270 *
271 * Set to true to force forward limiting. This allows users to use other limit
272 * switch sensors connected to robot controller. This also allows use of active
273 * sensors that require external power.
274 *
275 * \param newLimitForwardMotion Parameter to modify
276 * \returns Itself
277 */
279 {
280 LimitForwardMotion = std::move(newLimitForwardMotion);
281 return *this;
282 }
283
284 /**
285 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
286 * method-chaining and easier to use request API.
287 *
288 * Set to true to force reverse limiting. This allows users to use other limit
289 * switch sensors connected to robot controller. This also allows use of active
290 * sensors that require external power.
291 *
292 * \param newLimitReverseMotion Parameter to modify
293 * \returns Itself
294 */
296 {
297 LimitReverseMotion = std::move(newLimitReverseMotion);
298 return *this;
299 }
300 /**
301 * \brief Sets the period at which this control will update at.
302 * This is designated in Hertz, with a minimum of 20 Hz
303 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
304 *
305 * If this field is set to 0 Hz, the control request will
306 * be sent immediately as a one-shot frame. This may be useful
307 * for advanced applications that require outputs to be
308 * synchronized with data acquisition. In this case, we
309 * recommend not exceeding 50 ms between control calls.
310 *
311 * \param newUpdateFreqHz Parameter to modify
312 * \returns Itself
313 */
314 MotionMagicExpoDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
315 {
316 UpdateFreqHz = newUpdateFreqHz;
317 return *this;
318 }
319 /**
320 * Returns a string representation of the object.
321 *
322 * \returns a string representation of the object.
323 */
324 std::string ToString() const override
325 {
326 std::stringstream ss;
327 ss << "class: MotionMagicExpoDutyCycle" << std::endl;
328 ss << "Position: " << Position.to<double>() << std::endl;
329 ss << "EnableFOC: " << EnableFOC << std::endl;
330 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
331 ss << "Slot: " << Slot << std::endl;
332 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
333 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
334 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
335 return ss.str();
336 }
337
338 /**
339 * \brief Gets information about this control request.
340 *
341 * \returns Map of control parameter names and corresponding applied values
342 */
343 std::map<std::string, std::string> GetControlInfo() const override
344 {
345 std::map<std::string, std::string> controlInfo;
346 std::stringstream ss;
347 controlInfo["Name"] = GetName();
348 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
349 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
350 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
351 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
352 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
353 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
354 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
355 return controlInfo;
356 }
357};
358
359}
360}
361}
362
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicExpoDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition: MotionMagicExpoDutyCycle.hpp:35
MotionMagicExpoDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicExpoDutyCycle.hpp:295
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicExpoDutyCycle.hpp:100
MotionMagicExpoDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicExpoDutyCycle.hpp:261
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: MotionMagicExpoDutyCycle.hpp:71
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicExpoDutyCycle.hpp:58
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicExpoDutyCycle.hpp:94
MotionMagicExpoDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicExpoDutyCycle.hpp:211
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicExpoDutyCycle.hpp:81
MotionMagicExpoDutyCycle & WithFeedForward(units::dimensionless::scalar_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: MotionMagicExpoDutyCycle.hpp:226
MotionMagicExpoDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicExpoDutyCycle.hpp:314
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicExpoDutyCycle.hpp:113
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: MotionMagicExpoDutyCycle.hpp:88
MotionMagicExpoDutyCycle & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: MotionMagicExpoDutyCycle.hpp:187
MotionMagicExpoDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicExpoDutyCycle.hpp:243
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicExpoDutyCycle.hpp:324
MotionMagicExpoDutyCycle(units::angle::turn_t Position, bool EnableFOC=true, units::dimensionless::scalar_t FeedForward=0.0, int Slot=0, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition: MotionMagicExpoDutyCycle.hpp:168
MotionMagicExpoDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicExpoDutyCycle.hpp:278
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: MotionMagicExpoDutyCycle.hpp:75
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicExpoDutyCycle.hpp:343
Definition: string_util.hpp:15