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