CTRE Phoenix 6 C++ 24.2.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. FOC
63 * improves motor performance by leveraging torque (current) control. However,
64 * this may be inconvenient for applications that require specifying duty cycle
65 * or voltage. CTR-Electronics has developed a hybrid method that combines the
66 * performances gains of FOC while still allowing applications to provide duty
67 * cycle or voltage demand. This not to be confused with simple sinusoidal
68 * control or phase voltage control which lacks the performance gains.
69 */
71 /**
72 * Feedforward to apply in fractional units between -1 and +1.
73 */
74 units::dimensionless::scalar_t FeedForward;
75 /**
76 * Select which gains are applied by selecting the slot. Use the configuration
77 * api to set the gain values for the selected slot before enabling this
78 * feature. Slot must be within [0,2].
79 */
80 int Slot;
81 /**
82 * Set to true to static-brake the rotor when output is zero (or within
83 * deadband). Set to false to use the NeutralMode configuration setting
84 * (default). This flag exists to provide the fundamental behavior of this
85 * control when output is zero, which is to provide 0V to the motor.
86 */
88 /**
89 * Set to true to force forward limiting. This allows users to use other limit
90 * switch sensors connected to robot controller. This also allows use of active
91 * sensors that require external power.
92 */
94 /**
95 * Set to true to force reverse limiting. This allows users to use other limit
96 * switch sensors connected to robot controller. This also allows use of active
97 * sensors that require external power.
98 */
100
101 /**
102 * \brief The period at which this control will update at.
103 * This is designated in Hertz, with a minimum of 20 Hz
104 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
105 *
106 * If this field is set to 0 Hz, the control request will
107 * be sent immediately as a one-shot frame. This may be useful
108 * for advanced applications that require outputs to be
109 * synchronized with data acquisition. In this case, we
110 * recommend not exceeding 50 ms between control calls.
111 */
112 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
113
114 /**
115 * \brief Request PID to target position with duty cycle feedforward.
116 *
117 * \details This control mode will set the motor's position setpoint to the
118 * position specified by the user. In addition, it will apply an
119 * additional duty cycle as an arbitrary feedforward value.
120 *
121 * \param Position Position to drive toward in rotations.
122 * \param Velocity Velocity to drive toward in rotations per second. This is
123 * typically used for motion profiles generated by the robot
124 * program.
125 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
126 * Pro), which increases peak power by ~15%. Set to false to
127 * use trapezoidal commutation. FOC improves motor
128 * performance by leveraging torque (current) control.
129 * However, this may be inconvenient for applications that
130 * require specifying duty cycle or voltage.
131 * CTR-Electronics has developed a hybrid method that
132 * combines the performances gains of FOC while still
133 * allowing applications to provide duty cycle or voltage
134 * demand. This not to be confused with simple sinusoidal
135 * control or phase voltage control which lacks the
136 * performance gains.
137 * \param FeedForward Feedforward to apply in fractional units between -1 and
138 * +1.
139 * \param Slot Select which gains are applied by selecting the slot. Use the
140 * configuration api to set the gain values for the selected slot
141 * before enabling this feature. Slot must be within [0,2].
142 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
143 * output is zero (or within deadband). Set
144 * to false to use the NeutralMode
145 * configuration setting (default). This flag
146 * exists to provide the fundamental behavior
147 * of this control when output is zero, which
148 * is to provide 0V to the motor.
149 * \param LimitForwardMotion Set to true to force forward limiting. This
150 * allows users to use other limit switch sensors
151 * connected to robot controller. This also allows
152 * use of active sensors that require external
153 * power.
154 * \param LimitReverseMotion Set to true to force reverse limiting. This
155 * allows users to use other limit switch sensors
156 * connected to robot controller. This also allows
157 * use of active sensors that require external
158 * power.
159 */
160 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"},
161 Position{std::move(Position)},
162 Velocity{std::move(Velocity)},
163 EnableFOC{std::move(EnableFOC)},
164 FeedForward{std::move(FeedForward)},
165 Slot{std::move(Slot)},
169 {}
170
171 /**
172 * \brief Modifies this Control Request's Position parameter and returns itself for
173 * method-chaining and easier to use request API.
174 * \param newPosition Parameter to modify
175 * \returns Itself
176 */
177 PositionDutyCycle& WithPosition(units::angle::turn_t newPosition)
178 {
179 Position = std::move(newPosition);
180 return *this;
181 }
182
183 /**
184 * \brief Modifies this Control Request's Velocity parameter and returns itself for
185 * method-chaining and easier to use request API.
186 * \param newVelocity Parameter to modify
187 * \returns Itself
188 */
189 PositionDutyCycle& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
190 {
191 Velocity = std::move(newVelocity);
192 return *this;
193 }
194
195 /**
196 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
197 * method-chaining and easier to use request API.
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 * \param newFeedForward Parameter to modify
211 * \returns Itself
212 */
213 PositionDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
214 {
215 FeedForward = std::move(newFeedForward);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's Slot parameter and returns itself for
221 * method-chaining and easier to use request API.
222 * \param newSlot Parameter to modify
223 * \returns Itself
224 */
226 {
227 Slot = std::move(newSlot);
228 return *this;
229 }
230
231 /**
232 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
233 * method-chaining and easier to use request API.
234 * \param newOverrideBrakeDurNeutral Parameter to modify
235 * \returns Itself
236 */
237 PositionDutyCycle& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
238 {
239 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
240 return *this;
241 }
242
243 /**
244 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
245 * method-chaining and easier to use request API.
246 * \param newLimitForwardMotion Parameter to modify
247 * \returns Itself
248 */
249 PositionDutyCycle& WithLimitForwardMotion(bool newLimitForwardMotion)
250 {
251 LimitForwardMotion = std::move(newLimitForwardMotion);
252 return *this;
253 }
254
255 /**
256 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
257 * method-chaining and easier to use request API.
258 * \param newLimitReverseMotion Parameter to modify
259 * \returns Itself
260 */
261 PositionDutyCycle& WithLimitReverseMotion(bool newLimitReverseMotion)
262 {
263 LimitReverseMotion = std::move(newLimitReverseMotion);
264 return *this;
265 }
266 /**
267 * \brief Sets the period at which this control will update at.
268 * This is designated in Hertz, with a minimum of 20 Hz
269 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
270 *
271 * If this field is set to 0 Hz, the control request will
272 * be sent immediately as a one-shot frame. This may be useful
273 * for advanced applications that require outputs to be
274 * synchronized with data acquisition. In this case, we
275 * recommend not exceeding 50 ms between control calls.
276 *
277 * \param newUpdateFreqHz Parameter to modify
278 * \returns Itself
279 */
280 PositionDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
281 {
282 UpdateFreqHz = newUpdateFreqHz;
283 return *this;
284 }
285 /**
286 * Returns a string representation of the object.
287 *
288 * \returns a string representation of the object.
289 */
290 std::string ToString() const override
291 {
292 std::stringstream ss;
293 ss << "class: PositionDutyCycle" << std::endl;
294 ss << "Position: " << Position.to<double>() << std::endl;
295 ss << "Velocity: " << Velocity.to<double>() << std::endl;
296 ss << "EnableFOC: " << EnableFOC << std::endl;
297 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
298 ss << "Slot: " << Slot << std::endl;
299 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
300 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
301 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
302 return ss.str();
303 }
304
305 /**
306 * \brief Gets information about this control request.
307 *
308 * \returns Map of control parameter names and corresponding applied values
309 */
310 std::map<std::string, std::string> GetControlInfo() const override
311 {
312 std::map<std::string, std::string> controlInfo;
313 std::stringstream ss;
314 controlInfo["Name"] = GetName();
315 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
316 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
317 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
318 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
319 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
320 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
321 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
322 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
323 return controlInfo;
324 }
325};
326
327}
328}
329}
330
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:225
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: PositionDutyCycle.hpp:74
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:189
PositionDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: PositionDutyCycle.hpp:201
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:213
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: PositionDutyCycle.hpp:70
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:261
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: PositionDutyCycle.hpp:93
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionDutyCycle.hpp:80
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: PositionDutyCycle.hpp:310
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: PositionDutyCycle.hpp:87
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionDutyCycle.hpp:112
PositionDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionDutyCycle.hpp:280
PositionDutyCycle & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: PositionDutyCycle.hpp:249
PositionDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: PositionDutyCycle.hpp:237
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:177
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionDutyCycle.hpp:290
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: PositionDutyCycle.hpp:99
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:160
Definition: RcManualEvent.hpp:12