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