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