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