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