CTRE Phoenix 6 C++ 24.2.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
106 * position using a motion profile. Users can optionally provide a
107 * torque current feedforward.
108 *
109 * \details Motion Magic® produces a motion profile in real-time while
110 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
111 * value specified via the Motion Magic® configuration values. This
112 * control mode does not use the Expo_kV or Expo_kA configs. Target
113 * position can be changed on-the-fly and Motion Magic® will do its
114 * best to adjust the profile. This control mode is based on torque
115 * current, so relevant closed-loop gains will use Amperes for the
116 * numerator.
117 *
118 * \param Position Position to drive toward in rotations.
119 * \param FeedForward Feedforward to apply in torque current in Amperes.
120 * User can use motor's kT to scale Newton-meter to
121 * Amperes.
122 * \param Slot Select which gains are applied by selecting the slot. Use the
123 * configuration api to set the gain values for the selected slot
124 * before enabling this feature. Slot must be within [0,2].
125 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
126 * is zero (or within deadband). Set to false
127 * to use the NeutralMode configuration
128 * setting (default). This flag exists to
129 * provide the fundamental behavior of this
130 * control when output is zero, which is to
131 * provide 0A (zero torque).
132 * \param LimitForwardMotion Set to true to force forward limiting. This
133 * allows users to use other limit switch sensors
134 * connected to robot controller. This also allows
135 * use of active sensors that require external
136 * power.
137 * \param LimitReverseMotion Set to true to force reverse 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 */
143 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"},
144 Position{std::move(Position)},
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 MotionMagicTorqueCurrentFOC& 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 FeedForward parameter and returns itself for
166 * method-chaining and easier to use request API.
167 * \param newFeedForward Parameter to modify
168 * \returns Itself
169 */
170 MotionMagicTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
171 {
172 FeedForward = std::move(newFeedForward);
173 return *this;
174 }
175
176 /**
177 * \brief Modifies this Control Request's Slot parameter and returns itself for
178 * method-chaining and easier to use request API.
179 * \param newSlot Parameter to modify
180 * \returns Itself
181 */
183 {
184 Slot = std::move(newSlot);
185 return *this;
186 }
187
188 /**
189 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
190 * method-chaining and easier to use request API.
191 * \param newOverrideCoastDurNeutral Parameter to modify
192 * \returns Itself
193 */
195 {
196 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
197 return *this;
198 }
199
200 /**
201 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
202 * method-chaining and easier to use request API.
203 * \param newLimitForwardMotion Parameter to modify
204 * \returns Itself
205 */
207 {
208 LimitForwardMotion = std::move(newLimitForwardMotion);
209 return *this;
210 }
211
212 /**
213 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
214 * method-chaining and easier to use request API.
215 * \param newLimitReverseMotion Parameter to modify
216 * \returns Itself
217 */
219 {
220 LimitReverseMotion = std::move(newLimitReverseMotion);
221 return *this;
222 }
223 /**
224 * \brief Sets the period at which this control will update at.
225 * This is designated in Hertz, with a minimum of 20 Hz
226 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
227 *
228 * If this field is set to 0 Hz, the control request will
229 * be sent immediately as a one-shot frame. This may be useful
230 * for advanced applications that require outputs to be
231 * synchronized with data acquisition. In this case, we
232 * recommend not exceeding 50 ms between control calls.
233 *
234 * \param newUpdateFreqHz Parameter to modify
235 * \returns Itself
236 */
237 MotionMagicTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
238 {
239 UpdateFreqHz = newUpdateFreqHz;
240 return *this;
241 }
242 /**
243 * Returns a string representation of the object.
244 *
245 * \returns a string representation of the object.
246 */
247 std::string ToString() const override
248 {
249 std::stringstream ss;
250 ss << "class: MotionMagicTorqueCurrentFOC" << std::endl;
251 ss << "Position: " << Position.to<double>() << std::endl;
252 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
253 ss << "Slot: " << Slot << std::endl;
254 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
255 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
256 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
257 return ss.str();
258 }
259
260 /**
261 * \brief Gets information about this control request.
262 *
263 * \returns Map of control parameter names and corresponding applied values
264 */
265 std::map<std::string, std::string> GetControlInfo() const override
266 {
267 std::map<std::string, std::string> controlInfo;
268 std::stringstream ss;
269 controlInfo["Name"] = GetName();
270 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
271 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
272 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
273 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
274 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
275 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
276 return controlInfo;
277 }
278};
279
280}
281}
282}
283
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:218
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:143
MotionMagicTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicTorqueCurrentFOC.hpp:237
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:182
MotionMagicTorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: MotionMagicTorqueCurrentFOC.hpp:206
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:170
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:247
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:158
MotionMagicTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicTorqueCurrentFOC.hpp:194
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MotionMagicTorqueCurrentFOC.hpp:265
Definition: RcManualEvent.hpp:12