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