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