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