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