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