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