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