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