CTRE Phoenix 6 C++ 24.3.0
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, bool cancelOtherRequests, 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
47 return c_ctre_phoenix6_RequestControlVelocityVoltage(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 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 /**
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 voltage 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 volts
141 * \param Slot Select which gains are applied by selecting the slot. Use the
142 * configuration api to set the gain values for the selected slot
143 * before enabling this feature. Slot must be within [0,2].
144 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
145 * output is zero (or within deadband). Set
146 * to false to use the NeutralMode
147 * configuration setting (default). This flag
148 * exists to provide the fundamental behavior
149 * of this control when output is zero, which
150 * is to provide 0V to the motor.
151 * \param LimitForwardMotion Set to true to force forward limiting. This
152 * allows users to use other limit switch sensors
153 * connected to robot controller. This also allows
154 * use of active sensors that require external
155 * power.
156 * \param LimitReverseMotion Set to true to force reverse limiting. This
157 * allows users to use other limit switch sensors
158 * connected to robot controller. This also allows
159 * use of active sensors that require external
160 * power.
161 */
162 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) : ControlRequest{"VelocityVoltage"},
163 Velocity{std::move(Velocity)},
164 Acceleration{std::move(Acceleration)},
165 EnableFOC{std::move(EnableFOC)},
166 FeedForward{std::move(FeedForward)},
167 Slot{std::move(Slot)},
171 {}
172
173 /**
174 * \brief Modifies this Control Request's Velocity parameter and returns itself for
175 * method-chaining and easier to use request API.
176 *
177 * Velocity to drive toward in rotations per second.
178 *
179 * \param newVelocity Parameter to modify
180 * \returns Itself
181 */
182 VelocityVoltage& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
183 {
184 Velocity = std::move(newVelocity);
185 return *this;
186 }
187
188 /**
189 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
190 * method-chaining and easier to use request API.
191 *
192 * Acceleration to drive toward in rotations per second squared. This is
193 * typically used for motion profiles generated by the robot program.
194 *
195 * \param newAcceleration Parameter to modify
196 * \returns Itself
197 */
198 VelocityVoltage& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
199 {
200 Acceleration = std::move(newAcceleration);
201 return *this;
202 }
203
204 /**
205 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
206 * method-chaining and easier to use request API.
207 *
208 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
209 * peak power by ~15%. Set to false to use trapezoidal commutation.
210 *
211 * FOC improves motor performance by leveraging torque (current) control.
212 * However, this may be inconvenient for applications that require specifying
213 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
214 * combines the performances gains of FOC while still allowing applications to
215 * provide duty cycle or voltage demand. This not to be confused with simple
216 * sinusoidal control or phase voltage control which lacks the performance
217 * gains.
218 *
219 * \param newEnableFOC Parameter to modify
220 * \returns Itself
221 */
222 VelocityVoltage& WithEnableFOC(bool newEnableFOC)
223 {
224 EnableFOC = std::move(newEnableFOC);
225 return *this;
226 }
227
228 /**
229 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
230 * method-chaining and easier to use request API.
231 *
232 * Feedforward to apply in volts
233 *
234 * \param newFeedForward Parameter to modify
235 * \returns Itself
236 */
237 VelocityVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
238 {
239 FeedForward = std::move(newFeedForward);
240 return *this;
241 }
242
243 /**
244 * \brief Modifies this Control Request's Slot parameter and returns itself for
245 * method-chaining and easier to use request API.
246 *
247 * Select which gains are applied by selecting the slot. Use the configuration
248 * api to set the gain values for the selected slot before enabling this
249 * feature. Slot must be within [0,2].
250 *
251 * \param newSlot Parameter to modify
252 * \returns Itself
253 */
255 {
256 Slot = std::move(newSlot);
257 return *this;
258 }
259
260 /**
261 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
262 * method-chaining and easier to use request API.
263 *
264 * Set to true to static-brake the rotor when output is zero (or within
265 * deadband). Set to false to use the NeutralMode configuration setting
266 * (default). This flag exists to provide the fundamental behavior of this
267 * control when output is zero, which is to provide 0V to the motor.
268 *
269 * \param newOverrideBrakeDurNeutral Parameter to modify
270 * \returns Itself
271 */
272 VelocityVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
273 {
274 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
275 return *this;
276 }
277
278 /**
279 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
280 * method-chaining and easier to use request API.
281 *
282 * Set to true to force forward limiting. This allows users to use other limit
283 * switch sensors connected to robot controller. This also allows use of active
284 * sensors that require external power.
285 *
286 * \param newLimitForwardMotion Parameter to modify
287 * \returns Itself
288 */
289 VelocityVoltage& WithLimitForwardMotion(bool newLimitForwardMotion)
290 {
291 LimitForwardMotion = std::move(newLimitForwardMotion);
292 return *this;
293 }
294
295 /**
296 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
297 * method-chaining and easier to use request API.
298 *
299 * Set to true to force reverse limiting. This allows users to use other limit
300 * switch sensors connected to robot controller. This also allows use of active
301 * sensors that require external power.
302 *
303 * \param newLimitReverseMotion Parameter to modify
304 * \returns Itself
305 */
306 VelocityVoltage& WithLimitReverseMotion(bool newLimitReverseMotion)
307 {
308 LimitReverseMotion = std::move(newLimitReverseMotion);
309 return *this;
310 }
311 /**
312 * \brief Sets the period at which this control will update at.
313 * This is designated in Hertz, with a minimum of 20 Hz
314 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
315 *
316 * If this field is set to 0 Hz, the control request will
317 * be sent immediately as a one-shot frame. This may be useful
318 * for advanced applications that require outputs to be
319 * synchronized with data acquisition. In this case, we
320 * recommend not exceeding 50 ms between control calls.
321 *
322 * \param newUpdateFreqHz Parameter to modify
323 * \returns Itself
324 */
325 VelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
326 {
327 UpdateFreqHz = newUpdateFreqHz;
328 return *this;
329 }
330 /**
331 * Returns a string representation of the object.
332 *
333 * \returns a string representation of the object.
334 */
335 std::string ToString() const override
336 {
337 std::stringstream ss;
338 ss << "class: VelocityVoltage" << std::endl;
339 ss << "Velocity: " << Velocity.to<double>() << std::endl;
340 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
341 ss << "EnableFOC: " << EnableFOC << std::endl;
342 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
343 ss << "Slot: " << Slot << std::endl;
344 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
345 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
346 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
347 return ss.str();
348 }
349
350 /**
351 * \brief Gets information about this control request.
352 *
353 * \returns Map of control parameter names and corresponding applied values
354 */
355 std::map<std::string, std::string> GetControlInfo() const override
356 {
357 std::map<std::string, std::string> controlInfo;
358 std::stringstream ss;
359 controlInfo["Name"] = GetName();
360 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
361 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
362 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
363 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
364 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
365 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
366 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
367 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
368 return controlInfo;
369 }
370};
371
372}
373}
374}
375
CTREXPORT int c_ctre_phoenix6_RequestControlVelocityVoltage(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 voltage feedforward.
Definition: VelocityVoltage.hpp:31
std::string ToString() const override
Returns a string representation of the object.
Definition: VelocityVoltage.hpp:335
VelocityVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: VelocityVoltage.hpp:272
VelocityVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: VelocityVoltage.hpp:289
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VelocityVoltage.hpp:114
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:182
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:237
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VelocityVoltage.hpp:355
VelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: VelocityVoltage.hpp:222
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:254
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:306
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: VelocityVoltage.hpp:72
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:198
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)
Request PID to target velocity with voltage feedforward.
Definition: VelocityVoltage.hpp:162
VelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VelocityVoltage.hpp:325
Definition: string_util.hpp:15