CTRE Phoenix 6 C++ 25.3.0
Loading...
Searching...
No Matches
PositionTorqueCurrentFOC.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/angle.h>
13#include <units/angular_velocity.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 position with torque current feedforward.
26 *
27 * This control mode will set the motor's position setpoint to the position 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<PositionTorqueCurrentFOC *>(req.get());
37 if (reqCast != nullptr)
38 {
39 *reqCast = *this;
40 }
41 else
42 {
43 req = std::make_shared<PositionTorqueCurrentFOC>(*this);
44 }
45 }
46
48 }
49
50public:
51 /**
52 * \brief Position to drive toward in rotations.
53 *
54 * - Units: rotations
55 *
56 */
57 units::angle::turn_t Position;
58 /**
59 * \brief Velocity to drive toward in rotations per second. This is typically
60 * used for motion profiles generated by the robot program.
61 *
62 * - Units: rotations per second
63 *
64 */
65 units::angular_velocity::turns_per_second_t Velocity = 0.0_tps;
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 position with torque current feedforward.
140 *
141 * \details This control mode will set the motor's position setpoint to the
142 * position specified by the user. In addition, it will apply an
143 * additional torque current as an arbitrary feedforward value.
144 *
145 * \param Position Position to drive toward in rotations.
146 */
147 PositionTorqueCurrentFOC(units::angle::turn_t Position) : ControlRequest{"PositionTorqueCurrentFOC"},
148 Position{std::move(Position)}
149 {}
150
151 /**
152 * \brief Modifies this Control Request's Position parameter and returns itself for
153 * method-chaining and easier to use request API.
154 *
155 * Position to drive toward in rotations.
156 *
157 * - Units: rotations
158 *
159 *
160 * \param newPosition Parameter to modify
161 * \returns Itself
162 */
163 PositionTorqueCurrentFOC &WithPosition(units::angle::turn_t newPosition)
164 {
165 Position = std::move(newPosition);
166 return *this;
167 }
168
169 /**
170 * \brief Modifies this Control Request's Velocity parameter and returns itself for
171 * method-chaining and easier to use request API.
172 *
173 * Velocity to drive toward in rotations per second. This is typically used for
174 * motion profiles generated by the robot program.
175 *
176 * - Units: rotations per second
177 *
178 *
179 * \param newVelocity Parameter to modify
180 * \returns Itself
181 */
182 PositionTorqueCurrentFOC &WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
183 {
184 Velocity = std::move(newVelocity);
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 PositionTorqueCurrentFOC &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 PositionTorqueCurrentFOC &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: PositionTorqueCurrentFOC" << std::endl;
348 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
349 ss << " Velocity: " << Velocity.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 << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
371 ss << Velocity.to<double>(); controlInfo["Velocity"] = 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_RequestControlPositionTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, double Position, double Velocity, 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 position with torque current feedforward.
Definition PositionTorqueCurrentFOC.hpp:31
PositionTorqueCurrentFOC & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition PositionTorqueCurrentFOC.hpp:293
int Slot
Select which gains are applied by selecting the slot.
Definition PositionTorqueCurrentFOC.hpp:79
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition PositionTorqueCurrentFOC.hpp:65
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition PositionTorqueCurrentFOC.hpp:122
PositionTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition PositionTorqueCurrentFOC.hpp:236
PositionTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition PositionTorqueCurrentFOC.hpp:218
PositionTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition PositionTorqueCurrentFOC.hpp:270
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition PositionTorqueCurrentFOC.hpp:111
std::string ToString() const override
Returns a string representation of the object.
Definition PositionTorqueCurrentFOC.hpp:344
PositionTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition PositionTorqueCurrentFOC.hpp:163
units::angle::turn_t Position
Position to drive toward in rotations.
Definition PositionTorqueCurrentFOC.hpp:57
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition PositionTorqueCurrentFOC.hpp:135
PositionTorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition PositionTorqueCurrentFOC.hpp:315
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition PositionTorqueCurrentFOC.hpp:73
PositionTorqueCurrentFOC & 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 PositionTorqueCurrentFOC.hpp:182
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition PositionTorqueCurrentFOC.hpp:98
PositionTorqueCurrentFOC(units::angle::turn_t Position)
Requires Phoenix Pro; Request PID to target position with torque current feedforward.
Definition PositionTorqueCurrentFOC.hpp:147
PositionTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition PositionTorqueCurrentFOC.hpp:334
bool LimitForwardMotion
Set to true to force forward limiting.
Definition PositionTorqueCurrentFOC.hpp:92
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition PositionTorqueCurrentFOC.hpp:365
PositionTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition PositionTorqueCurrentFOC.hpp:201
PositionTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition PositionTorqueCurrentFOC.hpp:253
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition PositionTorqueCurrentFOC.hpp:86
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401