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