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