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