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
VelocityVoltage.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/voltage.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 voltage 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<VelocityVoltage *>(req.get());
39 if (reqCast != nullptr)
40 {
41 *reqCast = *this;
42 }
43 else
44 {
45 req = std::make_shared<VelocityVoltage>(*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_RequestControlVelocityVoltage(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 volts
79 */
80 units::voltage::volt_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 voltage 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 volts
138 * \param Slot Select which gains are applied by selecting the slot. Use the
139 * configuration api to set the gain values for the selected slot
140 * before enabling this feature. Slot must be within [0,2].
141 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
142 * output is zero (or within deadband). Set
143 * to false to use the NeutralMode
144 * configuration setting (default). This flag
145 * exists to provide the fundamental behavior
146 * of this control when output is zero, which
147 * is to provide 0V to the motor.
148 */
149 VelocityVoltage(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral) : ControlRequest{"VelocityVoltage"},
150 Velocity{std::move(Velocity)},
151 Acceleration{std::move(Acceleration)},
152 EnableFOC{std::move(EnableFOC)},
153 FeedForward{std::move(FeedForward)},
154 Slot{std::move(Slot)},
156 {}
157
158 /**
159 * \brief Request PID to target velocity with voltage feedforward.
160 *
161 * \details This control mode will set the motor's velocity setpoint to the
162 * velocity specified by the user. In addition, it will apply an
163 * additional voltage as an arbitrary feedforward value.
164 *
165 * \param Velocity Velocity to drive toward in rotations per second.
166 */
167 VelocityVoltage(units::angular_velocity::turns_per_second_t Velocity) : VelocityVoltage{Velocity, 0.0_tr_per_s_sq, true, 0.0_V, 0, false}
168 {}
169
170 /**
171 * \brief Modifies this Control Request's Velocity parameter and returns itself for
172 * method-chaining and easier to use request API.
173 * \param newVelocity Parameter to modify
174 * \returns Itself
175 */
176 VelocityVoltage& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
177 {
178 Velocity = std::move(newVelocity);
179 return *this;
180 }
181
182 /**
183 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
184 * method-chaining and easier to use request API.
185 * \param newAcceleration Parameter to modify
186 * \returns Itself
187 */
188 VelocityVoltage& WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
189 {
190 Acceleration = std::move(newAcceleration);
191 return *this;
192 }
193
194 /**
195 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
196 * method-chaining and easier to use request API.
197 * \param newEnableFOC Parameter to modify
198 * \returns Itself
199 */
200 VelocityVoltage& WithEnableFOC(bool newEnableFOC)
201 {
202 EnableFOC = std::move(newEnableFOC);
203 return *this;
204 }
205
206 /**
207 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
208 * method-chaining and easier to use request API.
209 * \param newFeedForward Parameter to modify
210 * \returns Itself
211 */
212 VelocityVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
213 {
214 FeedForward = std::move(newFeedForward);
215 return *this;
216 }
217
218 /**
219 * \brief Modifies this Control Request's Slot parameter and returns itself for
220 * method-chaining and easier to use request API.
221 * \param newSlot Parameter to modify
222 * \returns Itself
223 */
225 {
226 Slot = std::move(newSlot);
227 return *this;
228 }
229
230 /**
231 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
232 * method-chaining and easier to use request API.
233 * \param newOverrideBrakeDurNeutral Parameter to modify
234 * \returns Itself
235 */
236 VelocityVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
237 {
238 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
239 return *this;
240 }
241 /**
242 * \brief Sets the period at which this control will update at.
243 * This is designated in Hertz, with a minimum of 20 Hz
244 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
245 *
246 * If this field is set to 0 Hz, the control request will
247 * be sent immediately as a one-shot frame. This may be useful
248 * for advanced applications that require outputs to be
249 * synchronized with data acquisition. In this case, we
250 * recommend not exceeding 50 ms between control calls.
251 *
252 * \param newUpdateFreqHz Parameter to modify
253 * \returns Itself
254 */
255 VelocityVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
256 {
257 UpdateFreqHz = newUpdateFreqHz;
258 return *this;
259 }
260 /**
261 * Returns a string representation of the object.
262 *
263 * \returns a string representation of the object.
264 */
265 std::string ToString() const override
266 {
267 std::stringstream ss;
268 ss << "class: VelocityVoltage" << std::endl;
269 ss << "Velocity: " << Velocity.to<double>() << std::endl;
270 ss << "Acceleration: " << Acceleration.to<double>() << std::endl;
271 ss << "EnableFOC: " << EnableFOC << std::endl;
272 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
273 ss << "Slot: " << Slot << std::endl;
274 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
275 return ss.str();
276 }
277
278 /**
279 * \brief Forces configs to be applied the next time this is used in a setControl.
280 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
281 * properly set when they are not already set
282 */
283 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
284
285 /**
286 * \brief Gets information about this control request.
287 *
288 * \returns Map of control parameter names and corresponding applied values
289 */
290 std::map<std::string, std::string> GetControlInfo() const override
291 {
292 std::map<std::string, std::string> controlInfo;
293 std::stringstream ss;
294 controlInfo["Name"] = GetName();
295 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
296 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
297 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
298 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
299 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
300 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
301 return controlInfo;
302 }
303};
304
305}
306}
307}
308
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_RequestControlVelocityVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Velocity, double Acceleration, bool EnableFOC, double FeedForward, int Slot, 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 voltage feedforward.
Definition: VelocityVoltage.hpp:31
std::string ToString() const override
Returns a string representation of the object.
Definition: VelocityVoltage.hpp:265
VelocityVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: VelocityVoltage.hpp:236
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: VelocityVoltage.hpp:100
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: VelocityVoltage.hpp:113
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: VelocityVoltage.hpp:61
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: VelocityVoltage.hpp:93
VelocityVoltage & 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: VelocityVoltage.hpp:176
VelocityVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: VelocityVoltage.hpp:212
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: VelocityVoltage.hpp:290
VelocityVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: VelocityVoltage.hpp:200
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration to drive toward in rotations per second squared.
Definition: VelocityVoltage.hpp:65
VelocityVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: VelocityVoltage.hpp:224
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: VelocityVoltage.hpp:80
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: VelocityVoltage.hpp:283
int Slot
Select which gains are applied by selecting the slot.
Definition: VelocityVoltage.hpp:86
VelocityVoltage(units::angular_velocity::turns_per_second_t Velocity)
Request PID to target velocity with voltage feedforward.
Definition: VelocityVoltage.hpp:167
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: VelocityVoltage.hpp:76
VelocityVoltage & 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: VelocityVoltage.hpp:188
VelocityVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: VelocityVoltage.hpp:255
VelocityVoltage(units::angular_velocity::turns_per_second_t Velocity, units::angular_acceleration::turns_per_second_squared_t Acceleration, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral)
Request PID to target velocity with voltage feedforward.
Definition: VelocityVoltage.hpp:149
Definition: ManualEvent.hpp:12