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