CTRE Phoenix 6 C++ 24.3.0
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
12#include <sstream>
13#include <units/angle.h>
14#include <units/angular_velocity.h>
15#include <units/dimensionless.h>
16#include <units/frequency.h>
17#include <units/time.h>
18
19
20namespace ctre {
21namespace phoenix6 {
22namespace controls {
23
24/**
25 * Request PID to target position with duty cycle feedforward.
26 *
27 * This control mode will set the motor's position setpoint to the position specified by the user. In
28 * addition, it will apply an additional duty cycle as an arbitrary feedforward value.
29 */
31{
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
33 {
34 if (req.get() != this)
35 {
36 auto const reqCast = dynamic_cast<PositionDutyCycle *>(req.get());
37 if (reqCast != nullptr)
38 {
39 *reqCast = *this;
40 }
41 else
42 {
43 req = std::make_shared<PositionDutyCycle>(*this);
44 }
45 }
46
47 return c_ctre_phoenix6_RequestControlPositionDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), Velocity.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
48 }
49
50public:
51 /**
52 * Position to drive toward in rotations.
53 */
54 units::angle::turn_t Position;
55 /**
56 * Velocity to drive toward in rotations per second. This is typically used for
57 * motion profiles generated by the robot program.
58 */
59 units::angular_velocity::turns_per_second_t Velocity;
60 /**
61 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
62 * peak power by ~15%. Set to false to use trapezoidal commutation.
63 *
64 * FOC improves motor performance by leveraging torque (current) control.
65 * However, this may be inconvenient for applications that require specifying
66 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
67 * combines the performances gains of FOC while still allowing applications to
68 * provide duty cycle or voltage demand. This not to be confused with simple
69 * sinusoidal control or phase voltage control which lacks the performance
70 * gains.
71 */
73 /**
74 * Feedforward to apply in fractional units between -1 and +1.
75 */
76 units::dimensionless::scalar_t FeedForward;
77 /**
78 * Select which gains are applied by selecting the slot. Use the configuration
79 * api to set the gain values for the selected slot before enabling this
80 * feature. Slot must be within [0,2].
81 */
82 int Slot;
83 /**
84 * Set to true to static-brake the rotor when output is zero (or within
85 * deadband). Set to false to use the NeutralMode configuration setting
86 * (default). This flag exists to provide the fundamental behavior of this
87 * control when output is zero, which is to provide 0V to the motor.
88 */
90 /**
91 * Set to true to force forward limiting. This allows users to use other limit
92 * switch sensors connected to robot controller. This also allows use of active
93 * sensors that require external power.
94 */
96 /**
97 * Set to true to force reverse limiting. This allows users to use other limit
98 * switch sensors connected to robot controller. This also allows use of active
99 * sensors that require external power.
100 */
102
103 /**
104 * \brief The period at which this control will update at.
105 * This is designated in Hertz, with a minimum of 20 Hz
106 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
107 *
108 * If this field is set to 0 Hz, the control request will
109 * be sent immediately as a one-shot frame. This may be useful
110 * for advanced applications that require outputs to be
111 * synchronized with data acquisition. In this case, we
112 * recommend not exceeding 50 ms between control calls.
113 */
114 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
115
116 /**
117 * \brief Request PID to target position with duty cycle feedforward.
118 *
119 * \details This control mode will set the motor's position setpoint to the
120 * position specified by the user. In addition, it will apply an
121 * additional duty cycle as an arbitrary feedforward value.
122 *
123 * \param Position Position to drive toward in rotations.
124 * \param Velocity Velocity to drive toward in rotations per second. This is
125 * typically used for motion profiles generated by the robot
126 * program.
127 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
128 * Pro), which increases peak power by ~15%. Set to false to
129 * use trapezoidal commutation.
130 *
131 * FOC improves motor performance by leveraging torque
132 * (current) control. However, this may be inconvenient for
133 * applications that require specifying duty cycle or
134 * voltage. CTR-Electronics has developed a hybrid method
135 * that combines the performances gains of FOC while still
136 * allowing applications to provide duty cycle or voltage
137 * demand. This not to be confused with simple sinusoidal
138 * control or phase voltage control which lacks the
139 * performance gains.
140 * \param FeedForward Feedforward to apply in fractional units between -1 and
141 * +1.
142 * \param Slot Select which gains are applied by selecting the slot. Use the
143 * configuration api to set the gain values for the selected slot
144 * before enabling this feature. Slot must be within [0,2].
145 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
146 * output is zero (or within deadband). Set
147 * to false to use the NeutralMode
148 * configuration setting (default). This flag
149 * exists to provide the fundamental behavior
150 * of this control when output is zero, which
151 * is to provide 0V to the motor.
152 * \param LimitForwardMotion Set to true to force forward limiting. This
153 * allows users to use other limit switch sensors
154 * connected to robot controller. This also allows
155 * use of active sensors that require external
156 * power.
157 * \param LimitReverseMotion Set to true to force reverse limiting. This
158 * allows users to use other limit switch sensors
159 * connected to robot controller. This also allows
160 * use of active sensors that require external
161 * power.
162 */
163 PositionDutyCycle(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity = 0.0_tps, bool EnableFOC = true, units::dimensionless::scalar_t FeedForward = 0.0, int Slot = 0, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"PositionDutyCycle"},
164 Position{std::move(Position)},
165 Velocity{std::move(Velocity)},
166 EnableFOC{std::move(EnableFOC)},
167 FeedForward{std::move(FeedForward)},
168 Slot{std::move(Slot)},
172 {}
173
174 /**
175 * \brief Modifies this Control Request's Position parameter and returns itself for
176 * method-chaining and easier to use request API.
177 *
178 * Position to drive toward in rotations.
179 *
180 * \param newPosition Parameter to modify
181 * \returns Itself
182 */
183 PositionDutyCycle& WithPosition(units::angle::turn_t newPosition)
184 {
185 Position = std::move(newPosition);
186 return *this;
187 }
188
189 /**
190 * \brief Modifies this Control Request's Velocity parameter and returns itself for
191 * method-chaining and easier to use request API.
192 *
193 * Velocity to drive toward in rotations per second. This is typically used for
194 * motion profiles generated by the robot program.
195 *
196 * \param newVelocity Parameter to modify
197 * \returns Itself
198 */
199 PositionDutyCycle& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
200 {
201 Velocity = std::move(newVelocity);
202 return *this;
203 }
204
205 /**
206 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
207 * method-chaining and easier to use request API.
208 *
209 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
210 * peak power by ~15%. 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 */
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.
234 *
235 * \param newFeedForward Parameter to modify
236 * \returns Itself
237 */
238 PositionDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
239 {
240 FeedForward = std::move(newFeedForward);
241 return *this;
242 }
243
244 /**
245 * \brief Modifies this Control Request's Slot parameter and returns itself for
246 * method-chaining and easier to use request API.
247 *
248 * Select which gains are applied by selecting the slot. Use the configuration
249 * api to set the gain values for the selected slot before enabling this
250 * feature. Slot must be within [0,2].
251 *
252 * \param newSlot Parameter to modify
253 * \returns Itself
254 */
256 {
257 Slot = std::move(newSlot);
258 return *this;
259 }
260
261 /**
262 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
263 * method-chaining and easier to use request API.
264 *
265 * Set to true to static-brake the rotor when output is zero (or within
266 * deadband). Set to false to use the NeutralMode configuration setting
267 * (default). This flag exists to provide the fundamental behavior of this
268 * control when output is zero, which is to provide 0V to the motor.
269 *
270 * \param newOverrideBrakeDurNeutral Parameter to modify
271 * \returns Itself
272 */
273 PositionDutyCycle& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
274 {
275 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
276 return *this;
277 }
278
279 /**
280 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
281 * method-chaining and easier to use request API.
282 *
283 * Set to true to force forward limiting. This allows users to use other limit
284 * switch sensors connected to robot controller. This also allows use of active
285 * sensors that require external power.
286 *
287 * \param newLimitForwardMotion Parameter to modify
288 * \returns Itself
289 */
290 PositionDutyCycle& WithLimitForwardMotion(bool newLimitForwardMotion)
291 {
292 LimitForwardMotion = std::move(newLimitForwardMotion);
293 return *this;
294 }
295
296 /**
297 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
298 * method-chaining and easier to use request API.
299 *
300 * Set to true to force reverse limiting. This allows users to use other limit
301 * switch sensors connected to robot controller. This also allows use of active
302 * sensors that require external power.
303 *
304 * \param newLimitReverseMotion Parameter to modify
305 * \returns Itself
306 */
307 PositionDutyCycle& WithLimitReverseMotion(bool newLimitReverseMotion)
308 {
309 LimitReverseMotion = std::move(newLimitReverseMotion);
310 return *this;
311 }
312 /**
313 * \brief Sets the period at which this control will update at.
314 * This is designated in Hertz, with a minimum of 20 Hz
315 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
316 *
317 * If this field is set to 0 Hz, the control request will
318 * be sent immediately as a one-shot frame. This may be useful
319 * for advanced applications that require outputs to be
320 * synchronized with data acquisition. In this case, we
321 * recommend not exceeding 50 ms between control calls.
322 *
323 * \param newUpdateFreqHz Parameter to modify
324 * \returns Itself
325 */
326 PositionDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
327 {
328 UpdateFreqHz = newUpdateFreqHz;
329 return *this;
330 }
331 /**
332 * Returns a string representation of the object.
333 *
334 * \returns a string representation of the object.
335 */
336 std::string ToString() const override
337 {
338 std::stringstream ss;
339 ss << "class: PositionDutyCycle" << std::endl;
340 ss << "Position: " << Position.to<double>() << std::endl;
341 ss << "Velocity: " << Velocity.to<double>() << std::endl;
342 ss << "EnableFOC: " << EnableFOC << std::endl;
343 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
344 ss << "Slot: " << Slot << std::endl;
345 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
346 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
347 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
348 return ss.str();
349 }
350
351 /**
352 * \brief Gets information about this control request.
353 *
354 * \returns Map of control parameter names and corresponding applied values
355 */
356 std::map<std::string, std::string> GetControlInfo() const override
357 {
358 std::map<std::string, std::string> controlInfo;
359 std::stringstream ss;
360 controlInfo["Name"] = GetName();
361 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
362 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
363 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
364 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
365 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
366 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
367 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
368 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
369 return controlInfo;
370 }
371};
372
373}
374}
375}
376
CTREXPORT int c_ctre_phoenix6_RequestControlPositionDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, double Velocity, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Request PID to target position with duty cycle feedforward.
Definition: PositionDutyCycle.hpp:31
PositionDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionDutyCycle.hpp:255
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: PositionDutyCycle.hpp:76
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:199
PositionDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: PositionDutyCycle.hpp:223
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: PositionDutyCycle.hpp:59
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:238
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: PositionDutyCycle.hpp:72
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionDutyCycle.hpp:54
PositionDutyCycle & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: PositionDutyCycle.hpp:307
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: PositionDutyCycle.hpp:95
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionDutyCycle.hpp:82
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: PositionDutyCycle.hpp:356
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: PositionDutyCycle.hpp:89
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionDutyCycle.hpp:114
PositionDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionDutyCycle.hpp:326
PositionDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: PositionDutyCycle.hpp:290
PositionDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: PositionDutyCycle.hpp:273
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:183
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionDutyCycle.hpp:336
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: PositionDutyCycle.hpp:101
PositionDutyCycle(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity=0.0_tps, bool EnableFOC=true, units::dimensionless::scalar_t FeedForward=0.0, int Slot=0, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Request PID to target position with duty cycle feedforward.
Definition: PositionDutyCycle.hpp:163
Definition: string_util.hpp:15