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