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