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