CTRE Phoenix 6 C++ 24.2.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. FOC
56 * improves motor performance by leveraging torque (current) control. However,
57 * this may be inconvenient for applications that require specifying duty cycle
58 * or voltage. CTR-Electronics has developed a hybrid method that combines the
59 * performances gains of FOC while still allowing applications to provide duty
60 * cycle or voltage demand. This not to be confused with simple sinusoidal
61 * control or phase voltage control which lacks the performance gains.
62 */
64 /**
65 * Set to true to static-brake the rotor when output is zero (or within
66 * deadband). Set to false to use the NeutralMode configuration setting
67 * (default). This flag exists to provide the fundamental behavior of this
68 * control when output is zero, which is to provide 0V to the motor.
69 */
71 /**
72 * Set to true to force forward limiting. This allows users to use other limit
73 * switch sensors connected to robot controller. This also allows use of active
74 * sensors that require external power.
75 */
77 /**
78 * Set to true to force reverse limiting. This allows users to use other limit
79 * switch sensors connected to robot controller. This also allows use of active
80 * sensors that require external power.
81 */
83
84 /**
85 * \brief The period at which this control will update at.
86 * This is designated in Hertz, with a minimum of 20 Hz
87 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
88 *
89 * If this field is set to 0 Hz, the control request will
90 * be sent immediately as a one-shot frame. This may be useful
91 * for advanced applications that require outputs to be
92 * synchronized with data acquisition. In this case, we
93 * recommend not exceeding 50 ms between control calls.
94 */
95 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
96
97 /**
98 * \brief Request a specified voltage.
99 *
100 * \details This control mode will attempt to apply the specified voltage to the
101 * motor. If the supply voltage is below the requested voltage, the
102 * motor controller will output the supply voltage.
103 *
104 * \param Output Voltage to attempt to drive at
105 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
106 * Pro), which increases peak power by ~15%. Set to false to
107 * use trapezoidal commutation. FOC improves motor
108 * performance by leveraging torque (current) control.
109 * However, this may be inconvenient for applications that
110 * require specifying duty cycle or voltage.
111 * CTR-Electronics has developed a hybrid method that
112 * combines the performances gains of FOC while still
113 * allowing applications to provide duty cycle or voltage
114 * demand. This not to be confused with simple sinusoidal
115 * control or phase voltage control which lacks the
116 * performance gains.
117 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
118 * output is zero (or within deadband). Set
119 * to false to use the NeutralMode
120 * configuration setting (default). This flag
121 * exists to provide the fundamental behavior
122 * of this control when output is zero, which
123 * is to provide 0V to the motor.
124 * \param LimitForwardMotion Set to true to force forward limiting. This
125 * allows users to use other limit switch sensors
126 * connected to robot controller. This also allows
127 * use of active sensors that require external
128 * power.
129 * \param LimitReverseMotion Set to true to force reverse limiting. This
130 * allows users to use other limit switch sensors
131 * connected to robot controller. This also allows
132 * use of active sensors that require external
133 * power.
134 */
135 VoltageOut(units::voltage::volt_t Output, bool EnableFOC = true, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"VoltageOut"},
136 Output{std::move(Output)},
137 EnableFOC{std::move(EnableFOC)},
141 {}
142
143 /**
144 * \brief Modifies this Control Request's Output parameter and returns itself for
145 * method-chaining and easier to use request API.
146 * \param newOutput Parameter to modify
147 * \returns Itself
148 */
149 VoltageOut& WithOutput(units::voltage::volt_t newOutput)
150 {
151 Output = std::move(newOutput);
152 return *this;
153 }
154
155 /**
156 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
157 * method-chaining and easier to use request API.
158 * \param newEnableFOC Parameter to modify
159 * \returns Itself
160 */
161 VoltageOut& WithEnableFOC(bool newEnableFOC)
162 {
163 EnableFOC = std::move(newEnableFOC);
164 return *this;
165 }
166
167 /**
168 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
169 * method-chaining and easier to use request API.
170 * \param newOverrideBrakeDurNeutral Parameter to modify
171 * \returns Itself
172 */
173 VoltageOut& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
174 {
175 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
176 return *this;
177 }
178
179 /**
180 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
181 * method-chaining and easier to use request API.
182 * \param newLimitForwardMotion Parameter to modify
183 * \returns Itself
184 */
185 VoltageOut& WithLimitForwardMotion(bool newLimitForwardMotion)
186 {
187 LimitForwardMotion = std::move(newLimitForwardMotion);
188 return *this;
189 }
190
191 /**
192 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
193 * method-chaining and easier to use request API.
194 * \param newLimitReverseMotion Parameter to modify
195 * \returns Itself
196 */
197 VoltageOut& WithLimitReverseMotion(bool newLimitReverseMotion)
198 {
199 LimitReverseMotion = std::move(newLimitReverseMotion);
200 return *this;
201 }
202 /**
203 * \brief Sets the period at which this control will update at.
204 * This is designated in Hertz, with a minimum of 20 Hz
205 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
206 *
207 * If this field is set to 0 Hz, the control request will
208 * be sent immediately as a one-shot frame. This may be useful
209 * for advanced applications that require outputs to be
210 * synchronized with data acquisition. In this case, we
211 * recommend not exceeding 50 ms between control calls.
212 *
213 * \param newUpdateFreqHz Parameter to modify
214 * \returns Itself
215 */
216 VoltageOut &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
217 {
218 UpdateFreqHz = newUpdateFreqHz;
219 return *this;
220 }
221 /**
222 * Returns a string representation of the object.
223 *
224 * \returns a string representation of the object.
225 */
226 std::string ToString() const override
227 {
228 std::stringstream ss;
229 ss << "class: VoltageOut" << std::endl;
230 ss << "Output: " << Output.to<double>() << std::endl;
231 ss << "EnableFOC: " << EnableFOC << std::endl;
232 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
233 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
234 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
235 return ss.str();
236 }
237
238 /**
239 * \brief Gets information about this control request.
240 *
241 * \returns Map of control parameter names and corresponding applied values
242 */
243 std::map<std::string, std::string> GetControlInfo() const override
244 {
245 std::map<std::string, std::string> controlInfo;
246 std::stringstream ss;
247 controlInfo["Name"] = GetName();
248 ss << Output.to<double>(); controlInfo["Output"] = ss.str(); ss.str(std::string{});
249 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
250 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
251 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
252 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
253 return controlInfo;
254 }
255};
256
257}
258}
259}
260
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:82
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: VoltageOut.hpp:70
VoltageOut & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VoltageOut.hpp:216
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:149
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: VoltageOut.hpp:76
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VoltageOut.hpp:95
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:135
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VoltageOut.hpp:243
VoltageOut & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: VoltageOut.hpp:173
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:63
VoltageOut & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: VoltageOut.hpp:197
VoltageOut & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: VoltageOut.hpp:161
std::string ToString() const override
Returns a string representation of the object.
Definition: VoltageOut.hpp:226
VoltageOut & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: VoltageOut.hpp:185
Definition: RcManualEvent.hpp:12