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