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