Loading [MathJax]/extensions/tex2jax.js
CTRE Phoenix 6 C++ 25.4.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
11#include <sstream>
12
13#include <units/frequency.h>
14#include <units/time.h>
15#include <units/angle.h>
16#include <units/dimensionless.h>
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Requests Motion Magic® to target a final position using an exponential motion
24 * profile. Users can optionally provide a duty cycle feedforward.
25 *
26 * Motion Magic® Expo produces a motion profile in real-time while attempting to honor the Cruise Velocity
27 * (optional) and the mechanism kV and kA, specified via the Motion Magic® configuration values. Note that
28 * unlike the slot gains, the Expo_kV and Expo_kA configs are always in output units of Volts.
29 *
30 * Setting Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV.
31 * This control mode does not use the Acceleration or Jerk configs.
32 *
33 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile. This
34 * control mode is duty cycle based, so relevant closed-loop gains will use fractional duty cycle for the
35 * numerator: +1.0 represents full forward output.
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<MotionMagicExpoDutyCycle *>(req.get());
44 if (reqCast != nullptr)
45 {
46 *reqCast = *this;
47 }
48 else
49 {
50 req = std::make_shared<MotionMagicExpoDutyCycle>(*this);
51 }
52 }
53
55 }
56
57public:
58 /**
59 * \brief Position to drive toward in rotations.
60 *
61 * - Units: rotations
62 *
63 */
64 units::angle::turn_t Position;
65 /**
66 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
67 * increases peak power by ~15% on supported devices (see
68 * hardware#traits#SupportsFOC). Set to false to use trapezoidal commutation.
69 *
70 * FOC improves motor performance by leveraging torque (current) control.
71 * However, this may be inconvenient for applications that require specifying
72 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
73 * combines the performances gains of FOC while still allowing applications to
74 * provide duty cycle or voltage demand. This not to be confused with simple
75 * sinusoidal control or phase voltage control which lacks the performance
76 * gains.
77 */
78 bool EnableFOC = true;
79 /**
80 * \brief Feedforward to apply in fractional units between -1 and +1.
81 *
82 * - Units: fractional
83 *
84 */
85 units::dimensionless::scalar_t FeedForward = 0.0;
86 /**
87 * \brief Select which gains are applied by selecting the slot. Use the
88 * configuration api to set the gain values for the selected slot before
89 * enabling this feature. Slot must be within [0,2].
90 */
91 int Slot = 0;
92 /**
93 * \brief Set to true to static-brake the rotor when output is zero (or within
94 * deadband). Set to false to use the NeutralMode configuration setting
95 * (default). This flag exists to provide the fundamental behavior of this
96 * control when output is zero, which is to provide 0V to the motor.
97 */
99 /**
100 * \brief Set to true to force forward limiting. This allows users to use other
101 * limit switch sensors connected to robot controller. This also allows use of
102 * active sensors that require external power.
103 */
104 bool LimitForwardMotion = false;
105 /**
106 * \brief Set to true to force reverse limiting. This allows users to use other
107 * limit switch sensors connected to robot controller. This also allows use of
108 * active sensors that require external power.
109 */
110 bool LimitReverseMotion = false;
111 /**
112 * \brief Set to true to ignore hardware limit switches and the
113 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
114 * motion.
115 *
116 * This can be useful on mechanisms such as an intake/feeder, where a limit
117 * switch stops motion while intaking but should be ignored when feeding to a
118 * shooter.
119 *
120 * The hardware limit faults and Forward/ReverseLimit signals will still report
121 * the values of the limit switches regardless of this parameter.
122 */
124 /**
125 * \brief Set to true to delay applying this control request until a timesync
126 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
127 * nondeterministic network delays in exchange for a larger but deterministic
128 * control latency.
129 *
130 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
131 * Additionally, when this is enabled, the UpdateFreqHz of this request should
132 * be set to 0 Hz.
133 */
134 bool UseTimesync = false;
135
136 /**
137 * \brief The period at which this control will update at.
138 * This is designated in Hertz, with a minimum of 20 Hz
139 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
140 *
141 * If this field is set to 0 Hz, the control request will
142 * be sent immediately as a one-shot frame. This may be useful
143 * for advanced applications that require outputs to be
144 * synchronized with data acquisition. In this case, we
145 * recommend not exceeding 50 ms between control calls.
146 */
147 units::frequency::hertz_t UpdateFreqHz{100_Hz};
148
149 /**
150 * \brief Requests Motion Magic® to target a final position using an exponential
151 * motion profile. Users can optionally provide a duty cycle
152 * feedforward.
153 *
154 * \details Motion Magic® Expo produces a motion profile in real-time while
155 * attempting to honor the Cruise Velocity (optional) and the mechanism
156 * kV and kA, specified via the Motion Magic® configuration values.
157 * Note that unlike the slot gains, the Expo_kV and Expo_kA configs are
158 * always in output units of Volts.
159 *
160 * Setting Cruise Velocity to 0 will allow the profile to run to the
161 * max possible velocity based on Expo_kV. This control mode does not
162 * use the Acceleration or Jerk configs.
163 *
164 * Target position can be changed on-the-fly and Motion Magic® will do
165 * its best to adjust the profile. This control mode is duty cycle
166 * based, so relevant closed-loop gains will use fractional duty cycle
167 * for the numerator: +1.0 represents full forward output.
168 *
169 * \param Position Position to drive toward in rotations.
170 */
171 MotionMagicExpoDutyCycle(units::angle::turn_t Position) : ControlRequest{"MotionMagicExpoDutyCycle"},
172 Position{std::move(Position)}
173 {}
174
175 /**
176 * \brief Modifies this Control Request's Position parameter and returns itself for
177 * method-chaining and easier to use request API.
178 *
179 * Position to drive toward in rotations.
180 *
181 * - Units: rotations
182 *
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% on supported devices (see hardware#traits#SupportsFOC).
199 * Set to false to use trapezoidal commutation.
200 *
201 * FOC improves motor performance by leveraging torque (current) control.
202 * However, this may be inconvenient for applications that require specifying
203 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
204 * combines the performances gains of FOC while still allowing applications to
205 * provide duty cycle or voltage demand. This not to be confused with simple
206 * sinusoidal control or phase voltage control which lacks the performance
207 * gains.
208 *
209 * \param newEnableFOC Parameter to modify
210 * \returns Itself
211 */
213 {
214 EnableFOC = std::move(newEnableFOC);
215 return *this;
216 }
217
218 /**
219 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
220 * method-chaining and easier to use request API.
221 *
222 * Feedforward to apply in fractional units between -1 and +1.
223 *
224 * - Units: fractional
225 *
226 *
227 * \param newFeedForward Parameter to modify
228 * \returns Itself
229 */
230 MotionMagicExpoDutyCycle &WithFeedForward(units::dimensionless::scalar_t newFeedForward)
231 {
232 FeedForward = std::move(newFeedForward);
233 return *this;
234 }
235
236 /**
237 * \brief Modifies this Control Request's Slot parameter and returns itself for
238 * method-chaining and easier to use request API.
239 *
240 * Select which gains are applied by selecting the slot. Use the configuration
241 * api to set the gain values for the selected slot before enabling this
242 * feature. Slot must be within [0,2].
243 *
244 * \param newSlot Parameter to modify
245 * \returns Itself
246 */
248 {
249 Slot = std::move(newSlot);
250 return *this;
251 }
252
253 /**
254 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
255 * method-chaining and easier to use request API.
256 *
257 * Set to true to static-brake the rotor when output is zero (or within
258 * deadband). Set to false to use the NeutralMode configuration setting
259 * (default). This flag exists to provide the fundamental behavior of this
260 * control when output is zero, which is to provide 0V to the motor.
261 *
262 * \param newOverrideBrakeDurNeutral Parameter to modify
263 * \returns Itself
264 */
266 {
267 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
268 return *this;
269 }
270
271 /**
272 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
273 * method-chaining and easier to use request API.
274 *
275 * Set to true to force forward limiting. This allows users to use other limit
276 * switch sensors connected to robot controller. This also allows use of active
277 * sensors that require external power.
278 *
279 * \param newLimitForwardMotion Parameter to modify
280 * \returns Itself
281 */
283 {
284 LimitForwardMotion = std::move(newLimitForwardMotion);
285 return *this;
286 }
287
288 /**
289 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
290 * method-chaining and easier to use request API.
291 *
292 * Set to true to force reverse limiting. This allows users to use other limit
293 * switch sensors connected to robot controller. This also allows use of active
294 * sensors that require external power.
295 *
296 * \param newLimitReverseMotion Parameter to modify
297 * \returns Itself
298 */
300 {
301 LimitReverseMotion = std::move(newLimitReverseMotion);
302 return *this;
303 }
304
305 /**
306 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
307 * method-chaining and easier to use request API.
308 *
309 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
310 * LimitReverseMotion parameters, instead allowing motion.
311 *
312 * This can be useful on mechanisms such as an intake/feeder, where a limit
313 * switch stops motion while intaking but should be ignored when feeding to a
314 * shooter.
315 *
316 * The hardware limit faults and Forward/ReverseLimit signals will still report
317 * the values of the limit switches regardless of this parameter.
318 *
319 * \param newIgnoreHardwareLimits Parameter to modify
320 * \returns Itself
321 */
323 {
324 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
325 return *this;
326 }
327
328 /**
329 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
330 * method-chaining and easier to use request API.
331 *
332 * Set to true to delay applying this control request until a timesync boundary
333 * (requires Phoenix Pro and CANivore). This eliminates the impact of
334 * nondeterministic network delays in exchange for a larger but deterministic
335 * control latency.
336 *
337 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
338 * Additionally, when this is enabled, the UpdateFreqHz of this request should
339 * be set to 0 Hz.
340 *
341 * \param newUseTimesync Parameter to modify
342 * \returns Itself
343 */
345 {
346 UseTimesync = std::move(newUseTimesync);
347 return *this;
348 }
349 /**
350 * \brief Sets the period at which this control will update at.
351 * This is designated in Hertz, with a minimum of 20 Hz
352 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
353 *
354 * If this field is set to 0 Hz, the control request will
355 * be sent immediately as a one-shot frame. This may be useful
356 * for advanced applications that require outputs to be
357 * synchronized with data acquisition. In this case, we
358 * recommend not exceeding 50 ms between control calls.
359 *
360 * \param newUpdateFreqHz Parameter to modify
361 * \returns Itself
362 */
363 MotionMagicExpoDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
364 {
365 UpdateFreqHz = newUpdateFreqHz;
366 return *this;
367 }
368 /**
369 * \brief Returns a string representation of the object.
370 *
371 * \returns a string representation of the object.
372 */
373 std::string ToString() const override
374 {
375 std::stringstream ss;
376 ss << "Control: MotionMagicExpoDutyCycle" << std::endl;
377 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
378 ss << " EnableFOC: " << EnableFOC << std::endl;
379 ss << " FeedForward: " << FeedForward.to<double>() << " fractional" << std::endl;
380 ss << " Slot: " << Slot << std::endl;
381 ss << " OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
382 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
383 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
384 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
385 ss << " UseTimesync: " << UseTimesync << std::endl;
386 return ss.str();
387 }
388
389 /**
390 * \brief Gets information about this control request.
391 *
392 * \returns Map of control parameter names and corresponding applied values
393 */
394 std::map<std::string, std::string> GetControlInfo() const override
395 {
396 std::map<std::string, std::string> controlInfo;
397 std::stringstream ss;
398 controlInfo["Name"] = GetName();
399 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
400 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
401 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
402 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
403 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
404 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
405 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
406 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
407 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
408 return controlInfo;
409 }
410};
411
412}
413}
414}
415
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicExpoDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateFrequency, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, 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
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition MotionMagicExpoDutyCycle.hpp:38
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition MotionMagicExpoDutyCycle.hpp:123
MotionMagicExpoDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoDutyCycle.hpp:299
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition MotionMagicExpoDutyCycle.hpp:110
MotionMagicExpoDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition MotionMagicExpoDutyCycle.hpp:265
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15% on supp...
Definition MotionMagicExpoDutyCycle.hpp:78
MotionMagicExpoDutyCycle(units::angle::turn_t Position)
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition MotionMagicExpoDutyCycle.hpp:171
units::angle::turn_t Position
Position to drive toward in rotations.
Definition MotionMagicExpoDutyCycle.hpp:64
bool LimitForwardMotion
Set to true to force forward limiting.
Definition MotionMagicExpoDutyCycle.hpp:104
MotionMagicExpoDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition MotionMagicExpoDutyCycle.hpp:212
MotionMagicExpoDutyCycle & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicExpoDutyCycle.hpp:322
int Slot
Select which gains are applied by selecting the slot.
Definition MotionMagicExpoDutyCycle.hpp:91
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition MotionMagicExpoDutyCycle.hpp:134
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:230
MotionMagicExpoDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition MotionMagicExpoDutyCycle.hpp:363
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition MotionMagicExpoDutyCycle.hpp:147
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition MotionMagicExpoDutyCycle.hpp:98
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:247
std::string ToString() const override
Returns a string representation of the object.
Definition MotionMagicExpoDutyCycle.hpp:373
MotionMagicExpoDutyCycle & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition MotionMagicExpoDutyCycle.hpp:344
MotionMagicExpoDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoDutyCycle.hpp:282
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition MotionMagicExpoDutyCycle.hpp:85
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition MotionMagicExpoDutyCycle.hpp:394
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition Diff_PositionDutyCycle_Position.hpp:15
Definition span.hpp:401