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