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