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