CTRE Phoenix 6 C++ 24.2.0
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
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 a motion profile.
25 * Users can optionally provide a duty cycle feedforward.
26 *
27 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
28 * Acceleration, and Jerk value specified via the Motion Magic® configuration values. This control mode does
29 * not use the Expo_kV or Expo_kA configs. Target position can be changed on-the-fly and Motion Magic® will
30 * do its best to adjust the profile. This control mode is duty cycle based, so relevant closed-loop gains
31 * will use fractional duty cycle for the numerator: +1.0 represents full forward output.
32 */
34{
35 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
36 {
37 if (req.get() != this)
38 {
39 auto const reqCast = dynamic_cast<MotionMagicDutyCycle *>(req.get());
40 if (reqCast != nullptr)
41 {
42 *reqCast = *this;
43 }
44 else
45 {
46 req = std::make_shared<MotionMagicDutyCycle>(*this);
47 }
48 }
49
50 return c_ctre_phoenix6_RequestControlMotionMagicDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
51 }
52
53public:
54 /**
55 * Position to drive toward in rotations.
56 */
57 units::angle::turn_t Position;
58 /**
59 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
60 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
61 * improves motor performance by leveraging torque (current) control. However,
62 * this may be inconvenient for applications that require specifying duty cycle
63 * or voltage. CTR-Electronics has developed a hybrid method that combines the
64 * performances gains of FOC while still allowing applications to provide duty
65 * cycle or voltage demand. This not to be confused with simple sinusoidal
66 * control or phase voltage control which lacks the performance gains.
67 */
69 /**
70 * Feedforward to apply in fractional units between -1 and +1.
71 */
72 units::dimensionless::scalar_t FeedForward;
73 /**
74 * Select which gains are applied by selecting the slot. Use the configuration
75 * api to set the gain values for the selected slot before enabling this
76 * feature. Slot must be within [0,2].
77 */
78 int Slot;
79 /**
80 * Set to true to static-brake the rotor when output is zero (or within
81 * deadband). Set to false to use the NeutralMode configuration setting
82 * (default). This flag exists to provide the fundamental behavior of this
83 * control when output is zero, which is to provide 0V to the motor.
84 */
86 /**
87 * Set to true to force forward limiting. This allows users to use other limit
88 * switch sensors connected to robot controller. This also allows use of active
89 * sensors that require external power.
90 */
92 /**
93 * Set to true to force reverse limiting. This allows users to use other limit
94 * switch sensors connected to robot controller. This also allows use of active
95 * sensors that require external power.
96 */
98
99 /**
100 * \brief The period at which this control will update at.
101 * This is designated in Hertz, with a minimum of 20 Hz
102 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
103 *
104 * If this field is set to 0 Hz, the control request will
105 * be sent immediately as a one-shot frame. This may be useful
106 * for advanced applications that require outputs to be
107 * synchronized with data acquisition. In this case, we
108 * recommend not exceeding 50 ms between control calls.
109 */
110 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
111
112 /**
113 * \brief Requests Motion Magic® to target a final position using a motion
114 * profile. Users can optionally provide a duty cycle feedforward.
115 *
116 * \details Motion Magic® produces a motion profile in real-time while
117 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
118 * value specified via the Motion Magic® configuration values. This
119 * control mode does not use the Expo_kV or Expo_kA configs. Target
120 * position can be changed on-the-fly and Motion Magic® will do its
121 * best to adjust the profile. This control mode is duty cycle based,
122 * so relevant closed-loop gains will use fractional duty cycle for the
123 * numerator: +1.0 represents full forward output.
124 *
125 * \param Position Position to drive toward in rotations.
126 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
127 * Pro), which increases peak power by ~15%. Set to false to
128 * use trapezoidal commutation. FOC improves motor
129 * performance by leveraging torque (current) control.
130 * However, this may be inconvenient for applications that
131 * require specifying duty cycle or voltage.
132 * CTR-Electronics has developed a hybrid method that
133 * combines the performances gains of FOC while still
134 * allowing applications to provide duty cycle or voltage
135 * demand. This not to be confused with simple sinusoidal
136 * control or phase voltage control which lacks the
137 * performance gains.
138 * \param FeedForward Feedforward to apply in fractional units between -1 and
139 * +1.
140 * \param Slot Select which gains are applied by selecting the slot. Use the
141 * configuration api to set the gain values for the selected slot
142 * before enabling this feature. Slot must be within [0,2].
143 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
144 * output is zero (or within deadband). Set
145 * to false to use the NeutralMode
146 * configuration setting (default). This flag
147 * exists to provide the fundamental behavior
148 * of this control when output is zero, which
149 * is to provide 0V to the motor.
150 * \param LimitForwardMotion Set to true to force forward limiting. This
151 * allows users to use other limit switch sensors
152 * connected to robot controller. This also allows
153 * use of active sensors that require external
154 * power.
155 * \param LimitReverseMotion Set to true to force reverse limiting. This
156 * allows users to use other limit switch sensors
157 * connected to robot controller. This also allows
158 * use of active sensors that require external
159 * power.
160 */
161 MotionMagicDutyCycle(units::angle::turn_t Position, bool EnableFOC = true, units::dimensionless::scalar_t FeedForward = 0.0, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicDutyCycle"},
162 Position{std::move(Position)},
163 EnableFOC{std::move(EnableFOC)},
164 FeedForward{std::move(FeedForward)},
165 Slot{std::move(Slot)},
169 {}
170
171 /**
172 * \brief Modifies this Control Request's Position parameter and returns itself for
173 * method-chaining and easier to use request API.
174 * \param newPosition Parameter to modify
175 * \returns Itself
176 */
177 MotionMagicDutyCycle& 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 * \param newEnableFOC Parameter to modify
187 * \returns Itself
188 */
190 {
191 EnableFOC = std::move(newEnableFOC);
192 return *this;
193 }
194
195 /**
196 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
197 * method-chaining and easier to use request API.
198 * \param newFeedForward Parameter to modify
199 * \returns Itself
200 */
201 MotionMagicDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
202 {
203 FeedForward = std::move(newFeedForward);
204 return *this;
205 }
206
207 /**
208 * \brief Modifies this Control Request's Slot parameter and returns itself for
209 * method-chaining and easier to use request API.
210 * \param newSlot Parameter to modify
211 * \returns Itself
212 */
214 {
215 Slot = std::move(newSlot);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
221 * method-chaining and easier to use request API.
222 * \param newOverrideBrakeDurNeutral Parameter to modify
223 * \returns Itself
224 */
225 MotionMagicDutyCycle& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
226 {
227 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
228 return *this;
229 }
230
231 /**
232 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
233 * method-chaining and easier to use request API.
234 * \param newLimitForwardMotion Parameter to modify
235 * \returns Itself
236 */
237 MotionMagicDutyCycle& WithLimitForwardMotion(bool newLimitForwardMotion)
238 {
239 LimitForwardMotion = std::move(newLimitForwardMotion);
240 return *this;
241 }
242
243 /**
244 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
245 * method-chaining and easier to use request API.
246 * \param newLimitReverseMotion Parameter to modify
247 * \returns Itself
248 */
249 MotionMagicDutyCycle& WithLimitReverseMotion(bool newLimitReverseMotion)
250 {
251 LimitReverseMotion = std::move(newLimitReverseMotion);
252 return *this;
253 }
254 /**
255 * \brief Sets the period at which this control will update at.
256 * This is designated in Hertz, with a minimum of 20 Hz
257 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
258 *
259 * If this field is set to 0 Hz, the control request will
260 * be sent immediately as a one-shot frame. This may be useful
261 * for advanced applications that require outputs to be
262 * synchronized with data acquisition. In this case, we
263 * recommend not exceeding 50 ms between control calls.
264 *
265 * \param newUpdateFreqHz Parameter to modify
266 * \returns Itself
267 */
268 MotionMagicDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
269 {
270 UpdateFreqHz = newUpdateFreqHz;
271 return *this;
272 }
273 /**
274 * Returns a string representation of the object.
275 *
276 * \returns a string representation of the object.
277 */
278 std::string ToString() const override
279 {
280 std::stringstream ss;
281 ss << "class: MotionMagicDutyCycle" << std::endl;
282 ss << "Position: " << Position.to<double>() << std::endl;
283 ss << "EnableFOC: " << EnableFOC << std::endl;
284 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
285 ss << "Slot: " << Slot << std::endl;
286 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
287 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
288 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
289 return ss.str();
290 }
291
292 /**
293 * \brief Gets information about this control request.
294 *
295 * \returns Map of control parameter names and corresponding applied values
296 */
297 std::map<std::string, std::string> GetControlInfo() const override
298 {
299 std::map<std::string, std::string> controlInfo;
300 std::stringstream ss;
301 controlInfo["Name"] = GetName();
302 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
303 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
304 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
305 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
306 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
307 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
308 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
309 return controlInfo;
310 }
311};
312
313}
314}
315}
316
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicDutyCycle.hpp:34
MotionMagicDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicDutyCycle.hpp:225
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicDutyCycle.hpp:78
MotionMagicDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicDutyCycle.hpp:249
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:201
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicDutyCycle.hpp:278
MotionMagicDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicDutyCycle.hpp:213
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: MotionMagicDutyCycle.hpp:85
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicDutyCycle.hpp:297
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: MotionMagicDutyCycle.hpp:72
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicDutyCycle.hpp:57
MotionMagicDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicDutyCycle.hpp:237
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicDutyCycle.hpp:91
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: MotionMagicDutyCycle.hpp:68
MotionMagicDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicDutyCycle.hpp:189
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicDutyCycle.hpp:97
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicDutyCycle.hpp:110
MotionMagicDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicDutyCycle.hpp:268
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:177
MotionMagicDutyCycle(units::angle::turn_t Position, bool EnableFOC=true, units::dimensionless::scalar_t FeedForward=0.0, int Slot=0, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicDutyCycle.hpp:161
Definition: RcManualEvent.hpp:12