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