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