CTRE Phoenix 6 C++ 24.50.0-alpha-2
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, 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 * Set to true to delay applying this control request until a timesync boundary
104 * (requires Phoenix Pro and CANivore). This eliminates the impact of
105 * nondeterministic network delays in exchange for a larger but deterministic
106 * control latency.
107 */
109
110 /**
111 * \brief The period at which this control will update at.
112 * This is designated in Hertz, with a minimum of 20 Hz
113 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
114 *
115 * If this field is set to 0 Hz, the control request will
116 * be sent immediately as a one-shot frame. This may be useful
117 * for advanced applications that require outputs to be
118 * synchronized with data acquisition. In this case, we
119 * recommend not exceeding 50 ms between control calls.
120 */
121 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
122
123 /**
124 * \brief Request PID to target velocity with a differential position setpoint.
125 *
126 * \details This control mode will set the motor's velocity setpoint to the
127 * velocity specified by the user. It will also set the motor's
128 * differential position setpoint to the specified position.
129 *
130 * \param TargetVelocity Average velocity to drive toward in rotations per
131 * second.
132 * \param DifferentialPosition Differential position to drive toward in
133 * rotations.
134 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
135 * Pro), which increases peak power by ~15%. Set to false to
136 * use trapezoidal commutation.
137 *
138 * FOC improves motor performance by leveraging torque
139 * (current) control. However, this may be inconvenient for
140 * applications that require specifying duty cycle or
141 * voltage. CTR-Electronics has developed a hybrid method
142 * that combines the performances gains of FOC while still
143 * allowing applications to provide duty cycle or voltage
144 * demand. This not to be confused with simple sinusoidal
145 * control or phase voltage control which lacks the
146 * performance gains.
147 * \param TargetSlot Select which gains are applied to the primary controller
148 * by selecting the slot. Use the configuration api to set
149 * the gain values for the selected slot before enabling
150 * this feature. Slot must be within [0,2].
151 * \param DifferentialSlot Select which gains are applied to the differential
152 * controller by selecting the slot. Use the
153 * configuration api to set the gain values for the
154 * selected slot before enabling this feature. Slot
155 * must be within [0,2].
156 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
157 * output is zero (or within deadband). Set
158 * to false to use the NeutralMode
159 * configuration setting (default). This flag
160 * exists to provide the fundamental behavior
161 * of this control when output is zero, which
162 * is to provide 0V to the motor.
163 * \param LimitForwardMotion Set to true to force forward limiting. This
164 * allows users to use other limit switch sensors
165 * connected to robot controller. This also allows
166 * use of active sensors that require external
167 * power.
168 * \param LimitReverseMotion Set to true to force reverse limiting. This
169 * allows users to use other limit switch sensors
170 * connected to robot controller. This also allows
171 * use of active sensors that require external
172 * power.
173 * \param UseTimesync Set to true to delay applying this control request
174 * until a timesync boundary (requires Phoenix Pro and
175 * CANivore). This eliminates the impact of
176 * nondeterministic network delays in exchange for a
177 * larger but deterministic control latency.
178 */
179 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, bool UseTimesync = false) : ControlRequest{"DifferentialVelocityVoltage"},
182 EnableFOC{std::move(EnableFOC)},
183 TargetSlot{std::move(TargetSlot)},
189 {}
190
191 /**
192 * \brief Modifies this Control Request's TargetVelocity parameter and returns itself for
193 * method-chaining and easier to use request API.
194 *
195 * Average velocity to drive toward in rotations per second.
196 *
197 * \param newTargetVelocity Parameter to modify
198 * \returns Itself
199 */
200 DifferentialVelocityVoltage& WithTargetVelocity(units::angular_velocity::turns_per_second_t newTargetVelocity)
201 {
202 TargetVelocity = std::move(newTargetVelocity);
203 return *this;
204 }
205
206 /**
207 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
208 * method-chaining and easier to use request API.
209 *
210 * Differential position to drive toward in rotations.
211 *
212 * \param newDifferentialPosition Parameter to modify
213 * \returns Itself
214 */
215 DifferentialVelocityVoltage& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
216 {
217 DifferentialPosition = std::move(newDifferentialPosition);
218 return *this;
219 }
220
221 /**
222 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
223 * method-chaining and easier to use request API.
224 *
225 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
226 * peak power by ~15%. Set to false to use trapezoidal commutation.
227 *
228 * FOC improves motor performance by leveraging torque (current) control.
229 * However, this may be inconvenient for applications that require specifying
230 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
231 * combines the performances gains of FOC while still allowing applications to
232 * provide duty cycle or voltage demand. This not to be confused with simple
233 * sinusoidal control or phase voltage control which lacks the performance
234 * gains.
235 *
236 * \param newEnableFOC Parameter to modify
237 * \returns Itself
238 */
240 {
241 EnableFOC = std::move(newEnableFOC);
242 return *this;
243 }
244
245 /**
246 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
247 * method-chaining and easier to use request API.
248 *
249 * Select which gains are applied to the primary controller by selecting the
250 * slot. Use the configuration api to set the gain values for the selected slot
251 * before enabling this feature. Slot must be within [0,2].
252 *
253 * \param newTargetSlot Parameter to modify
254 * \returns Itself
255 */
257 {
258 TargetSlot = std::move(newTargetSlot);
259 return *this;
260 }
261
262 /**
263 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
264 * method-chaining and easier to use request API.
265 *
266 * Select which gains are applied to the differential controller by selecting
267 * the slot. Use the configuration api to set the gain values for the selected
268 * slot before enabling this feature. Slot must be within [0,2].
269 *
270 * \param newDifferentialSlot Parameter to modify
271 * \returns Itself
272 */
274 {
275 DifferentialSlot = std::move(newDifferentialSlot);
276 return *this;
277 }
278
279 /**
280 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
281 * method-chaining and easier to use request API.
282 *
283 * Set to true to static-brake the rotor when output is zero (or within
284 * deadband). Set to false to use the NeutralMode configuration setting
285 * (default). This flag exists to provide the fundamental behavior of this
286 * control when output is zero, which is to provide 0V to the motor.
287 *
288 * \param newOverrideBrakeDurNeutral Parameter to modify
289 * \returns Itself
290 */
292 {
293 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
294 return *this;
295 }
296
297 /**
298 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
299 * method-chaining and easier to use request API.
300 *
301 * Set to true to force forward limiting. This allows users to use other limit
302 * switch sensors connected to robot controller. This also allows use of active
303 * sensors that require external power.
304 *
305 * \param newLimitForwardMotion Parameter to modify
306 * \returns Itself
307 */
309 {
310 LimitForwardMotion = std::move(newLimitForwardMotion);
311 return *this;
312 }
313
314 /**
315 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
316 * method-chaining and easier to use request API.
317 *
318 * Set to true to force reverse limiting. This allows users to use other limit
319 * switch sensors connected to robot controller. This also allows use of active
320 * sensors that require external power.
321 *
322 * \param newLimitReverseMotion Parameter to modify
323 * \returns Itself
324 */
326 {
327 LimitReverseMotion = std::move(newLimitReverseMotion);
328 return *this;
329 }
330
331 /**
332 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
333 * method-chaining and easier to use request API.
334 *
335 * Set to true to delay applying this control request until a timesync boundary
336 * (requires Phoenix Pro and CANivore). This eliminates the impact of
337 * nondeterministic network delays in exchange for a larger but deterministic
338 * control latency.
339 *
340 * \param newUseTimesync Parameter to modify
341 * \returns Itself
342 */
344 {
345 UseTimesync = std::move(newUseTimesync);
346 return *this;
347 }
348 /**
349 * \brief Sets the period at which this control will update at.
350 * This is designated in Hertz, with a minimum of 20 Hz
351 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
352 *
353 * If this field is set to 0 Hz, the control request will
354 * be sent immediately as a one-shot frame. This may be useful
355 * for advanced applications that require outputs to be
356 * synchronized with data acquisition. In this case, we
357 * recommend not exceeding 50 ms between control calls.
358 *
359 * \param newUpdateFreqHz Parameter to modify
360 * \returns Itself
361 */
362 DifferentialVelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
363 {
364 UpdateFreqHz = newUpdateFreqHz;
365 return *this;
366 }
367 /**
368 * Returns a string representation of the object.
369 *
370 * \returns a string representation of the object.
371 */
372 std::string ToString() const override
373 {
374 std::stringstream ss;
375 ss << "Control: DifferentialVelocityVoltage" << std::endl;
376 ss << " TargetVelocity: " << TargetVelocity.to<double>() << " rotations per second" << std::endl;
377 ss << " DifferentialPosition: " << DifferentialPosition.to<double>() << " rotations" << std::endl;
378 ss << " EnableFOC: " << EnableFOC << std::endl;
379 ss << " TargetSlot: " << TargetSlot << std::endl;
380 ss << " DifferentialSlot: " << DifferentialSlot << std::endl;
381 ss << " OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
382 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
383 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
384 ss << " UseTimesync: " << UseTimesync << std::endl;
385 return ss.str();
386 }
387
388 /**
389 * \brief Gets information about this control request.
390 *
391 * \returns Map of control parameter names and corresponding applied values
392 */
393 std::map<std::string, std::string> GetControlInfo() const override
394 {
395 std::map<std::string, std::string> controlInfo;
396 std::stringstream ss;
397 controlInfo["Name"] = GetName();
398 ss << TargetVelocity.to<double>(); controlInfo["TargetVelocity"] = ss.str(); ss.str(std::string{});
399 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
400 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
401 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
402 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
403 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
404 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
405 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
406 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
407 return controlInfo;
408 }
409};
410
411}
412}
413}
414
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialVelocityVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, double TargetVelocity, double DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion, bool UseTimesync)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:29
std::string const & GetName() const
Definition: ControlRequest.hpp:52
Request PID to target velocity with a differential position setpoint.
Definition: DifferentialVelocityVoltage.hpp:30
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition: DifferentialVelocityVoltage.hpp:108
DifferentialVelocityVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DifferentialVelocityVoltage.hpp:325
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, bool UseTimesync=false)
Request PID to target velocity with a differential position setpoint.
Definition: DifferentialVelocityVoltage.hpp:179
DifferentialVelocityVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialVelocityVoltage.hpp:308
DifferentialVelocityVoltage & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialVelocityVoltage.hpp:256
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:393
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:121
DifferentialVelocityVoltage & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition: DifferentialVelocityVoltage.hpp:343
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:291
DifferentialVelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialVelocityVoltage.hpp:362
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:200
DifferentialVelocityVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialVelocityVoltage.hpp:215
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialVelocityVoltage.hpp:372
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 & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialVelocityVoltage.hpp:273
DifferentialVelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialVelocityVoltage.hpp:239
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialVelocityVoltage.hpp:76
Status codes reported by APIs, including OK, warnings, and errors.
Definition: StatusCodes.h:27
Represents the state of one swerve module.
Definition: StatusCodes.h:18
Definition: span.hpp:401