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