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