CTRE Phoenix 6 C++ 24.2.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. FOC
63 * improves motor performance by leveraging torque (current) control. However,
64 * this may be inconvenient for applications that require specifying duty cycle
65 * or voltage. CTR-Electronics has developed a hybrid method that combines the
66 * performances gains of FOC while still allowing applications to provide duty
67 * cycle or voltage demand. This not to be confused with simple sinusoidal
68 * control or phase voltage control which lacks the performance gains.
69 */
71 /**
72 * Feedforward to apply in volts
73 */
74 units::voltage::volt_t FeedForward;
75 /**
76 * Select which gains are applied by selecting the slot. Use the configuration
77 * api to set the gain values for the selected slot before enabling this
78 * feature. Slot must be within [0,2].
79 */
80 int Slot;
81 /**
82 * Set to true to static-brake the rotor when output is zero (or within
83 * deadband). Set to false to use the NeutralMode configuration setting
84 * (default). This flag exists to provide the fundamental behavior of this
85 * control when output is zero, which is to provide 0V to the motor.
86 */
88 /**
89 * Set to true to force forward limiting. This allows users to use other limit
90 * switch sensors connected to robot controller. This also allows use of active
91 * sensors that require external power.
92 */
94 /**
95 * Set to true to force reverse limiting. This allows users to use other limit
96 * switch sensors connected to robot controller. This also allows use of active
97 * sensors that require external power.
98 */
100
101 /**
102 * \brief The period at which this control will update at.
103 * This is designated in Hertz, with a minimum of 20 Hz
104 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
105 *
106 * If this field is set to 0 Hz, the control request will
107 * be sent immediately as a one-shot frame. This may be useful
108 * for advanced applications that require outputs to be
109 * synchronized with data acquisition. In this case, we
110 * recommend not exceeding 50 ms between control calls.
111 */
112 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
113
114 /**
115 * \brief Request PID to target velocity with voltage feedforward.
116 *
117 * \details This control mode will set the motor's velocity setpoint to the
118 * velocity specified by the user. In addition, it will apply an
119 * additional voltage as an arbitrary feedforward value.
120 *
121 * \param Velocity Velocity to drive toward in rotations per second.
122 * \param Acceleration Acceleration to drive toward in rotations per second
123 * squared. This is typically used for motion profiles
124 * generated by the robot program.
125 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
126 * Pro), which increases peak power by ~15%. Set to false to
127 * use trapezoidal commutation. FOC improves motor
128 * performance by leveraging torque (current) control.
129 * However, this may be inconvenient for applications that
130 * require specifying duty cycle or voltage.
131 * CTR-Electronics has developed a hybrid method that
132 * combines the performances gains of FOC while still
133 * allowing applications to provide duty cycle or voltage
134 * demand. This not to be confused with simple sinusoidal
135 * control or phase voltage control which lacks the
136 * performance gains.
137 * \param FeedForward Feedforward to apply in volts
138 * \param Slot Select which gains are applied by selecting the slot. Use the
139 * configuration api to set the gain values for the selected slot
140 * before enabling this feature. Slot must be within [0,2].
141 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
142 * output is zero (or within deadband). Set
143 * to false to use the NeutralMode
144 * configuration setting (default). This flag
145 * exists to provide the fundamental behavior
146 * of this control when output is zero, which
147 * is to provide 0V to the motor.
148 * \param LimitForwardMotion Set to true to force forward limiting. This
149 * allows users to use other limit switch sensors
150 * connected to robot controller. This also allows
151 * use of active sensors that require external
152 * power.
153 * \param LimitReverseMotion Set to true to force reverse limiting. This
154 * allows users to use other limit switch sensors
155 * connected to robot controller. This also allows
156 * use of active sensors that require external
157 * power.
158 */
159 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"},
160 Velocity{std::move(Velocity)},
161 Acceleration{std::move(Acceleration)},
162 EnableFOC{std::move(EnableFOC)},
163 FeedForward{std::move(FeedForward)},
164 Slot{std::move(Slot)},
168 {}
169
170 /**
171 * \brief Modifies this Control Request's Velocity parameter and returns itself for
172 * method-chaining and easier to use request API.
173 * \param newVelocity Parameter to modify
174 * \returns Itself
175 */
176 VelocityVoltage& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
177 {
178 Velocity = std::move(newVelocity);
179 return *this;
180 }
181
182 /**
183 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
184 * method-chaining and easier to use request API.
185 * \param newAcceleration Parameter to modify
186 * \returns Itself
187 */
188 VelocityVoltage& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
189 {
190 Acceleration = std::move(newAcceleration);
191 return *this;
192 }
193
194 /**
195 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
196 * method-chaining and easier to use request API.
197 * \param newEnableFOC Parameter to modify
198 * \returns Itself
199 */
200 VelocityVoltage& WithEnableFOC(bool newEnableFOC)
201 {
202 EnableFOC = std::move(newEnableFOC);
203 return *this;
204 }
205
206 /**
207 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
208 * method-chaining and easier to use request API.
209 * \param newFeedForward Parameter to modify
210 * \returns Itself
211 */
212 VelocityVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
213 {
214 FeedForward = std::move(newFeedForward);
215 return *this;
216 }
217
218 /**
219 * \brief Modifies this Control Request's Slot parameter and returns itself for
220 * method-chaining and easier to use request API.
221 * \param newSlot Parameter to modify
222 * \returns Itself
223 */
225 {
226 Slot = std::move(newSlot);
227 return *this;
228 }
229
230 /**
231 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
232 * method-chaining and easier to use request API.
233 * \param newOverrideBrakeDurNeutral Parameter to modify
234 * \returns Itself
235 */
236 VelocityVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
237 {
238 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
239 return *this;
240 }
241
242 /**
243 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
244 * method-chaining and easier to use request API.
245 * \param newLimitForwardMotion Parameter to modify
246 * \returns Itself
247 */
248 VelocityVoltage& WithLimitForwardMotion(bool newLimitForwardMotion)
249 {
250 LimitForwardMotion = std::move(newLimitForwardMotion);
251 return *this;
252 }
253
254 /**
255 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
256 * method-chaining and easier to use request API.
257 * \param newLimitReverseMotion Parameter to modify
258 * \returns Itself
259 */
260 VelocityVoltage& WithLimitReverseMotion(bool newLimitReverseMotion)
261 {
262 LimitReverseMotion = std::move(newLimitReverseMotion);
263 return *this;
264 }
265 /**
266 * \brief Sets the period at which this control will update at.
267 * This is designated in Hertz, with a minimum of 20 Hz
268 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
269 *
270 * If this field is set to 0 Hz, the control request will
271 * be sent immediately as a one-shot frame. This may be useful
272 * for advanced applications that require outputs to be
273 * synchronized with data acquisition. In this case, we
274 * recommend not exceeding 50 ms between control calls.
275 *
276 * \param newUpdateFreqHz Parameter to modify
277 * \returns Itself
278 */
279 VelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
280 {
281 UpdateFreqHz = newUpdateFreqHz;
282 return *this;
283 }
284 /**
285 * Returns a string representation of the object.
286 *
287 * \returns a string representation of the object.
288 */
289 std::string ToString() const override
290 {
291 std::stringstream ss;
292 ss << "class: VelocityVoltage" << std::endl;
293 ss << "Velocity: " << Velocity.to<double>() << std::endl;
294 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
295 ss << "EnableFOC: " << EnableFOC << std::endl;
296 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
297 ss << "Slot: " << Slot << std::endl;
298 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
299 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
300 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
301 return ss.str();
302 }
303
304 /**
305 * \brief Gets information about this control request.
306 *
307 * \returns Map of control parameter names and corresponding applied values
308 */
309 std::map<std::string, std::string> GetControlInfo() const override
310 {
311 std::map<std::string, std::string> controlInfo;
312 std::stringstream ss;
313 controlInfo["Name"] = GetName();
314 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
315 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
316 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
317 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
318 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
319 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
320 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
321 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
322 return controlInfo;
323 }
324};
325
326}
327}
328}
329
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:289
VelocityVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: VelocityVoltage.hpp:236
VelocityVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: VelocityVoltage.hpp:248
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VelocityVoltage.hpp:112
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:99
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: VelocityVoltage.hpp:87
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:176
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:212
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VelocityVoltage.hpp:309
VelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: VelocityVoltage.hpp:200
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:224
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: VelocityVoltage.hpp:93
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: VelocityVoltage.hpp:74
int Slot
Select which gains are applied by selecting the slot.
Definition: VelocityVoltage.hpp:80
VelocityVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: VelocityVoltage.hpp:260
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: VelocityVoltage.hpp:70
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:188
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:159
VelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VelocityVoltage.hpp:279
Definition: RcManualEvent.hpp:12