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