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