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