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