CTRE Phoenix 6 C++ 24.2.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
107 * position using an exponential motion profile. Users can optionally
108 * provide a torque current 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 * \param newPosition Parameter to modify
158 * \returns Itself
159 */
160 MotionMagicExpoTorqueCurrentFOC& 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 * \param newFeedForward Parameter to modify
170 * \returns Itself
171 */
172 MotionMagicExpoTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
173 {
174 FeedForward = std::move(newFeedForward);
175 return *this;
176 }
177
178 /**
179 * \brief Modifies this Control Request's Slot parameter and returns itself for
180 * method-chaining and easier to use request API.
181 * \param newSlot Parameter to modify
182 * \returns Itself
183 */
185 {
186 Slot = std::move(newSlot);
187 return *this;
188 }
189
190 /**
191 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
192 * method-chaining and easier to use request API.
193 * \param newOverrideCoastDurNeutral Parameter to modify
194 * \returns Itself
195 */
197 {
198 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
199 return *this;
200 }
201
202 /**
203 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
204 * method-chaining and easier to use request API.
205 * \param newLimitForwardMotion Parameter to modify
206 * \returns Itself
207 */
209 {
210 LimitForwardMotion = std::move(newLimitForwardMotion);
211 return *this;
212 }
213
214 /**
215 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
216 * method-chaining and easier to use request API.
217 * \param newLimitReverseMotion Parameter to modify
218 * \returns Itself
219 */
221 {
222 LimitReverseMotion = std::move(newLimitReverseMotion);
223 return *this;
224 }
225 /**
226 * \brief Sets the period at which this control will update at.
227 * This is designated in Hertz, with a minimum of 20 Hz
228 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
229 *
230 * If this field is set to 0 Hz, the control request will
231 * be sent immediately as a one-shot frame. This may be useful
232 * for advanced applications that require outputs to be
233 * synchronized with data acquisition. In this case, we
234 * recommend not exceeding 50 ms between control calls.
235 *
236 * \param newUpdateFreqHz Parameter to modify
237 * \returns Itself
238 */
239 MotionMagicExpoTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
240 {
241 UpdateFreqHz = newUpdateFreqHz;
242 return *this;
243 }
244 /**
245 * Returns a string representation of the object.
246 *
247 * \returns a string representation of the object.
248 */
249 std::string ToString() const override
250 {
251 std::stringstream ss;
252 ss << "class: MotionMagicExpoTorqueCurrentFOC" << std::endl;
253 ss << "Position: " << Position.to<double>() << std::endl;
254 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
255 ss << "Slot: " << Slot << std::endl;
256 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
257 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
258 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
259 return ss.str();
260 }
261
262 /**
263 * \brief Gets information about this control request.
264 *
265 * \returns Map of control parameter names and corresponding applied values
266 */
267 std::map<std::string, std::string> GetControlInfo() const override
268 {
269 std::map<std::string, std::string> controlInfo;
270 std::stringstream ss;
271 controlInfo["Name"] = GetName();
272 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
273 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
274 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
275 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
276 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
277 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
278 return controlInfo;
279 }
280};
281
282}
283}
284}
285
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:184
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:267
MotionMagicExpoTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:239
std::string ToString() const override
Returns a string representation of the object.
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:249
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:160
MotionMagicExpoTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:196
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:172
MotionMagicExpoTorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicExpoTorqueCurrentFOC.hpp:220
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:208
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: RcManualEvent.hpp:12