CTRE Phoenix 6 C++ 25.2.1
Loading...
Searching...
No Matches
VelocityTorqueCurrentFOC.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
11#include <sstream>
12#include <units/angular_velocity.h>
13#include <units/angular_acceleration.h>
14#include <units/current.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Requires Phoenix Pro;
25 * Request PID to target velocity with torque current 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 torque current as an arbitrary feedforward value.
29 */
31{
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
33 {
34 if (req.get() != this)
35 {
36 auto const reqCast = dynamic_cast<VelocityTorqueCurrentFOC *>(req.get());
37 if (reqCast != nullptr)
38 {
39 *reqCast = *this;
40 }
41 else
42 {
43 req = std::make_shared<VelocityTorqueCurrentFOC>(*this);
44 }
45 }
46
48 }
49
50public:
51 /**
52 * \brief Velocity to drive toward in rotations per second.
53 */
54 units::angular_velocity::turns_per_second_t Velocity;
55 /**
56 * \brief Acceleration to drive toward in rotations per second squared. This is
57 * typically used for motion profiles generated by the robot program.
58 */
59 units::angular_acceleration::turns_per_second_squared_t Acceleration = 0.0_tr_per_s_sq;
60 /**
61 * \brief Feedforward to apply in torque current in Amperes. User can use
62 * motor's kT to scale Newton-meter to Amperes.
63 */
64 units::current::ampere_t FeedForward = 0.0_A;
65 /**
66 * \brief Select which gains are applied by selecting the slot. Use the
67 * configuration api to set the gain values for the selected slot before
68 * enabling this feature. Slot must be within [0,2].
69 */
70 int Slot = 0;
71 /**
72 * \brief Set to true to coast the rotor when output is zero (or within
73 * deadband). Set to false to use the NeutralMode configuration setting
74 * (default). This flag exists to provide the fundamental behavior of this
75 * control when output is zero, which is to provide 0A (zero torque).
76 */
78 /**
79 * \brief Set to true to force forward limiting. This allows users to use other
80 * limit switch sensors connected to robot controller. This also allows use of
81 * active sensors that require external power.
82 */
83 bool LimitForwardMotion = false;
84 /**
85 * \brief Set to true to force reverse limiting. This allows users to use other
86 * limit switch sensors connected to robot controller. This also allows use of
87 * active sensors that require external power.
88 */
89 bool LimitReverseMotion = false;
90 /**
91 * \brief Set to true to ignore hardware limit switches and the
92 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
93 * motion.
94 *
95 * This can be useful on mechanisms such as an intake/feeder, where a limit
96 * switch stops motion while intaking but should be ignored when feeding to a
97 * shooter.
98 *
99 * The hardware limit faults and Forward/ReverseLimit signals will still report
100 * the values of the limit switches regardless of this parameter.
101 */
103 /**
104 * \brief Set to true to delay applying this control request until a timesync
105 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
106 * nondeterministic network delays in exchange for a larger but deterministic
107 * control latency.
108 *
109 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
110 * Additionally, when this is enabled, the UpdateFreqHz of this request should
111 * be set to 0 Hz.
112 */
113 bool UseTimesync = false;
114
115 /**
116 * \brief The period at which this control will update at.
117 * This is designated in Hertz, with a minimum of 20 Hz
118 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
119 *
120 * If this field is set to 0 Hz, the control request will
121 * be sent immediately as a one-shot frame. This may be useful
122 * for advanced applications that require outputs to be
123 * synchronized with data acquisition. In this case, we
124 * recommend not exceeding 50 ms between control calls.
125 */
126 units::frequency::hertz_t UpdateFreqHz{100_Hz};
127
128 /**
129 * \brief Requires Phoenix Pro;
130 * Request PID to target velocity with torque current feedforward.
131 *
132 * \details This control mode will set the motor's velocity setpoint to the
133 * velocity specified by the user. In addition, it will apply an
134 * additional torque current as an arbitrary feedforward value.
135 *
136 * \param Velocity Velocity to drive toward in rotations per second.
137 */
138 VelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity) : ControlRequest{"VelocityTorqueCurrentFOC"},
139 Velocity{std::move(Velocity)}
140 {}
141
142 /**
143 * \brief Modifies this Control Request's Velocity parameter and returns itself for
144 * method-chaining and easier to use request API.
145 *
146 * Velocity to drive toward in rotations per second.
147 *
148 * \param newVelocity Parameter to modify
149 * \returns Itself
150 */
151 VelocityTorqueCurrentFOC &WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
152 {
153 Velocity = std::move(newVelocity);
154 return *this;
155 }
156
157 /**
158 * \brief Modifies this Control Request's Acceleration parameter and returns itself for
159 * method-chaining and easier to use request API.
160 *
161 * Acceleration to drive toward in rotations per second squared. This is
162 * typically used for motion profiles generated by the robot program.
163 *
164 * \param newAcceleration Parameter to modify
165 * \returns Itself
166 */
167 VelocityTorqueCurrentFOC &WithAcceleration(units::angular_acceleration::turns_per_second_squared_t newAcceleration)
168 {
169 Acceleration = std::move(newAcceleration);
170 return *this;
171 }
172
173 /**
174 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
175 * method-chaining and easier to use request API.
176 *
177 * Feedforward to apply in torque current in Amperes. User can use motor's kT
178 * to scale Newton-meter to Amperes.
179 *
180 * \param newFeedForward Parameter to modify
181 * \returns Itself
182 */
183 VelocityTorqueCurrentFOC &WithFeedForward(units::current::ampere_t newFeedForward)
184 {
185 FeedForward = std::move(newFeedForward);
186 return *this;
187 }
188
189 /**
190 * \brief Modifies this Control Request's Slot parameter and returns itself for
191 * method-chaining and easier to use request API.
192 *
193 * Select which gains are applied by selecting the slot. Use the configuration
194 * api to set the gain values for the selected slot before enabling this
195 * feature. Slot must be within [0,2].
196 *
197 * \param newSlot Parameter to modify
198 * \returns Itself
199 */
201 {
202 Slot = std::move(newSlot);
203 return *this;
204 }
205
206 /**
207 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
208 * method-chaining and easier to use request API.
209 *
210 * Set to true to coast the rotor when output is zero (or within deadband). Set
211 * to false to use the NeutralMode configuration setting (default). This flag
212 * exists to provide the fundamental behavior of this control when output is
213 * zero, which is to provide 0A (zero torque).
214 *
215 * \param newOverrideCoastDurNeutral Parameter to modify
216 * \returns Itself
217 */
219 {
220 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
221 return *this;
222 }
223
224 /**
225 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
226 * method-chaining and easier to use request API.
227 *
228 * Set to true to force forward limiting. This allows users to use other limit
229 * switch sensors connected to robot controller. This also allows use of active
230 * sensors that require external power.
231 *
232 * \param newLimitForwardMotion Parameter to modify
233 * \returns Itself
234 */
236 {
237 LimitForwardMotion = std::move(newLimitForwardMotion);
238 return *this;
239 }
240
241 /**
242 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
243 * method-chaining and easier to use request API.
244 *
245 * Set to true to force reverse limiting. This allows users to use other limit
246 * switch sensors connected to robot controller. This also allows use of active
247 * sensors that require external power.
248 *
249 * \param newLimitReverseMotion Parameter to modify
250 * \returns Itself
251 */
253 {
254 LimitReverseMotion = std::move(newLimitReverseMotion);
255 return *this;
256 }
257
258 /**
259 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
260 * method-chaining and easier to use request API.
261 *
262 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
263 * LimitReverseMotion parameters, instead allowing motion.
264 *
265 * This can be useful on mechanisms such as an intake/feeder, where a limit
266 * switch stops motion while intaking but should be ignored when feeding to a
267 * shooter.
268 *
269 * The hardware limit faults and Forward/ReverseLimit signals will still report
270 * the values of the limit switches regardless of this parameter.
271 *
272 * \param newIgnoreHardwareLimits Parameter to modify
273 * \returns Itself
274 */
276 {
277 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
278 return *this;
279 }
280
281 /**
282 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
283 * method-chaining and easier to use request API.
284 *
285 * Set to true to delay applying this control request until a timesync boundary
286 * (requires Phoenix Pro and CANivore). This eliminates the impact of
287 * nondeterministic network delays in exchange for a larger but deterministic
288 * control latency.
289 *
290 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
291 * Additionally, when this is enabled, the UpdateFreqHz of this request should
292 * be set to 0 Hz.
293 *
294 * \param newUseTimesync Parameter to modify
295 * \returns Itself
296 */
298 {
299 UseTimesync = std::move(newUseTimesync);
300 return *this;
301 }
302 /**
303 * \brief Sets the period at which this control will update at.
304 * This is designated in Hertz, with a minimum of 20 Hz
305 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
306 *
307 * If this field is set to 0 Hz, the control request will
308 * be sent immediately as a one-shot frame. This may be useful
309 * for advanced applications that require outputs to be
310 * synchronized with data acquisition. In this case, we
311 * recommend not exceeding 50 ms between control calls.
312 *
313 * \param newUpdateFreqHz Parameter to modify
314 * \returns Itself
315 */
316 VelocityTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
317 {
318 UpdateFreqHz = newUpdateFreqHz;
319 return *this;
320 }
321 /**
322 * \brief Returns a string representation of the object.
323 *
324 * \returns a string representation of the object.
325 */
326 std::string ToString() const override
327 {
328 std::stringstream ss;
329 ss << "Control: VelocityTorqueCurrentFOC" << std::endl;
330 ss << " Velocity: " << Velocity.to<double>() << " rotations per second" << std::endl;
331 ss << " Acceleration: " << Acceleration.to<double>() << " rotations per second²" << std::endl;
332 ss << " FeedForward: " << FeedForward.to<double>() << " A" << std::endl;
333 ss << " Slot: " << Slot << std::endl;
334 ss << " OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
335 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
336 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
337 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
338 ss << " UseTimesync: " << UseTimesync << std::endl;
339 return ss.str();
340 }
341
342 /**
343 * \brief Gets information about this control request.
344 *
345 * \returns Map of control parameter names and corresponding applied values
346 */
347 std::map<std::string, std::string> GetControlInfo() const override
348 {
349 std::map<std::string, std::string> controlInfo;
350 std::stringstream ss;
351 controlInfo["Name"] = GetName();
352 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
353 ss << Acceleration.to<double>(); controlInfo["Acceleration"] = ss.str(); ss.str(std::string{});
354 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
355 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
356 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
357 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
358 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
359 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
360 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
361 return controlInfo;
362 }
363};
364
365}
366}
367}
368
CTREXPORT int c_ctre_phoenix6_RequestControlVelocityTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, double Velocity, double Acceleration, double FeedForward, int Slot, bool OverrideCoastDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion, bool IgnoreHardwareLimits, bool UseTimesync)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Requires Phoenix Pro; Request PID to target velocity with torque current feedforward.
Definition VelocityTorqueCurrentFOC.hpp:31
units::angular_acceleration::turns_per_second_squared_t Acceleration
Acceleration to drive toward in rotations per second squared.
Definition VelocityTorqueCurrentFOC.hpp:59
VelocityTorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition VelocityTorqueCurrentFOC.hpp:297
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition VelocityTorqueCurrentFOC.hpp:102
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition VelocityTorqueCurrentFOC.hpp:347
VelocityTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition VelocityTorqueCurrentFOC.hpp:218
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition VelocityTorqueCurrentFOC.hpp:77
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition VelocityTorqueCurrentFOC.hpp:54
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition VelocityTorqueCurrentFOC.hpp:64
VelocityTorqueCurrentFOC & 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 VelocityTorqueCurrentFOC.hpp:151
VelocityTorqueCurrentFOC & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition VelocityTorqueCurrentFOC.hpp:275
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition VelocityTorqueCurrentFOC.hpp:126
VelocityTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition VelocityTorqueCurrentFOC.hpp:183
VelocityTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition VelocityTorqueCurrentFOC.hpp:200
VelocityTorqueCurrentFOC(units::angular_velocity::turns_per_second_t Velocity)
Requires Phoenix Pro; Request PID to target velocity with torque current feedforward.
Definition VelocityTorqueCurrentFOC.hpp:138
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition VelocityTorqueCurrentFOC.hpp:113
VelocityTorqueCurrentFOC & 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 VelocityTorqueCurrentFOC.hpp:167
int Slot
Select which gains are applied by selecting the slot.
Definition VelocityTorqueCurrentFOC.hpp:70
VelocityTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition VelocityTorqueCurrentFOC.hpp:235
std::string ToString() const override
Returns a string representation of the object.
Definition VelocityTorqueCurrentFOC.hpp:326
VelocityTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition VelocityTorqueCurrentFOC.hpp:252
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition VelocityTorqueCurrentFOC.hpp:89
bool LimitForwardMotion
Set to true to force forward limiting.
Definition VelocityTorqueCurrentFOC.hpp:83
VelocityTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition VelocityTorqueCurrentFOC.hpp:316
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401