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