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