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