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