Loading [MathJax]/extensions/tex2jax.js
CTRE Phoenix 6 C++ 23.10.0-alpha-8
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 bool ApplyConfigsOnRequest{false};
31
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
33 {
34 if (req.get() != this)
35 {
36 auto const reqCast = dynamic_cast<VoltageOut *>(req.get());
37 if (reqCast != nullptr)
38 {
39 *reqCast = *this;
40 }
41 else
42 {
43 req = std::make_shared<VoltageOut>(*this);
44 }
45 }
46
47 std::stringstream ss;
48 ss << Voltage.Serialize();
49 std::string strs{ss.str()};
50 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
51 ApplyConfigsOnRequest = false;
52 return c_ctre_phoenix6_RequestControlVoltageOut(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Output.to<double>(), EnableFOC, OverrideBrakeDurNeutral);
53 }
54
55public:
56 /**
57 * Voltage to attempt to drive at
58 */
59 units::voltage::volt_t Output;
60 /**
61 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
62 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
63 * improves motor performance by leveraging torque (current) control. However,
64 * this may be inconvenient for applications that require specifying duty cycle
65 * or voltage. CTR-Electronics has developed a hybrid method that combines the
66 * performances gains of FOC while still allowing applications to provide duty
67 * cycle or voltage demand. This not to be confused with simple sinusoidal
68 * control or phase voltage control which lacks the performance gains.
69 */
71 /**
72 * Set to true to static-brake the rotor when output is zero (or within
73 * deadband). Set to false to use the NeutralMode configuration setting
74 * (default). This flag exists to provide the fundamental behavior of this
75 * control when output is zero, which is to provide 0V to the motor.
76 */
78
79 /**
80 * \brief Voltage-specific configs
81 *
82 * \details Voltage-specific configs
83 */
85
86 /**
87 * \brief The timeout when sending configs associated with this control
88 */
89 units::time::second_t ConfigTimeout{0.1_s};
90
91 /**
92 * \brief The period at which this control will update at.
93 * This is designated in Hertz, with a minimum of 20 Hz
94 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
95 *
96 * If this field is set to 0 Hz, the control request will
97 * be sent immediately as a one-shot frame. This may be useful
98 * for advanced applications that require outputs to be
99 * synchronized with data acquisition. In this case, we
100 * recommend not exceeding 50 ms between control calls.
101 */
102 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
103
104 /**
105 * \brief Request a specified voltage.
106 *
107 * \details This control mode will attempt to apply the specified voltage to the
108 * motor. If the supply voltage is below the requested voltage, the
109 * motor controller will output the supply voltage.
110 *
111 * \param Output Voltage to attempt to drive at
112 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
113 * Pro), which increases peak power by ~15%. Set to false to
114 * use trapezoidal commutation. FOC improves motor
115 * performance by leveraging torque (current) control.
116 * However, this may be inconvenient for applications that
117 * require specifying duty cycle or voltage.
118 * CTR-Electronics has developed a hybrid method that
119 * combines the performances gains of FOC while still
120 * allowing applications to provide duty cycle or voltage
121 * demand. This not to be confused with simple sinusoidal
122 * control or phase voltage control which lacks the
123 * performance gains.
124 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
125 * output is zero (or within deadband). Set
126 * to false to use the NeutralMode
127 * configuration setting (default). This flag
128 * exists to provide the fundamental behavior
129 * of this control when output is zero, which
130 * is to provide 0V to the motor.
131 */
132 VoltageOut(units::voltage::volt_t Output, bool EnableFOC, bool OverrideBrakeDurNeutral) : ControlRequest{"VoltageOut"},
133 Output{std::move(Output)},
134 EnableFOC{std::move(EnableFOC)},
136 {}
137
138 /**
139 * \brief Request a specified voltage.
140 *
141 * \details This control mode will attempt to apply the specified voltage to the
142 * motor. If the supply voltage is below the requested voltage, the
143 * motor controller will output the supply voltage.
144 *
145 * \param Output Voltage to attempt to drive at
146 */
147 VoltageOut(units::voltage::volt_t Output) : VoltageOut{Output, true, false}
148 {}
149
150 /**
151 * \brief Modifies this Control Request's Output parameter and returns itself for
152 * method-chaining and easier to use request API.
153 * \param newOutput Parameter to modify
154 * \returns Itself
155 */
156 VoltageOut& WithOutput(units::voltage::volt_t newOutput)
157 {
158 Output = std::move(newOutput);
159 return *this;
160 }
161
162 /**
163 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
164 * method-chaining and easier to use request API.
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 * \param newOverrideBrakeDurNeutral Parameter to modify
178 * \returns Itself
179 */
180 VoltageOut& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
181 {
182 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
183 return *this;
184 }
185 /**
186 * \brief Sets the period at which this control will update at.
187 * This is designated in Hertz, with a minimum of 20 Hz
188 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
189 *
190 * If this field is set to 0 Hz, the control request will
191 * be sent immediately as a one-shot frame. This may be useful
192 * for advanced applications that require outputs to be
193 * synchronized with data acquisition. In this case, we
194 * recommend not exceeding 50 ms between control calls.
195 *
196 * \param newUpdateFreqHz Parameter to modify
197 * \returns Itself
198 */
199 VoltageOut &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
200 {
201 UpdateFreqHz = newUpdateFreqHz;
202 return *this;
203 }
204 /**
205 * Returns a string representation of the object.
206 *
207 * \returns a string representation of the object.
208 */
209 std::string ToString() const override
210 {
211 std::stringstream ss;
212 ss << "class: VoltageOut" << std::endl;
213 ss << "Output: " << Output.to<double>() << std::endl;
214 ss << "EnableFOC: " << EnableFOC << std::endl;
215 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
216 return ss.str();
217 }
218
219 /**
220 * \brief Forces configs to be applied the next time this is used in a setControl.
221 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
222 * properly set when they are not already set
223 */
224 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
225
226 /**
227 * \brief Gets information about this control request.
228 *
229 * \returns Map of control parameter names and corresponding applied values
230 */
231 std::map<std::string, std::string> GetControlInfo() const override
232 {
233 std::map<std::string, std::string> controlInfo;
234 std::stringstream ss;
235 controlInfo["Name"] = GetName();
236 ss << Output.to<double>(); controlInfo["Output"] = ss.str(); ss.str(std::string{});
237 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
238 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
239 return controlInfo;
240 }
241};
242
243}
244}
245}
246
CTREXPORT int c_ctre_phoenix6_requestConfigApply(const char *canbus, uint32_t ecuEncoding, double timeoutSeconds, const char *str, uint32_t strlen, bool forceApply)
CTREXPORT int c_ctre_phoenix6_RequestControlVoltageOut(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Output, bool EnableFOC, bool OverrideBrakeDurNeutral)
Voltage-specific configs.
Definition: Configs.hpp:538
std::string Serialize() const override
Definition: Configs.hpp:587
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
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: VoltageOut.hpp:224
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: VoltageOut.hpp:77
VoltageOut & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VoltageOut.hpp:199
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:156
configs::VoltageConfigs Voltage
Voltage-specific configs.
Definition: VoltageOut.hpp:84
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VoltageOut.hpp:102
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VoltageOut.hpp:231
VoltageOut(units::voltage::volt_t Output)
Request a specified voltage.
Definition: VoltageOut.hpp:147
VoltageOut & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: VoltageOut.hpp:180
units::voltage::volt_t Output
Voltage to attempt to drive at.
Definition: VoltageOut.hpp:59
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: VoltageOut.hpp:70
VoltageOut(units::voltage::volt_t Output, bool EnableFOC, bool OverrideBrakeDurNeutral)
Request a specified voltage.
Definition: VoltageOut.hpp:132
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: VoltageOut.hpp:89
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:209
Definition: ManualEvent.hpp:12