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