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