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