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