CTRE Phoenix 6 C++ 24.3.0
MotionMagicTorqueCurrentFOC.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/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 * Requests Motion Magic® to target a final position using a motion profile.
26 * Users can optionally provide a torque current feedforward.
27 *
28 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
29 * Acceleration, and Jerk value specified via the Motion Magic® configuration values. This control mode does
30 * not use the Expo_kV or Expo_kA configs. Target position can be changed on-the-fly and Motion Magic® will
31 * do its best to adjust the profile. This control mode is based on torque current, so relevant closed-loop
32 * gains will use Amperes for the numerator.
33 */
35{
36 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
37 {
38 if (req.get() != this)
39 {
40 auto const reqCast = dynamic_cast<MotionMagicTorqueCurrentFOC *>(req.get());
41 if (reqCast != nullptr)
42 {
43 *reqCast = *this;
44 }
45 else
46 {
47 req = std::make_shared<MotionMagicTorqueCurrentFOC>(*this);
48 }
49 }
50
51 return c_ctre_phoenix6_RequestControlMotionMagicTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral, LimitForwardMotion, LimitReverseMotion);
52 }
53
54public:
55 /**
56 * Position to drive toward in rotations.
57 */
58 units::angle::turn_t Position;
59 /**
60 * Feedforward to apply in torque current in Amperes. User can use motor's kT
61 * to scale Newton-meter to Amperes.
62 */
63 units::current::ampere_t FeedForward;
64 /**
65 * Select which gains are applied by selecting the slot. Use the configuration
66 * api to set the gain values for the selected slot before enabling this
67 * feature. Slot must be within [0,2].
68 */
69 int Slot;
70 /**
71 * Set to true to coast the rotor when output is zero (or within deadband). Set
72 * to false to use the NeutralMode configuration setting (default). This flag
73 * exists to provide the fundamental behavior of this control when output is
74 * zero, which is to provide 0A (zero torque).
75 */
77 /**
78 * Set to true to force forward limiting. This allows users to use other limit
79 * switch sensors connected to robot controller. This also allows use of active
80 * sensors that require external power.
81 */
83 /**
84 * Set to true to force reverse limiting. This allows users to use other limit
85 * switch sensors connected to robot controller. This also allows use of active
86 * sensors that require external power.
87 */
89
90 /**
91 * \brief The period at which this control will update at.
92 * This is designated in Hertz, with a minimum of 20 Hz
93 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
94 *
95 * If this field is set to 0 Hz, the control request will
96 * be sent immediately as a one-shot frame. This may be useful
97 * for advanced applications that require outputs to be
98 * synchronized with data acquisition. In this case, we
99 * recommend not exceeding 50 ms between control calls.
100 */
101 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
102
103 /**
104 * \brief Requires Phoenix Pro;
105 * Requests Motion Magic® to target a final position using a motion
106 * profile. Users can optionally provide a torque current feedforward.
107 *
108 * \details Motion Magic® produces a motion profile in real-time while
109 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
110 * value specified via the Motion Magic® configuration values. This
111 * control mode does not use the Expo_kV or Expo_kA configs. Target
112 * position can be changed on-the-fly and Motion Magic® will do its
113 * best to adjust the profile. This control mode is based on torque
114 * current, so relevant closed-loop gains will use Amperes for the
115 * numerator.
116 *
117 * \param Position Position to drive toward in rotations.
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 MotionMagicTorqueCurrentFOC(units::angle::turn_t Position, units::current::ampere_t FeedForward = 0.0_A, int Slot = 0, bool OverrideCoastDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"MotionMagicTorqueCurrentFOC"},
143 Position{std::move(Position)},
144 FeedForward{std::move(FeedForward)},
145 Slot{std::move(Slot)},
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 * \param newPosition Parameter to modify
158 * \returns Itself
159 */
160 MotionMagicTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
161 {
162 Position = std::move(newPosition);
163 return *this;
164 }
165
166 /**
167 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
168 * method-chaining and easier to use request API.
169 *
170 * Feedforward to apply in torque current in Amperes. User can use motor's kT
171 * to scale Newton-meter to Amperes.
172 *
173 * \param newFeedForward Parameter to modify
174 * \returns Itself
175 */
176 MotionMagicTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
177 {
178 FeedForward = std::move(newFeedForward);
179 return *this;
180 }
181
182 /**
183 * \brief Modifies this Control Request's Slot parameter and returns itself for
184 * method-chaining and easier to use request API.
185 *
186 * Select which gains are applied by selecting the slot. Use the configuration
187 * api to set the gain values for the selected slot before enabling this
188 * feature. Slot must be within [0,2].
189 *
190 * \param newSlot Parameter to modify
191 * \returns Itself
192 */
194 {
195 Slot = std::move(newSlot);
196 return *this;
197 }
198
199 /**
200 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
201 * method-chaining and easier to use request API.
202 *
203 * Set to true to coast the rotor when output is zero (or within deadband). Set
204 * to false to use the NeutralMode configuration setting (default). This flag
205 * exists to provide the fundamental behavior of this control when output is
206 * zero, which is to provide 0A (zero torque).
207 *
208 * \param newOverrideCoastDurNeutral Parameter to modify
209 * \returns Itself
210 */
212 {
213 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
214 return *this;
215 }
216
217 /**
218 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
219 * method-chaining and easier to use request API.
220 *
221 * Set to true to force forward limiting. This allows users to use other limit
222 * switch sensors connected to robot controller. This also allows use of active
223 * sensors that require external power.
224 *
225 * \param newLimitForwardMotion Parameter to modify
226 * \returns Itself
227 */
229 {
230 LimitForwardMotion = std::move(newLimitForwardMotion);
231 return *this;
232 }
233
234 /**
235 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
236 * method-chaining and easier to use request API.
237 *
238 * Set to true to force reverse limiting. This allows users to use other limit
239 * switch sensors connected to robot controller. This also allows use of active
240 * sensors that require external power.
241 *
242 * \param newLimitReverseMotion Parameter to modify
243 * \returns Itself
244 */
246 {
247 LimitReverseMotion = std::move(newLimitReverseMotion);
248 return *this;
249 }
250 /**
251 * \brief Sets the period at which this control will update at.
252 * This is designated in Hertz, with a minimum of 20 Hz
253 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
254 *
255 * If this field is set to 0 Hz, the control request will
256 * be sent immediately as a one-shot frame. This may be useful
257 * for advanced applications that require outputs to be
258 * synchronized with data acquisition. In this case, we
259 * recommend not exceeding 50 ms between control calls.
260 *
261 * \param newUpdateFreqHz Parameter to modify
262 * \returns Itself
263 */
264 MotionMagicTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
265 {
266 UpdateFreqHz = newUpdateFreqHz;
267 return *this;
268 }
269 /**
270 * Returns a string representation of the object.
271 *
272 * \returns a string representation of the object.
273 */
274 std::string ToString() const override
275 {
276 std::stringstream ss;
277 ss << "class: MotionMagicTorqueCurrentFOC" << std::endl;
278 ss << "Position: " << Position.to<double>() << std::endl;
279 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
280 ss << "Slot: " << Slot << std::endl;
281 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
282 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
283 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
284 return ss.str();
285 }
286
287 /**
288 * \brief Gets information about this control request.
289 *
290 * \returns Map of control parameter names and corresponding applied values
291 */
292 std::map<std::string, std::string> GetControlInfo() const override
293 {
294 std::map<std::string, std::string> controlInfo;
295 std::stringstream ss;
296 controlInfo["Name"] = GetName();
297 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
298 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
299 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
300 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
301 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
302 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
303 return controlInfo;
304 }
305};
306
307}
308}
309}
310
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, 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; Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicTorqueCurrentFOC.hpp:35
MotionMagicTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicTorqueCurrentFOC.hpp:245
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicTorqueCurrentFOC.hpp:101
MotionMagicTorqueCurrentFOC(units::angle::turn_t Position, units::current::ampere_t FeedForward=0.0_A, int Slot=0, bool OverrideCoastDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Requires Phoenix Pro; Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicTorqueCurrentFOC.hpp:142
MotionMagicTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicTorqueCurrentFOC.hpp:264
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: MotionMagicTorqueCurrentFOC.hpp:63
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicTorqueCurrentFOC.hpp:58
MotionMagicTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicTorqueCurrentFOC.hpp:193
MotionMagicTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicTorqueCurrentFOC.hpp:228
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicTorqueCurrentFOC.hpp:69
MotionMagicTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: MotionMagicTorqueCurrentFOC.hpp:176
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: MotionMagicTorqueCurrentFOC.hpp:76
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: MotionMagicTorqueCurrentFOC.hpp:88
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicTorqueCurrentFOC.hpp:274
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: MotionMagicTorqueCurrentFOC.hpp:82
MotionMagicTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: MotionMagicTorqueCurrentFOC.hpp:160
MotionMagicTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicTorqueCurrentFOC.hpp:211
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicTorqueCurrentFOC.hpp:292
Definition: string_util.hpp:15