CTRE Phoenix 6 C++ 25.2.1
Loading...
Searching...
No Matches
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
11#include <sstream>
12#include <units/angle.h>
13#include <units/voltage.h>
14#include <units/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Requests Motion Magic® to target a final position using an exponential motion
24 * profile. Users can optionally provide a voltage feedforward.
25 *
26 * Motion Magic® Expo produces a motion profile in real-time while attempting to honor the Cruise Velocity
27 * (optional) and the mechanism kV and kA, specified via the Motion Magic® configuration values. Note that
28 * unlike the slot gains, the Expo_kV and Expo_kA configs are always in output units of Volts.
29 *
30 * Setting Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV.
31 * This control mode does not use the Acceleration or Jerk configs.
32 *
33 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile. This
34 * control mode is voltage-based, so relevant closed-loop gains will use Volts for the numerator.
35 */
37{
38 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
39 {
40 if (req.get() != this)
41 {
42 auto const reqCast = dynamic_cast<MotionMagicExpoVoltage *>(req.get());
43 if (reqCast != nullptr)
44 {
45 *reqCast = *this;
46 }
47 else
48 {
49 req = std::make_shared<MotionMagicExpoVoltage>(*this);
50 }
51 }
52
54 }
55
56public:
57 /**
58 * \brief Position to drive toward in rotations.
59 */
60 units::angle::turn_t Position;
61 /**
62 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
63 * increases peak power by ~15%. Set to false to use trapezoidal commutation.
64 *
65 * FOC improves motor performance by leveraging torque (current) control.
66 * However, this may be inconvenient for applications that require specifying
67 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
68 * combines the performances gains of FOC while still allowing applications to
69 * provide duty cycle or voltage demand. This not to be confused with simple
70 * sinusoidal control or phase voltage control which lacks the performance
71 * gains.
72 */
73 bool EnableFOC = true;
74 /**
75 * \brief Feedforward to apply in volts
76 */
77 units::voltage::volt_t FeedForward = 0.0_V;
78 /**
79 * \brief Select which gains are applied by selecting the slot. Use the
80 * configuration api to set the gain values for the selected slot before
81 * enabling this feature. Slot must be within [0,2].
82 */
83 int Slot = 0;
84 /**
85 * \brief Set to true to static-brake the rotor when output is zero (or within
86 * deadband). Set to false to use the NeutralMode configuration setting
87 * (default). This flag exists to provide the fundamental behavior of this
88 * control when output is zero, which is to provide 0V to the motor.
89 */
91 /**
92 * \brief Set to true to force forward limiting. This allows users to use other
93 * limit switch sensors connected to robot controller. This also allows use of
94 * active sensors that require external power.
95 */
96 bool LimitForwardMotion = false;
97 /**
98 * \brief Set to true to force reverse limiting. This allows users to use other
99 * limit switch sensors connected to robot controller. This also allows use of
100 * active sensors that require external power.
101 */
102 bool LimitReverseMotion = false;
103 /**
104 * \brief Set to true to ignore hardware limit switches and the
105 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
106 * motion.
107 *
108 * This can be useful on mechanisms such as an intake/feeder, where a limit
109 * switch stops motion while intaking but should be ignored when feeding to a
110 * shooter.
111 *
112 * The hardware limit faults and Forward/ReverseLimit signals will still report
113 * the values of the limit switches regardless of this parameter.
114 */
116 /**
117 * \brief Set to true to delay applying this control request until a timesync
118 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
119 * nondeterministic network delays in exchange for a larger but deterministic
120 * control latency.
121 *
122 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
123 * Additionally, when this is enabled, the UpdateFreqHz of this request should
124 * be set to 0 Hz.
125 */
126 bool UseTimesync = false;
127
128 /**
129 * \brief The period at which this control will update at.
130 * This is designated in Hertz, with a minimum of 20 Hz
131 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
132 *
133 * If this field is set to 0 Hz, the control request will
134 * be sent immediately as a one-shot frame. This may be useful
135 * for advanced applications that require outputs to be
136 * synchronized with data acquisition. In this case, we
137 * recommend not exceeding 50 ms between control calls.
138 */
139 units::frequency::hertz_t UpdateFreqHz{100_Hz};
140
141 /**
142 * \brief Requests Motion Magic® to target a final position using an exponential
143 * motion profile. Users can optionally provide a voltage feedforward.
144 *
145 * \details Motion Magic® Expo produces a motion profile in real-time while
146 * attempting to honor the Cruise Velocity (optional) and the mechanism
147 * kV and kA, specified via the Motion Magic® configuration values.
148 * Note that unlike the slot gains, the Expo_kV and Expo_kA configs are
149 * always in output units of Volts.
150 *
151 * Setting Cruise Velocity to 0 will allow the profile to run to the
152 * max possible velocity based on Expo_kV. This control mode does not
153 * use the Acceleration or Jerk configs.
154 *
155 * Target position can be changed on-the-fly and Motion Magic® will do
156 * its best to adjust the profile. This control mode is voltage-based,
157 * so relevant closed-loop gains will use Volts for the numerator.
158 *
159 * \param Position Position to drive toward in rotations.
160 */
161 MotionMagicExpoVoltage(units::angle::turn_t Position) : ControlRequest{"MotionMagicExpoVoltage"},
162 Position{std::move(Position)}
163 {}
164
165 /**
166 * \brief Modifies this Control Request's Position parameter and returns itself for
167 * method-chaining and easier to use request API.
168 *
169 * Position to drive toward in rotations.
170 *
171 * \param newPosition Parameter to modify
172 * \returns Itself
173 */
174 MotionMagicExpoVoltage &WithPosition(units::angle::turn_t newPosition)
175 {
176 Position = std::move(newPosition);
177 return *this;
178 }
179
180 /**
181 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
182 * method-chaining and easier to use request API.
183 *
184 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
185 * peak power by ~15%. Set to false to use trapezoidal commutation.
186 *
187 * FOC improves motor performance by leveraging torque (current) control.
188 * However, this may be inconvenient for applications that require specifying
189 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
190 * combines the performances gains of FOC while still allowing applications to
191 * provide duty cycle or voltage demand. This not to be confused with simple
192 * sinusoidal control or phase voltage control which lacks the performance
193 * gains.
194 *
195 * \param newEnableFOC Parameter to modify
196 * \returns Itself
197 */
199 {
200 EnableFOC = std::move(newEnableFOC);
201 return *this;
202 }
203
204 /**
205 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
206 * method-chaining and easier to use request API.
207 *
208 * Feedforward to apply in volts
209 *
210 * \param newFeedForward Parameter to modify
211 * \returns Itself
212 */
213 MotionMagicExpoVoltage &WithFeedForward(units::voltage::volt_t newFeedForward)
214 {
215 FeedForward = std::move(newFeedForward);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's Slot parameter and returns itself for
221 * method-chaining and easier to use request API.
222 *
223 * Select which gains are applied by selecting the slot. Use the configuration
224 * api to set the gain values for the selected slot before enabling this
225 * feature. Slot must be within [0,2].
226 *
227 * \param newSlot Parameter to modify
228 * \returns Itself
229 */
231 {
232 Slot = std::move(newSlot);
233 return *this;
234 }
235
236 /**
237 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
238 * method-chaining and easier to use request API.
239 *
240 * Set to true to static-brake the rotor when output is zero (or within
241 * deadband). Set to false to use the NeutralMode configuration setting
242 * (default). This flag exists to provide the fundamental behavior of this
243 * control when output is zero, which is to provide 0V to the motor.
244 *
245 * \param newOverrideBrakeDurNeutral Parameter to modify
246 * \returns Itself
247 */
248 MotionMagicExpoVoltage &WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
249 {
250 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
251 return *this;
252 }
253
254 /**
255 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
256 * method-chaining and easier to use request API.
257 *
258 * Set to true to force forward limiting. This allows users to use other limit
259 * switch sensors connected to robot controller. This also allows use of active
260 * sensors that require external power.
261 *
262 * \param newLimitForwardMotion Parameter to modify
263 * \returns Itself
264 */
266 {
267 LimitForwardMotion = std::move(newLimitForwardMotion);
268 return *this;
269 }
270
271 /**
272 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
273 * method-chaining and easier to use request API.
274 *
275 * Set to true to force reverse limiting. This allows users to use other limit
276 * switch sensors connected to robot controller. This also allows use of active
277 * sensors that require external power.
278 *
279 * \param newLimitReverseMotion Parameter to modify
280 * \returns Itself
281 */
283 {
284 LimitReverseMotion = std::move(newLimitReverseMotion);
285 return *this;
286 }
287
288 /**
289 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
290 * method-chaining and easier to use request API.
291 *
292 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
293 * LimitReverseMotion parameters, instead allowing motion.
294 *
295 * This can be useful on mechanisms such as an intake/feeder, where a limit
296 * switch stops motion while intaking but should be ignored when feeding to a
297 * shooter.
298 *
299 * The hardware limit faults and Forward/ReverseLimit signals will still report
300 * the values of the limit switches regardless of this parameter.
301 *
302 * \param newIgnoreHardwareLimits Parameter to modify
303 * \returns Itself
304 */
306 {
307 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
308 return *this;
309 }
310
311 /**
312 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
313 * method-chaining and easier to use request API.
314 *
315 * Set to true to delay applying this control request until a timesync boundary
316 * (requires Phoenix Pro and CANivore). This eliminates the impact of
317 * nondeterministic network delays in exchange for a larger but deterministic
318 * control latency.
319 *
320 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
321 * Additionally, when this is enabled, the UpdateFreqHz of this request should
322 * be set to 0 Hz.
323 *
324 * \param newUseTimesync Parameter to modify
325 * \returns Itself
326 */
328 {
329 UseTimesync = std::move(newUseTimesync);
330 return *this;
331 }
332 /**
333 * \brief Sets the period at which this control will update at.
334 * This is designated in Hertz, with a minimum of 20 Hz
335 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
336 *
337 * If this field is set to 0 Hz, the control request will
338 * be sent immediately as a one-shot frame. This may be useful
339 * for advanced applications that require outputs to be
340 * synchronized with data acquisition. In this case, we
341 * recommend not exceeding 50 ms between control calls.
342 *
343 * \param newUpdateFreqHz Parameter to modify
344 * \returns Itself
345 */
346 MotionMagicExpoVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
347 {
348 UpdateFreqHz = newUpdateFreqHz;
349 return *this;
350 }
351 /**
352 * \brief Returns a string representation of the object.
353 *
354 * \returns a string representation of the object.
355 */
356 std::string ToString() const override
357 {
358 std::stringstream ss;
359 ss << "Control: MotionMagicExpoVoltage" << std::endl;
360 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
361 ss << " EnableFOC: " << EnableFOC << std::endl;
362 ss << " FeedForward: " << FeedForward.to<double>() << " Volts" << std::endl;
363 ss << " Slot: " << Slot << std::endl;
364 ss << " OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
365 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
366 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
367 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
368 ss << " UseTimesync: " << UseTimesync << std::endl;
369 return ss.str();
370 }
371
372 /**
373 * \brief Gets information about this control request.
374 *
375 * \returns Map of control parameter names and corresponding applied values
376 */
377 std::map<std::string, std::string> GetControlInfo() const override
378 {
379 std::map<std::string, std::string> controlInfo;
380 std::stringstream ss;
381 controlInfo["Name"] = GetName();
382 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
383 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
384 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
385 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
386 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
387 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
388 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
389 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
390 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
391 return controlInfo;
392 }
393};
394
395}
396}
397}
398
CTREXPORT int c_ctre_phoenix6_RequestControlMotionMagicExpoVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion, bool IgnoreHardwareLimits, bool UseTimesync)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition MotionMagicExpoVoltage.hpp:37
std::string ToString() const override
Returns a string representation of the object.
Definition MotionMagicExpoVoltage.hpp:356
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition MotionMagicExpoVoltage.hpp:139
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:174
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition MotionMagicExpoVoltage.hpp:73
MotionMagicExpoVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoVoltage.hpp:265
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition MotionMagicExpoVoltage.hpp:115
MotionMagicExpoVoltage(units::angle::turn_t Position)
Requests Motion Magic® to target a final position using an exponential motion profile.
Definition MotionMagicExpoVoltage.hpp:161
int Slot
Select which gains are applied by selecting the slot.
Definition MotionMagicExpoVoltage.hpp:83
MotionMagicExpoVoltage & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition MotionMagicExpoVoltage.hpp:305
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition MotionMagicExpoVoltage.hpp:102
MotionMagicExpoVoltage & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition MotionMagicExpoVoltage.hpp:327
MotionMagicExpoVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition MotionMagicExpoVoltage.hpp:230
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition MotionMagicExpoVoltage.hpp:90
MotionMagicExpoVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition MotionMagicExpoVoltage.hpp:282
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition MotionMagicExpoVoltage.hpp:377
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition MotionMagicExpoVoltage.hpp:126
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:213
units::angle::turn_t Position
Position to drive toward in rotations.
Definition MotionMagicExpoVoltage.hpp:60
MotionMagicExpoVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition MotionMagicExpoVoltage.hpp:248
MotionMagicExpoVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition MotionMagicExpoVoltage.hpp:346
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition MotionMagicExpoVoltage.hpp:77
bool LimitForwardMotion
Set to true to force forward limiting.
Definition MotionMagicExpoVoltage.hpp:96
MotionMagicExpoVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition MotionMagicExpoVoltage.hpp:198
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401