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