CTRE Phoenix 6 C++ 24.3.0
DifferentialVelocityVoltage.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/angle.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 a differential position setpoint.
25 *
26 * This control mode will set the motor's velocity setpoint to the velocity specified by the user. It will
27 * also set the motor's differential position setpoint to the specified position.
28 */
30{
31 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
32 {
33 if (req.get() != this)
34 {
35 auto const reqCast = dynamic_cast<DifferentialVelocityVoltage *>(req.get());
36 if (reqCast != nullptr)
37 {
38 *reqCast = *this;
39 }
40 else
41 {
42 req = std::make_shared<DifferentialVelocityVoltage>(*this);
43 }
44 }
45
47 }
48
49public:
50 /**
51 * Average velocity to drive toward in rotations per second.
52 */
53 units::angular_velocity::turns_per_second_t TargetVelocity;
54 /**
55 * Differential position to drive toward in rotations.
56 */
57 units::angle::turn_t DifferentialPosition;
58 /**
59 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
60 * peak power by ~15%. Set to false to use trapezoidal commutation.
61 *
62 * FOC improves motor performance by leveraging torque (current) control.
63 * However, this may be inconvenient for applications that require specifying
64 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
65 * combines the performances gains of FOC while still allowing applications to
66 * provide duty cycle or voltage demand. This not to be confused with simple
67 * sinusoidal control or phase voltage control which lacks the performance
68 * gains.
69 */
71 /**
72 * Select which gains are applied to the primary controller by selecting the
73 * slot. Use the configuration api to set the gain values for the selected slot
74 * before enabling this feature. Slot must be within [0,2].
75 */
77 /**
78 * Select which gains are applied to the differential controller by selecting
79 * the slot. Use the configuration api to set the gain values for the selected
80 * slot before enabling this feature. Slot must be within [0,2].
81 */
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 a differential position setpoint.
118 *
119 * \details This control mode will set the motor's velocity setpoint to the
120 * velocity specified by the user. It will also set the motor's
121 * differential position setpoint to the specified position.
122 *
123 * \param TargetVelocity Average velocity to drive toward in rotations per
124 * second.
125 * \param DifferentialPosition Differential position to drive toward in
126 * rotations.
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 TargetSlot Select which gains are applied to the primary controller
141 * by selecting the slot. Use the configuration api to set
142 * the gain values for the selected slot before enabling
143 * this feature. Slot must be within [0,2].
144 * \param DifferentialSlot Select which gains are applied to the differential
145 * controller by selecting the slot. Use the
146 * configuration api to set the gain values for the
147 * selected slot before enabling this feature. Slot
148 * must be within [0,2].
149 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
150 * output is zero (or within deadband). Set
151 * to false to use the NeutralMode
152 * configuration setting (default). This flag
153 * exists to provide the fundamental behavior
154 * of this control when output is zero, which
155 * is to provide 0V to the motor.
156 * \param LimitForwardMotion Set to true to force forward 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 * \param LimitReverseMotion Set to true to force reverse limiting. This
162 * allows users to use other limit switch sensors
163 * connected to robot controller. This also allows
164 * use of active sensors that require external
165 * power.
166 */
167 DifferentialVelocityVoltage(units::angular_velocity::turns_per_second_t TargetVelocity, units::angle::turn_t DifferentialPosition, bool EnableFOC = true, int TargetSlot = 0, int DifferentialSlot = 1, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"DifferentialVelocityVoltage"},
168 TargetVelocity{std::move(TargetVelocity)},
170 EnableFOC{std::move(EnableFOC)},
171 TargetSlot{std::move(TargetSlot)},
176 {}
177
178 /**
179 * \brief Modifies this Control Request's TargetVelocity parameter and returns itself for
180 * method-chaining and easier to use request API.
181 *
182 * Average velocity to drive toward in rotations per second.
183 *
184 * \param newTargetVelocity Parameter to modify
185 * \returns Itself
186 */
187 DifferentialVelocityVoltage& WithTargetVelocity(units::angular_velocity::turns_per_second_t newTargetVelocity)
188 {
189 TargetVelocity = std::move(newTargetVelocity);
190 return *this;
191 }
192
193 /**
194 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
195 * method-chaining and easier to use request API.
196 *
197 * Differential position to drive toward in rotations.
198 *
199 * \param newDifferentialPosition Parameter to modify
200 * \returns Itself
201 */
202 DifferentialVelocityVoltage& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
203 {
204 DifferentialPosition = std::move(newDifferentialPosition);
205 return *this;
206 }
207
208 /**
209 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
210 * method-chaining and easier to use request API.
211 *
212 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
213 * peak power by ~15%. Set to false to use trapezoidal commutation.
214 *
215 * FOC improves motor performance by leveraging torque (current) control.
216 * However, this may be inconvenient for applications that require specifying
217 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
218 * combines the performances gains of FOC while still allowing applications to
219 * provide duty cycle or voltage demand. This not to be confused with simple
220 * sinusoidal control or phase voltage control which lacks the performance
221 * gains.
222 *
223 * \param newEnableFOC Parameter to modify
224 * \returns Itself
225 */
227 {
228 EnableFOC = std::move(newEnableFOC);
229 return *this;
230 }
231
232 /**
233 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
234 * method-chaining and easier to use request API.
235 *
236 * Select which gains are applied to the primary controller by selecting the
237 * slot. Use the configuration api to set the gain values for the selected slot
238 * before enabling this feature. Slot must be within [0,2].
239 *
240 * \param newTargetSlot Parameter to modify
241 * \returns Itself
242 */
244 {
245 TargetSlot = std::move(newTargetSlot);
246 return *this;
247 }
248
249 /**
250 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
251 * method-chaining and easier to use request API.
252 *
253 * Select which gains are applied to the differential controller by selecting
254 * the slot. Use the configuration api to set the gain values for the selected
255 * slot before enabling this feature. Slot must be within [0,2].
256 *
257 * \param newDifferentialSlot Parameter to modify
258 * \returns Itself
259 */
261 {
262 DifferentialSlot = std::move(newDifferentialSlot);
263 return *this;
264 }
265
266 /**
267 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
268 * method-chaining and easier to use request API.
269 *
270 * Set to true to static-brake the rotor when output is zero (or within
271 * deadband). Set to false to use the NeutralMode configuration setting
272 * (default). This flag exists to provide the fundamental behavior of this
273 * control when output is zero, which is to provide 0V to the motor.
274 *
275 * \param newOverrideBrakeDurNeutral Parameter to modify
276 * \returns Itself
277 */
279 {
280 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
281 return *this;
282 }
283
284 /**
285 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
286 * method-chaining and easier to use request API.
287 *
288 * Set to true to force forward limiting. This allows users to use other limit
289 * switch sensors connected to robot controller. This also allows use of active
290 * sensors that require external power.
291 *
292 * \param newLimitForwardMotion Parameter to modify
293 * \returns Itself
294 */
296 {
297 LimitForwardMotion = std::move(newLimitForwardMotion);
298 return *this;
299 }
300
301 /**
302 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
303 * method-chaining and easier to use request API.
304 *
305 * Set to true to force reverse limiting. This allows users to use other limit
306 * switch sensors connected to robot controller. This also allows use of active
307 * sensors that require external power.
308 *
309 * \param newLimitReverseMotion Parameter to modify
310 * \returns Itself
311 */
313 {
314 LimitReverseMotion = std::move(newLimitReverseMotion);
315 return *this;
316 }
317 /**
318 * \brief Sets the period at which this control will update at.
319 * This is designated in Hertz, with a minimum of 20 Hz
320 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
321 *
322 * If this field is set to 0 Hz, the control request will
323 * be sent immediately as a one-shot frame. This may be useful
324 * for advanced applications that require outputs to be
325 * synchronized with data acquisition. In this case, we
326 * recommend not exceeding 50 ms between control calls.
327 *
328 * \param newUpdateFreqHz Parameter to modify
329 * \returns Itself
330 */
331 DifferentialVelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
332 {
333 UpdateFreqHz = newUpdateFreqHz;
334 return *this;
335 }
336 /**
337 * Returns a string representation of the object.
338 *
339 * \returns a string representation of the object.
340 */
341 std::string ToString() const override
342 {
343 std::stringstream ss;
344 ss << "class: DifferentialVelocityVoltage" << std::endl;
345 ss << "TargetVelocity: " << TargetVelocity.to<double>() << std::endl;
346 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
347 ss << "EnableFOC: " << EnableFOC << std::endl;
348 ss << "TargetSlot: " << TargetSlot << std::endl;
349 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
350 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
351 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
352 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
353 return ss.str();
354 }
355
356 /**
357 * \brief Gets information about this control request.
358 *
359 * \returns Map of control parameter names and corresponding applied values
360 */
361 std::map<std::string, std::string> GetControlInfo() const override
362 {
363 std::map<std::string, std::string> controlInfo;
364 std::stringstream ss;
365 controlInfo["Name"] = GetName();
366 ss << TargetVelocity.to<double>(); controlInfo["TargetVelocity"] = ss.str(); ss.str(std::string{});
367 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
368 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
369 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
370 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
371 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
372 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
373 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
374 return controlInfo;
375 }
376};
377
378}
379}
380}
381
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialVelocityVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double TargetVelocity, double DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, 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 a differential position setpoint.
Definition: DifferentialVelocityVoltage.hpp:30
DifferentialVelocityVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DifferentialVelocityVoltage.hpp:312
DifferentialVelocityVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialVelocityVoltage.hpp:295
DifferentialVelocityVoltage & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialVelocityVoltage.hpp:243
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialVelocityVoltage.hpp:82
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialVelocityVoltage.hpp:361
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:114
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialVelocityVoltage.hpp:70
DifferentialVelocityVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialVelocityVoltage.hpp:278
DifferentialVelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:331
DifferentialVelocityVoltage & WithTargetVelocity(units::angular_velocity::turns_per_second_t newTargetVelocity)
Modifies this Control Request's TargetVelocity parameter and returns itself for method-chaining and e...
Definition: DifferentialVelocityVoltage.hpp:187
DifferentialVelocityVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialVelocityVoltage.hpp:202
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialVelocityVoltage.hpp:341
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialVelocityVoltage.hpp:89
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DifferentialVelocityVoltage.hpp:95
units::angular_velocity::turns_per_second_t TargetVelocity
Average velocity to drive toward in rotations per second.
Definition: DifferentialVelocityVoltage.hpp:53
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: DifferentialVelocityVoltage.hpp:101
units::angle::turn_t DifferentialPosition
Differential position to drive toward in rotations.
Definition: DifferentialVelocityVoltage.hpp:57
DifferentialVelocityVoltage(units::angular_velocity::turns_per_second_t TargetVelocity, units::angle::turn_t DifferentialPosition, bool EnableFOC=true, int TargetSlot=0, int DifferentialSlot=1, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Request PID to target velocity with a differential position setpoint.
Definition: DifferentialVelocityVoltage.hpp:167
DifferentialVelocityVoltage & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialVelocityVoltage.hpp:260
DifferentialVelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialVelocityVoltage.hpp:226
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialVelocityVoltage.hpp:76
Definition: string_util.hpp:15