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