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