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