CTRE Phoenix 6 C++ 24.2.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. FOC
61 * improves motor performance by leveraging torque (current) control. However,
62 * this may be inconvenient for applications that require specifying duty cycle
63 * or voltage. CTR-Electronics has developed a hybrid method that combines the
64 * performances gains of FOC while still allowing applications to provide duty
65 * cycle or voltage demand. This not to be confused with simple sinusoidal
66 * control or phase voltage control which lacks the performance gains.
67 */
69 /**
70 * Select which gains are applied to the primary controller by selecting the
71 * slot. Use the configuration api to set the gain values for the selected slot
72 * before enabling this feature. Slot must be within [0,2].
73 */
75 /**
76 * Select which gains are applied to the differential controller by selecting
77 * the slot. Use the configuration api to set the gain values for the selected
78 * slot before enabling this feature. Slot must be within [0,2].
79 */
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 a differential position setpoint.
116 *
117 * \details This control mode will set the motor's velocity setpoint to the
118 * velocity specified by the user. It will also set the motor's
119 * differential position setpoint to the specified position.
120 *
121 * \param TargetVelocity Average velocity to drive toward in rotations per
122 * second.
123 * \param DifferentialPosition Differential position to drive toward in
124 * rotations.
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 TargetSlot Select which gains are applied to the primary controller
138 * by selecting the slot. Use the configuration api to set
139 * the gain values for the selected slot before enabling
140 * this feature. Slot must be within [0,2].
141 * \param DifferentialSlot Select which gains are applied to the differential
142 * controller by selecting the slot. Use the
143 * configuration api to set the gain values for the
144 * selected slot before enabling this feature. Slot
145 * must be within [0,2].
146 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
147 * output is zero (or within deadband). Set
148 * to false to use the NeutralMode
149 * configuration setting (default). This flag
150 * exists to provide the fundamental behavior
151 * of this control when output is zero, which
152 * is to provide 0V to the motor.
153 * \param LimitForwardMotion Set to true to force forward 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 * \param LimitReverseMotion Set to true to force reverse 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 */
164 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"},
165 TargetVelocity{std::move(TargetVelocity)},
167 EnableFOC{std::move(EnableFOC)},
168 TargetSlot{std::move(TargetSlot)},
173 {}
174
175 /**
176 * \brief Modifies this Control Request's TargetVelocity parameter and returns itself for
177 * method-chaining and easier to use request API.
178 * \param newTargetVelocity Parameter to modify
179 * \returns Itself
180 */
181 DifferentialVelocityVoltage& WithTargetVelocity(units::angular_velocity::turns_per_second_t newTargetVelocity)
182 {
183 TargetVelocity = std::move(newTargetVelocity);
184 return *this;
185 }
186
187 /**
188 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
189 * method-chaining and easier to use request API.
190 * \param newDifferentialPosition Parameter to modify
191 * \returns Itself
192 */
193 DifferentialVelocityVoltage& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
194 {
195 DifferentialPosition = std::move(newDifferentialPosition);
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 * \param newEnableFOC Parameter to modify
203 * \returns Itself
204 */
206 {
207 EnableFOC = std::move(newEnableFOC);
208 return *this;
209 }
210
211 /**
212 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
213 * method-chaining and easier to use request API.
214 * \param newTargetSlot Parameter to modify
215 * \returns Itself
216 */
218 {
219 TargetSlot = std::move(newTargetSlot);
220 return *this;
221 }
222
223 /**
224 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
225 * method-chaining and easier to use request API.
226 * \param newDifferentialSlot Parameter to modify
227 * \returns Itself
228 */
230 {
231 DifferentialSlot = std::move(newDifferentialSlot);
232 return *this;
233 }
234
235 /**
236 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
237 * method-chaining and easier to use request API.
238 * \param newOverrideBrakeDurNeutral Parameter to modify
239 * \returns Itself
240 */
242 {
243 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
244 return *this;
245 }
246
247 /**
248 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
249 * method-chaining and easier to use request API.
250 * \param newLimitForwardMotion Parameter to modify
251 * \returns Itself
252 */
254 {
255 LimitForwardMotion = std::move(newLimitForwardMotion);
256 return *this;
257 }
258
259 /**
260 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
261 * method-chaining and easier to use request API.
262 * \param newLimitReverseMotion Parameter to modify
263 * \returns Itself
264 */
266 {
267 LimitReverseMotion = std::move(newLimitReverseMotion);
268 return *this;
269 }
270 /**
271 * \brief Sets the period at which this control will update at.
272 * This is designated in Hertz, with a minimum of 20 Hz
273 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
274 *
275 * If this field is set to 0 Hz, the control request will
276 * be sent immediately as a one-shot frame. This may be useful
277 * for advanced applications that require outputs to be
278 * synchronized with data acquisition. In this case, we
279 * recommend not exceeding 50 ms between control calls.
280 *
281 * \param newUpdateFreqHz Parameter to modify
282 * \returns Itself
283 */
284 DifferentialVelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
285 {
286 UpdateFreqHz = newUpdateFreqHz;
287 return *this;
288 }
289 /**
290 * Returns a string representation of the object.
291 *
292 * \returns a string representation of the object.
293 */
294 std::string ToString() const override
295 {
296 std::stringstream ss;
297 ss << "class: DifferentialVelocityVoltage" << std::endl;
298 ss << "TargetVelocity: " << TargetVelocity.to<double>() << std::endl;
299 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
300 ss << "EnableFOC: " << EnableFOC << std::endl;
301 ss << "TargetSlot: " << TargetSlot << std::endl;
302 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
303 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
304 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
305 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
306 return ss.str();
307 }
308
309 /**
310 * \brief Gets information about this control request.
311 *
312 * \returns Map of control parameter names and corresponding applied values
313 */
314 std::map<std::string, std::string> GetControlInfo() const override
315 {
316 std::map<std::string, std::string> controlInfo;
317 std::stringstream ss;
318 controlInfo["Name"] = GetName();
319 ss << TargetVelocity.to<double>(); controlInfo["TargetVelocity"] = ss.str(); ss.str(std::string{});
320 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
321 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
322 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
323 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
324 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
325 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
326 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
327 return controlInfo;
328 }
329};
330
331}
332}
333}
334
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:265
DifferentialVelocityVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialVelocityVoltage.hpp:253
DifferentialVelocityVoltage & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialVelocityVoltage.hpp:217
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialVelocityVoltage.hpp:80
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialVelocityVoltage.hpp:314
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:112
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialVelocityVoltage.hpp:68
DifferentialVelocityVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialVelocityVoltage.hpp:241
DifferentialVelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:284
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:181
DifferentialVelocityVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialVelocityVoltage.hpp:193
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialVelocityVoltage.hpp:294
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialVelocityVoltage.hpp:87
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DifferentialVelocityVoltage.hpp:93
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:99
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:164
DifferentialVelocityVoltage & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialVelocityVoltage.hpp:229
DifferentialVelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialVelocityVoltage.hpp:205
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialVelocityVoltage.hpp:74
Definition: RcManualEvent.hpp:12