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