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