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
DifferentialDutyCycle.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/angle.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Request a specified motor duty cycle with a differential position
25 * closed-loop.
26 *
27 * This control mode will output a proportion of the supplied voltage which is supplied by the user. It will
28 * also set the motor's differential position setpoint to the specified position.
29 */
31{
32 bool ApplyConfigsOnRequest{false};
33
34 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
35 {
36 if (req.get() != this)
37 {
38 auto const reqCast = dynamic_cast<DifferentialDutyCycle *>(req.get());
39 if (reqCast != nullptr)
40 {
41 *reqCast = *this;
42 }
43 else
44 {
45 req = std::make_shared<DifferentialDutyCycle>(*this);
46 }
47 }
48
49 std::stringstream ss;
50
51 std::string strs{ss.str()};
52 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
53 ApplyConfigsOnRequest = false;
54 return c_ctre_phoenix6_RequestControlDifferentialDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, TargetOutput.to<double>(), DifferentialPosition.to<double>(), EnableFOC, DifferentialSlot, OverrideBrakeDurNeutral);
55 }
56
57public:
58 /**
59 * Proportion of supply voltage to apply in fractional units between -1 and +1
60 */
61 units::dimensionless::scalar_t TargetOutput;
62 /**
63 * Differential position to drive towards in rotations
64 */
65 units::angle::turn_t DifferentialPosition;
66 /**
67 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
68 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
69 * improves motor performance by leveraging torque (current) control. However,
70 * this may be inconvenient for applications that require specifying duty cycle
71 * or voltage. CTR-Electronics has developed a hybrid method that combines the
72 * performances gains of FOC while still allowing applications to provide duty
73 * cycle or voltage demand. This not to be confused with simple sinusoidal
74 * control or phase voltage control which lacks the performance gains.
75 */
77 /**
78 * Select which gains are applied to the differential controller by selecting
79 * the slot. Use the configuration api to set the gain values for the selected
80 * slot before enabling this feature. Slot must be within [0,2].
81 */
83 /**
84 * Set to true to static-brake the rotor when output is zero (or within
85 * deadband). Set to false to use the NeutralMode configuration setting
86 * (default). This flag exists to provide the fundamental behavior of this
87 * control when output is zero, which is to provide 0V to the motor.
88 */
90
91
92
93 /**
94 * \brief The timeout when sending configs associated with this control
95 */
96 units::time::second_t ConfigTimeout{0.1_s};
97
98 /**
99 * \brief The period at which this control will update at.
100 * This is designated in Hertz, with a minimum of 20 Hz
101 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
102 *
103 * If this field is set to 0 Hz, the control request will
104 * be sent immediately as a one-shot frame. This may be useful
105 * for advanced applications that require outputs to be
106 * synchronized with data acquisition. In this case, we
107 * recommend not exceeding 50 ms between control calls.
108 */
109 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
110
111 /**
112 * \brief Request a specified motor duty cycle with a differential position
113 * closed-loop.
114 *
115 * \details This control mode will output a proportion of the supplied voltage
116 * which is supplied by the user. It will also set the motor's
117 * differential position setpoint to the specified position.
118 *
119 * \param TargetOutput Proportion of supply voltage to apply in fractional
120 * units between -1 and +1
121 * \param DifferentialPosition Differential position to drive towards in
122 * rotations
123 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
124 * Pro), which increases peak power by ~15%. Set to false to
125 * use trapezoidal commutation. FOC improves motor
126 * performance by leveraging torque (current) control.
127 * However, this may be inconvenient for applications that
128 * require specifying duty cycle or voltage.
129 * CTR-Electronics has developed a hybrid method that
130 * combines the performances gains of FOC while still
131 * allowing applications to provide duty cycle or voltage
132 * demand. This not to be confused with simple sinusoidal
133 * control or phase voltage control which lacks the
134 * performance gains.
135 * \param DifferentialSlot Select which gains are applied to the differential
136 * controller by selecting the slot. Use the
137 * configuration api to set the gain values for the
138 * selected slot before enabling this feature. Slot
139 * must be within [0,2].
140 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
141 * output is zero (or within deadband). Set
142 * to false to use the NeutralMode
143 * configuration setting (default). This flag
144 * exists to provide the fundamental behavior
145 * of this control when output is zero, which
146 * is to provide 0V to the motor.
147 */
148 DifferentialDutyCycle(units::dimensionless::scalar_t TargetOutput, units::angle::turn_t DifferentialPosition, bool EnableFOC, int DifferentialSlot, bool OverrideBrakeDurNeutral) : ControlRequest{"DifferentialDutyCycle"},
149 TargetOutput{std::move(TargetOutput)},
151 EnableFOC{std::move(EnableFOC)},
154 {}
155
156 /**
157 * \brief Request a specified motor duty cycle with a differential position
158 * closed-loop.
159 *
160 * \details This control mode will output a proportion of the supplied voltage
161 * which is supplied by the user. It will also set the motor's
162 * differential position setpoint to the specified position.
163 *
164 * \param TargetOutput Proportion of supply voltage to apply in fractional
165 * units between -1 and +1
166 * \param DifferentialPosition Differential position to drive towards in
167 * rotations
168 */
169 DifferentialDutyCycle(units::dimensionless::scalar_t TargetOutput, units::angle::turn_t DifferentialPosition) : DifferentialDutyCycle{TargetOutput, DifferentialPosition, true, 1, false}
170 {}
171
172 /**
173 * \brief Modifies this Control Request's TargetOutput parameter and returns itself for
174 * method-chaining and easier to use request API.
175 * \param newTargetOutput Parameter to modify
176 * \returns Itself
177 */
178 DifferentialDutyCycle& WithTargetOutput(units::dimensionless::scalar_t newTargetOutput)
179 {
180 TargetOutput = std::move(newTargetOutput);
181 return *this;
182 }
183
184 /**
185 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
186 * method-chaining and easier to use request API.
187 * \param newDifferentialPosition Parameter to modify
188 * \returns Itself
189 */
190 DifferentialDutyCycle& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
191 {
192 DifferentialPosition = std::move(newDifferentialPosition);
193 return *this;
194 }
195
196 /**
197 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
198 * method-chaining and easier to use request API.
199 * \param newEnableFOC Parameter to modify
200 * \returns Itself
201 */
203 {
204 EnableFOC = std::move(newEnableFOC);
205 return *this;
206 }
207
208 /**
209 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
210 * method-chaining and easier to use request API.
211 * \param newDifferentialSlot Parameter to modify
212 * \returns Itself
213 */
215 {
216 DifferentialSlot = std::move(newDifferentialSlot);
217 return *this;
218 }
219
220 /**
221 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
222 * method-chaining and easier to use request API.
223 * \param newOverrideBrakeDurNeutral Parameter to modify
224 * \returns Itself
225 */
226 DifferentialDutyCycle& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
227 {
228 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
229 return *this;
230 }
231 /**
232 * \brief Sets the period at which this control will update at.
233 * This is designated in Hertz, with a minimum of 20 Hz
234 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
235 *
236 * If this field is set to 0 Hz, the control request will
237 * be sent immediately as a one-shot frame. This may be useful
238 * for advanced applications that require outputs to be
239 * synchronized with data acquisition. In this case, we
240 * recommend not exceeding 50 ms between control calls.
241 *
242 * \param newUpdateFreqHz Parameter to modify
243 * \returns Itself
244 */
245 DifferentialDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
246 {
247 UpdateFreqHz = newUpdateFreqHz;
248 return *this;
249 }
250 /**
251 * Returns a string representation of the object.
252 *
253 * \returns a string representation of the object.
254 */
255 std::string ToString() const override
256 {
257 std::stringstream ss;
258 ss << "class: DifferentialDutyCycle" << std::endl;
259 ss << "TargetOutput: " << TargetOutput.to<double>() << std::endl;
260 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
261 ss << "EnableFOC: " << EnableFOC << std::endl;
262 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
263 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
264 return ss.str();
265 }
266
267 /**
268 * \brief Forces configs to be applied the next time this is used in a setControl.
269 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
270 * properly set when they are not already set
271 */
272 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
273
274 /**
275 * \brief Gets information about this control request.
276 *
277 * \returns Map of control parameter names and corresponding applied values
278 */
279 std::map<std::string, std::string> GetControlInfo() const override
280 {
281 std::map<std::string, std::string> controlInfo;
282 std::stringstream ss;
283 controlInfo["Name"] = GetName();
284 ss << TargetOutput.to<double>(); controlInfo["TargetOutput"] = ss.str(); ss.str(std::string{});
285 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
286 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
287 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
288 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
289 return controlInfo;
290 }
291};
292
293}
294}
295}
296
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double TargetOutput, double DifferentialPosition, bool EnableFOC, int DifferentialSlot, 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 with a differential position closed-loop.
Definition: DifferentialDutyCycle.hpp:31
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialDutyCycle.hpp:255
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialDutyCycle.hpp:279
DifferentialDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialDutyCycle.hpp:245
units::angle::turn_t DifferentialPosition
Differential position to drive towards in rotations.
Definition: DifferentialDutyCycle.hpp:65
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialDutyCycle.hpp:82
DifferentialDutyCycle(units::dimensionless::scalar_t TargetOutput, units::angle::turn_t DifferentialPosition, bool EnableFOC, int DifferentialSlot, bool OverrideBrakeDurNeutral)
Request a specified motor duty cycle with a differential position closed-loop.
Definition: DifferentialDutyCycle.hpp:148
DifferentialDutyCycle(units::dimensionless::scalar_t TargetOutput, units::angle::turn_t DifferentialPosition)
Request a specified motor duty cycle with a differential position closed-loop.
Definition: DifferentialDutyCycle.hpp:169
DifferentialDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialDutyCycle.hpp:226
DifferentialDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialDutyCycle.hpp:202
DifferentialDutyCycle & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialDutyCycle.hpp:190
DifferentialDutyCycle & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialDutyCycle.hpp:214
DifferentialDutyCycle & WithTargetOutput(units::dimensionless::scalar_t newTargetOutput)
Modifies this Control Request's TargetOutput parameter and returns itself for method-chaining and eas...
Definition: DifferentialDutyCycle.hpp:178
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialDutyCycle.hpp:76
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: DifferentialDutyCycle.hpp:96
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialDutyCycle.hpp:109
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: DifferentialDutyCycle.hpp:272
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialDutyCycle.hpp:89
units::dimensionless::scalar_t TargetOutput
Proportion of supply voltage to apply in fractional units between -1 and +1.
Definition: DifferentialDutyCycle.hpp:61
Definition: ManualEvent.hpp:12