CTRE Phoenix 6 C++ 24.3.0
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, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) 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
44 return c_ctre_phoenix6_RequestControlDutyCycleOut(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Output.to<double>(), EnableFOC, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion);
45 }
46
47public:
48 /**
49 * Proportion of supply voltage to apply in fractional units between -1 and +1
50 */
51 units::dimensionless::scalar_t Output;
52 /**
53 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
54 * 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 */
65 /**
66 * 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 * Set to true to force forward limiting. This allows users to use other limit
74 * switch sensors connected to robot controller. This also allows use of active
75 * sensors that require external power.
76 */
78 /**
79 * Set to true to force reverse limiting. This allows users to use other limit
80 * switch sensors connected to robot controller. This also allows use of active
81 * sensors that require external power.
82 */
84
85 /**
86 * \brief The period at which this control will update at.
87 * This is designated in Hertz, with a minimum of 20 Hz
88 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
89 *
90 * If this field is set to 0 Hz, the control request will
91 * be sent immediately as a one-shot frame. This may be useful
92 * for advanced applications that require outputs to be
93 * synchronized with data acquisition. In this case, we
94 * recommend not exceeding 50 ms between control calls.
95 */
96 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
97
98 /**
99 * \brief Request a specified motor duty cycle.
100 *
101 * \details This control mode will output a proportion of the supplied voltage
102 * which is supplied by the user.
103 *
104 * \param Output Proportion of supply voltage to apply in fractional units
105 * between -1 and +1
106 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
107 * Pro), which increases peak power by ~15%. Set to false to
108 * use trapezoidal commutation.
109 *
110 * FOC improves motor performance by leveraging torque
111 * (current) control. However, this may be inconvenient for
112 * applications that require specifying duty cycle or
113 * voltage. CTR-Electronics has developed a hybrid method
114 * that combines the performances gains of FOC while still
115 * allowing applications to provide duty cycle or voltage
116 * demand. This not to be confused with simple sinusoidal
117 * control or phase voltage control which lacks the
118 * performance gains.
119 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
120 * output is zero (or within deadband). Set
121 * to false to use the NeutralMode
122 * configuration setting (default). This flag
123 * exists to provide the fundamental behavior
124 * of this control when output is zero, which
125 * is to provide 0V to the motor.
126 * \param LimitForwardMotion Set to true to force forward limiting. This
127 * allows users to use other limit switch sensors
128 * connected to robot controller. This also allows
129 * use of active sensors that require external
130 * power.
131 * \param LimitReverseMotion Set to true to force reverse limiting. This
132 * allows users to use other limit switch sensors
133 * connected to robot controller. This also allows
134 * use of active sensors that require external
135 * power.
136 */
137 DutyCycleOut(units::dimensionless::scalar_t Output, bool EnableFOC = true, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"DutyCycleOut"},
138 Output{std::move(Output)},
139 EnableFOC{std::move(EnableFOC)},
143 {}
144
145 /**
146 * \brief Modifies this Control Request's Output parameter and returns itself for
147 * method-chaining and easier to use request API.
148 *
149 * Proportion of supply voltage to apply in fractional units between -1 and +1
150 *
151 * \param newOutput Parameter to modify
152 * \returns Itself
153 */
154 DutyCycleOut& WithOutput(units::dimensionless::scalar_t newOutput)
155 {
156 Output = std::move(newOutput);
157 return *this;
158 }
159
160 /**
161 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
162 * method-chaining and easier to use request API.
163 *
164 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
165 * peak power by ~15%. Set to false to use trapezoidal commutation.
166 *
167 * FOC improves motor performance by leveraging torque (current) control.
168 * However, this may be inconvenient for applications that require specifying
169 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
170 * combines the performances gains of FOC while still allowing applications to
171 * provide duty cycle or voltage demand. This not to be confused with simple
172 * sinusoidal control or phase voltage control which lacks the performance
173 * gains.
174 *
175 * \param newEnableFOC Parameter to modify
176 * \returns Itself
177 */
178 DutyCycleOut& WithEnableFOC(bool newEnableFOC)
179 {
180 EnableFOC = std::move(newEnableFOC);
181 return *this;
182 }
183
184 /**
185 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
186 * method-chaining and easier to use request API.
187 *
188 * Set to true to static-brake the rotor when output is zero (or within
189 * deadband). Set to false to use the NeutralMode configuration setting
190 * (default). This flag exists to provide the fundamental behavior of this
191 * control when output is zero, which is to provide 0V to the motor.
192 *
193 * \param newOverrideBrakeDurNeutral Parameter to modify
194 * \returns Itself
195 */
196 DutyCycleOut& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
197 {
198 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
199 return *this;
200 }
201
202 /**
203 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
204 * method-chaining and easier to use request API.
205 *
206 * Set to true to force forward limiting. This allows users to use other limit
207 * switch sensors connected to robot controller. This also allows use of active
208 * sensors that require external power.
209 *
210 * \param newLimitForwardMotion Parameter to modify
211 * \returns Itself
212 */
213 DutyCycleOut& WithLimitForwardMotion(bool newLimitForwardMotion)
214 {
215 LimitForwardMotion = std::move(newLimitForwardMotion);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
221 * method-chaining and easier to use request API.
222 *
223 * Set to true to force reverse limiting. This allows users to use other limit
224 * switch sensors connected to robot controller. This also allows use of active
225 * sensors that require external power.
226 *
227 * \param newLimitReverseMotion Parameter to modify
228 * \returns Itself
229 */
230 DutyCycleOut& WithLimitReverseMotion(bool newLimitReverseMotion)
231 {
232 LimitReverseMotion = std::move(newLimitReverseMotion);
233 return *this;
234 }
235 /**
236 * \brief Sets the period at which this control will update at.
237 * This is designated in Hertz, with a minimum of 20 Hz
238 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
239 *
240 * If this field is set to 0 Hz, the control request will
241 * be sent immediately as a one-shot frame. This may be useful
242 * for advanced applications that require outputs to be
243 * synchronized with data acquisition. In this case, we
244 * recommend not exceeding 50 ms between control calls.
245 *
246 * \param newUpdateFreqHz Parameter to modify
247 * \returns Itself
248 */
249 DutyCycleOut &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
250 {
251 UpdateFreqHz = newUpdateFreqHz;
252 return *this;
253 }
254 /**
255 * Returns a string representation of the object.
256 *
257 * \returns a string representation of the object.
258 */
259 std::string ToString() const override
260 {
261 std::stringstream ss;
262 ss << "class: DutyCycleOut" << std::endl;
263 ss << "Output: " << Output.to<double>() << std::endl;
264 ss << "EnableFOC: " << EnableFOC << std::endl;
265 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
266 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
267 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
268 return ss.str();
269 }
270
271 /**
272 * \brief Gets information about this control request.
273 *
274 * \returns Map of control parameter names and corresponding applied values
275 */
276 std::map<std::string, std::string> GetControlInfo() const override
277 {
278 std::map<std::string, std::string> controlInfo;
279 std::stringstream ss;
280 controlInfo["Name"] = GetName();
281 ss << Output.to<double>(); controlInfo["Output"] = ss.str(); ss.str(std::string{});
282 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
283 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
284 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
285 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
286 return controlInfo;
287 }
288};
289
290}
291}
292}
293
CTREXPORT int c_ctre_phoenix6_RequestControlDutyCycleOut(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Output, bool EnableFOC, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
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:230
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:249
units::dimensionless::scalar_t Output
Proportion of supply voltage to apply in fractional units between -1 and +1.
Definition: DutyCycleOut.hpp:51
DutyCycleOut(units::dimensionless::scalar_t Output, bool EnableFOC=true, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Request a specified motor duty cycle.
Definition: DutyCycleOut.hpp:137
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DutyCycleOut.hpp:96
DutyCycleOut & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DutyCycleOut.hpp:196
DutyCycleOut & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DutyCycleOut.hpp:178
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:154
std::string ToString() const override
Returns a string representation of the object.
Definition: DutyCycleOut.hpp:259
DutyCycleOut & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DutyCycleOut.hpp:213
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:276
Definition: string_util.hpp:15