CTRE Phoenix 6 C++ 24.2.0
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, bool cancelOtherRequests, 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
48 return c_ctre_phoenix6_RequestControlPositionTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), Velocity.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral, LimitForwardMotion, LimitReverseMotion);
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 /**
93 * \brief The period at which this control will update at.
94 * This is designated in Hertz, with a minimum of 20 Hz
95 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
96 *
97 * If this field is set to 0 Hz, the control request will
98 * be sent immediately as a one-shot frame. This may be useful
99 * for advanced applications that require outputs to be
100 * synchronized with data acquisition. In this case, we
101 * recommend not exceeding 50 ms between control calls.
102 */
103 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
104
105 /**
106 * \brief Requires Phoenix Pro;
107 * Request PID to target position with torque
108 * current feedforward.
109 *
110 * \details This control mode will set the motor's position setpoint to the
111 * position specified by the user. In addition, it will apply an
112 * additional torque current as an arbitrary feedforward value.
113 *
114 * \param Position Position to drive toward in rotations.
115 * \param Velocity Velocity to drive toward in rotations per second. This is
116 * typically used for motion profiles generated by the robot
117 * program.
118 * \param FeedForward Feedforward to apply in torque current in Amperes.
119 * User can use motor's kT to scale Newton-meter to
120 * Amperes.
121 * \param Slot Select which gains are applied by selecting the slot. Use the
122 * configuration api to set the gain values for the selected slot
123 * before enabling this feature. Slot must be within [0,2].
124 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
125 * is zero (or within deadband). Set to false
126 * to use the NeutralMode configuration
127 * setting (default). This flag exists to
128 * provide the fundamental behavior of this
129 * control when output is zero, which is to
130 * provide 0A (zero torque).
131 * \param LimitForwardMotion Set to true to force forward limiting. This
132 * allows users to use other limit switch sensors
133 * connected to robot controller. This also allows
134 * use of active sensors that require external
135 * power.
136 * \param LimitReverseMotion Set to true to force reverse limiting. This
137 * allows users to use other limit switch sensors
138 * connected to robot controller. This also allows
139 * use of active sensors that require external
140 * power.
141 */
142 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) : ControlRequest{"PositionTorqueCurrentFOC"},
143 Position{std::move(Position)},
144 Velocity{std::move(Velocity)},
145 FeedForward{std::move(FeedForward)},
146 Slot{std::move(Slot)},
150 {}
151
152 /**
153 * \brief Modifies this Control Request's Position parameter and returns itself for
154 * method-chaining and easier to use request API.
155 * \param newPosition Parameter to modify
156 * \returns Itself
157 */
158 PositionTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
159 {
160 Position = std::move(newPosition);
161 return *this;
162 }
163
164 /**
165 * \brief Modifies this Control Request's Velocity parameter and returns itself for
166 * method-chaining and easier to use request API.
167 * \param newVelocity Parameter to modify
168 * \returns Itself
169 */
170 PositionTorqueCurrentFOC& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
171 {
172 Velocity = std::move(newVelocity);
173 return *this;
174 }
175
176 /**
177 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
178 * method-chaining and easier to use request API.
179 * \param newFeedForward Parameter to modify
180 * \returns Itself
181 */
182 PositionTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
183 {
184 FeedForward = std::move(newFeedForward);
185 return *this;
186 }
187
188 /**
189 * \brief Modifies this Control Request's Slot parameter and returns itself for
190 * method-chaining and easier to use request API.
191 * \param newSlot Parameter to modify
192 * \returns Itself
193 */
195 {
196 Slot = std::move(newSlot);
197 return *this;
198 }
199
200 /**
201 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
202 * method-chaining and easier to use request API.
203 * \param newOverrideCoastDurNeutral Parameter to modify
204 * \returns Itself
205 */
207 {
208 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
209 return *this;
210 }
211
212 /**
213 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
214 * method-chaining and easier to use request API.
215 * \param newLimitForwardMotion Parameter to modify
216 * \returns Itself
217 */
219 {
220 LimitForwardMotion = std::move(newLimitForwardMotion);
221 return *this;
222 }
223
224 /**
225 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
226 * method-chaining and easier to use request API.
227 * \param newLimitReverseMotion Parameter to modify
228 * \returns Itself
229 */
231 {
232 LimitReverseMotion = std::move(newLimitReverseMotion);
233 return *this;
234 }
235 /**
236 * \brief Sets the period at which this control will update at.
237 * This is designated in Hertz, with a minimum of 20 Hz
238 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
239 *
240 * If this field is set to 0 Hz, the control request will
241 * be sent immediately as a one-shot frame. This may be useful
242 * for advanced applications that require outputs to be
243 * synchronized with data acquisition. In this case, we
244 * recommend not exceeding 50 ms between control calls.
245 *
246 * \param newUpdateFreqHz Parameter to modify
247 * \returns Itself
248 */
249 PositionTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
250 {
251 UpdateFreqHz = newUpdateFreqHz;
252 return *this;
253 }
254 /**
255 * Returns a string representation of the object.
256 *
257 * \returns a string representation of the object.
258 */
259 std::string ToString() const override
260 {
261 std::stringstream ss;
262 ss << "class: PositionTorqueCurrentFOC" << std::endl;
263 ss << "Position: " << Position.to<double>() << std::endl;
264 ss << "Velocity: " << Velocity.to<double>() << std::endl;
265 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
266 ss << "Slot: " << Slot << std::endl;
267 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
268 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
269 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
270 return ss.str();
271 }
272
273 /**
274 * \brief Gets information about this control request.
275 *
276 * \returns Map of control parameter names and corresponding applied values
277 */
278 std::map<std::string, std::string> GetControlInfo() const override
279 {
280 std::map<std::string, std::string> controlInfo;
281 std::stringstream ss;
282 controlInfo["Name"] = GetName();
283 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
284 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
285 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
286 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
287 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
288 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
289 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
290 return controlInfo;
291 }
292};
293
294}
295}
296}
297
CTREXPORT int c_ctre_phoenix6_RequestControlPositionTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, double Velocity, double FeedForward, int Slot, bool OverrideCoastDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
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
PositionTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: PositionTorqueCurrentFOC.hpp:206
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)
Requires Phoenix Pro; Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:142
PositionTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionTorqueCurrentFOC.hpp:194
PositionTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: PositionTorqueCurrentFOC.hpp:230
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionTorqueCurrentFOC.hpp:259
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:158
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:103
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:170
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:249
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:278
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:182
PositionTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: PositionTorqueCurrentFOC.hpp:218
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: PositionTorqueCurrentFOC.hpp:78
Definition: RcManualEvent.hpp:12