CTRE Phoenix 6 C++ 25.2.2
Loading...
Searching...
No Matches
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
11#include <sstream>
12#include <units/angular_velocity.h>
13#include <units/angular_acceleration.h>
14#include <units/dimensionless.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Request PID to target velocity with duty cycle feedforward.
25 *
26 * This control mode will set the motor's velocity setpoint to the velocity specified by the user. In
27 * addition, it will apply an additional voltage as an arbitrary feedforward value.
28 */
30{
31 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
32 {
33 if (req.get() != this)
34 {
35 auto const reqCast = dynamic_cast<VelocityDutyCycle *>(req.get());
36 if (reqCast != nullptr)
37 {
38 *reqCast = *this;
39 }
40 else
41 {
42 req = std::make_shared<VelocityDutyCycle>(*this);
43 }
44 }
45
47 }
48
49public:
50 /**
51 * \brief Velocity to drive toward in rotations per second.
52 *
53 * - Units: rotations per second
54 *
55 */
56 units::angular_velocity::turns_per_second_t Velocity;
57 /**
58 * \brief Acceleration to drive toward in rotations per second squared. This is
59 * typically used for motion profiles generated by the robot program.
60 *
61 * - Units: rotations per second²
62 *
63 */
64 units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq;
65 /**
66 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
67 * increases peak power by ~15%. Set to false to use trapezoidal commutation.
68 *
69 * FOC improves motor performance by leveraging torque (current) control.
70 * However, this may be inconvenient for applications that require specifying
71 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
72 * combines the performances gains of FOC while still allowing applications to
73 * provide duty cycle or voltage demand. This not to be confused with simple
74 * sinusoidal control or phase voltage control which lacks the performance
75 * gains.
76 */
77 bool EnableFOC = true;
78 /**
79 * \brief Feedforward to apply in fractional units between -1 and +1.
80 *
81 * - Units: fractional
82 *
83 */
84 units::dimensionless::scalar_t FeedForward = 0.0;
85 /**
86 * \brief Select which gains are applied by selecting the slot. Use the
87 * configuration api to set the gain values for the selected slot before
88 * enabling this feature. Slot must be within [0,2].
89 */
90 int Slot = 0;
91 /**
92 * \brief Set to true to static-brake the rotor when output is zero (or within
93 * deadband). Set to false to use the NeutralMode configuration setting
94 * (default). This flag exists to provide the fundamental behavior of this
95 * control when output is zero, which is to provide 0V to the motor.
96 */
98 /**
99 * \brief Set to true to force forward limiting. This allows users to use other
100 * limit switch sensors connected to robot controller. This also allows use of
101 * active sensors that require external power.
102 */
103 bool LimitForwardMotion = false;
104 /**
105 * \brief Set to true to force reverse limiting. This allows users to use other
106 * limit switch sensors connected to robot controller. This also allows use of
107 * active sensors that require external power.
108 */
109 bool LimitReverseMotion = false;
110 /**
111 * \brief Set to true to ignore hardware limit switches and the
112 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
113 * motion.
114 *
115 * This can be useful on mechanisms such as an intake/feeder, where a limit
116 * switch stops motion while intaking but should be ignored when feeding to a
117 * shooter.
118 *
119 * The hardware limit faults and Forward/ReverseLimit signals will still report
120 * the values of the limit switches regardless of this parameter.
121 */
123 /**
124 * \brief Set to true to delay applying this control request until a timesync
125 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
126 * nondeterministic network delays in exchange for a larger but deterministic
127 * control latency.
128 *
129 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
130 * Additionally, when this is enabled, the UpdateFreqHz of this request should
131 * be set to 0 Hz.
132 */
133 bool UseTimesync = false;
134
135 /**
136 * \brief The period at which this control will update at.
137 * This is designated in Hertz, with a minimum of 20 Hz
138 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
139 *
140 * If this field is set to 0 Hz, the control request will
141 * be sent immediately as a one-shot frame. This may be useful
142 * for advanced applications that require outputs to be
143 * synchronized with data acquisition. In this case, we
144 * recommend not exceeding 50 ms between control calls.
145 */
146 units::frequency::hertz_t UpdateFreqHz{100_Hz};
147
148 /**
149 * \brief Request PID to target velocity with duty cycle feedforward.
150 *
151 * \details This control mode will set the motor's velocity setpoint to the
152 * velocity specified by the user. In addition, it will apply an
153 * additional voltage as an arbitrary feedforward value.
154 *
155 * \param Velocity Velocity to drive toward in rotations per second.
156 */
157 VelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity) : ControlRequest{"VelocityDutyCycle"},
158 Velocity{std::move(Velocity)}
159 {}
160
161 /**
162 * \brief Modifies this Control Request's Velocity parameter and returns itself for
163 * method-chaining and easier to use request API.
164 *
165 * Velocity to drive toward in rotations per second.
166 *
167 * - Units: rotations per second
168 *
169 *
170 * \param newVelocity Parameter to modify
171 * \returns Itself
172 */
173 VelocityDutyCycle &WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
174 {
175 Velocity = std::move(newVelocity);
176 return *this;
177 }
178
179 /**
180 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
181 * method-chaining and easier to use request API.
182 *
183 * Acceleration to drive toward in rotations per second squared. This is
184 * typically used for motion profiles generated by the robot program.
185 *
186 * - Units: rotations per second²
187 *
188 *
189 * \param newAcceleration Parameter to modify
190 * \returns Itself
191 */
192 VelocityDutyCycle &WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
193 {
194 Acceleration = std::move(newAcceleration);
195 return *this;
196 }
197
198 /**
199 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
200 * method-chaining and easier to use request API.
201 *
202 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
203 * peak power by ~15%. Set to false to use trapezoidal commutation.
204 *
205 * FOC improves motor performance by leveraging torque (current) control.
206 * However, this may be inconvenient for applications that require specifying
207 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
208 * combines the performances gains of FOC while still allowing applications to
209 * provide duty cycle or voltage demand. This not to be confused with simple
210 * sinusoidal control or phase voltage control which lacks the performance
211 * gains.
212 *
213 * \param newEnableFOC Parameter to modify
214 * \returns Itself
215 */
217 {
218 EnableFOC = std::move(newEnableFOC);
219 return *this;
220 }
221
222 /**
223 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
224 * method-chaining and easier to use request API.
225 *
226 * Feedforward to apply in fractional units between -1 and +1.
227 *
228 * - Units: fractional
229 *
230 *
231 * \param newFeedForward Parameter to modify
232 * \returns Itself
233 */
234 VelocityDutyCycle &WithFeedForward(units::dimensionless::scalar_t newFeedForward)
235 {
236 FeedForward = std::move(newFeedForward);
237 return *this;
238 }
239
240 /**
241 * \brief Modifies this Control Request's Slot parameter and returns itself for
242 * method-chaining and easier to use request API.
243 *
244 * Select which gains are applied by selecting the slot. Use the configuration
245 * api to set the gain values for the selected slot before enabling this
246 * feature. Slot must be within [0,2].
247 *
248 * \param newSlot Parameter to modify
249 * \returns Itself
250 */
252 {
253 Slot = std::move(newSlot);
254 return *this;
255 }
256
257 /**
258 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
259 * method-chaining and easier to use request API.
260 *
261 * Set to true to static-brake the rotor when output is zero (or within
262 * deadband). Set to false to use the NeutralMode configuration setting
263 * (default). This flag exists to provide the fundamental behavior of this
264 * control when output is zero, which is to provide 0V to the motor.
265 *
266 * \param newOverrideBrakeDurNeutral Parameter to modify
267 * \returns Itself
268 */
269 VelocityDutyCycle &WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
270 {
271 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
272 return *this;
273 }
274
275 /**
276 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
277 * method-chaining and easier to use request API.
278 *
279 * Set to true to force forward limiting. This allows users to use other limit
280 * switch sensors connected to robot controller. This also allows use of active
281 * sensors that require external power.
282 *
283 * \param newLimitForwardMotion Parameter to modify
284 * \returns Itself
285 */
286 VelocityDutyCycle &WithLimitForwardMotion(bool newLimitForwardMotion)
287 {
288 LimitForwardMotion = std::move(newLimitForwardMotion);
289 return *this;
290 }
291
292 /**
293 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
294 * method-chaining and easier to use request API.
295 *
296 * Set to true to force reverse limiting. This allows users to use other limit
297 * switch sensors connected to robot controller. This also allows use of active
298 * sensors that require external power.
299 *
300 * \param newLimitReverseMotion Parameter to modify
301 * \returns Itself
302 */
303 VelocityDutyCycle &WithLimitReverseMotion(bool newLimitReverseMotion)
304 {
305 LimitReverseMotion = std::move(newLimitReverseMotion);
306 return *this;
307 }
308
309 /**
310 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
311 * method-chaining and easier to use request API.
312 *
313 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
314 * LimitReverseMotion parameters, instead allowing motion.
315 *
316 * This can be useful on mechanisms such as an intake/feeder, where a limit
317 * switch stops motion while intaking but should be ignored when feeding to a
318 * shooter.
319 *
320 * The hardware limit faults and Forward/ReverseLimit signals will still report
321 * the values of the limit switches regardless of this parameter.
322 *
323 * \param newIgnoreHardwareLimits Parameter to modify
324 * \returns Itself
325 */
326 VelocityDutyCycle &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
327 {
328 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
329 return *this;
330 }
331
332 /**
333 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
334 * method-chaining and easier to use request API.
335 *
336 * Set to true to delay applying this control request until a timesync boundary
337 * (requires Phoenix Pro and CANivore). This eliminates the impact of
338 * nondeterministic network delays in exchange for a larger but deterministic
339 * control latency.
340 *
341 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
342 * Additionally, when this is enabled, the UpdateFreqHz of this request should
343 * be set to 0 Hz.
344 *
345 * \param newUseTimesync Parameter to modify
346 * \returns Itself
347 */
348 VelocityDutyCycle &WithUseTimesync(bool newUseTimesync)
349 {
350 UseTimesync = std::move(newUseTimesync);
351 return *this;
352 }
353 /**
354 * \brief Sets the period at which this control will update at.
355 * This is designated in Hertz, with a minimum of 20 Hz
356 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
357 *
358 * If this field is set to 0 Hz, the control request will
359 * be sent immediately as a one-shot frame. This may be useful
360 * for advanced applications that require outputs to be
361 * synchronized with data acquisition. In this case, we
362 * recommend not exceeding 50 ms between control calls.
363 *
364 * \param newUpdateFreqHz Parameter to modify
365 * \returns Itself
366 */
367 VelocityDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
368 {
369 UpdateFreqHz = newUpdateFreqHz;
370 return *this;
371 }
372 /**
373 * \brief Returns a string representation of the object.
374 *
375 * \returns a string representation of the object.
376 */
377 std::string ToString() const override
378 {
379 std::stringstream ss;
380 ss << "Control: VelocityDutyCycle" << std::endl;
381 ss << " Velocity: " << Velocity.to<double>() << " rotations per second" << std::endl;
382 ss << " Acceleration: " << Acceleration.to<double>() << " rotations per second²" << std::endl;
383 ss << " EnableFOC: " << EnableFOC << std::endl;
384 ss << " FeedForward: " << FeedForward.to<double>() << " fractional" << std::endl;
385 ss << " Slot: " << Slot << std::endl;
386 ss << " OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
387 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
388 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
389 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
390 ss << " UseTimesync: " << UseTimesync << std::endl;
391 return ss.str();
392 }
393
394 /**
395 * \brief Gets information about this control request.
396 *
397 * \returns Map of control parameter names and corresponding applied values
398 */
399 std::map<std::string, std::string> GetControlInfo() const override
400 {
401 std::map<std::string, std::string> controlInfo;
402 std::stringstream ss;
403 controlInfo["Name"] = GetName();
404 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
405 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
406 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
407 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
408 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
409 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
410 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
411 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
412 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
413 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
414 return controlInfo;
415 }
416};
417
418}
419}
420}
421
CTREXPORT int c_ctre_phoenix6_RequestControlVelocityDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, double Velocity, double Acceleration, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion, bool IgnoreHardwareLimits, bool UseTimesync)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Request PID to target velocity with duty cycle feedforward.
Definition VelocityDutyCycle.hpp:30
int Slot
Select which gains are applied by selecting the slot.
Definition VelocityDutyCycle.hpp:90
VelocityDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition VelocityDutyCycle.hpp:251
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration to drive toward in rotations per second squared.
Definition VelocityDutyCycle.hpp:64
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:173
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition VelocityDutyCycle.hpp:97
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:234
VelocityDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition VelocityDutyCycle.hpp:286
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition VelocityDutyCycle.hpp:122
VelocityDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition VelocityDutyCycle.hpp:367
VelocityDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition VelocityDutyCycle.hpp:216
VelocityDutyCycle & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition VelocityDutyCycle.hpp:348
VelocityDutyCycle(units::angular_velocity::turns_per_second_t Velocity)
Request PID to target velocity with duty cycle feedforward.
Definition VelocityDutyCycle.hpp:157
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition VelocityDutyCycle.hpp:56
VelocityDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition VelocityDutyCycle.hpp:269
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition VelocityDutyCycle.hpp:399
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition VelocityDutyCycle.hpp:146
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:192
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition VelocityDutyCycle.hpp:84
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition VelocityDutyCycle.hpp:77
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition VelocityDutyCycle.hpp:109
VelocityDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition VelocityDutyCycle.hpp:303
std::string ToString() const override
Returns a string representation of the object.
Definition VelocityDutyCycle.hpp:377
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition VelocityDutyCycle.hpp:133
bool LimitForwardMotion
Set to true to force forward limiting.
Definition VelocityDutyCycle.hpp:103
VelocityDutyCycle & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition VelocityDutyCycle.hpp:326
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401