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