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