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