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