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