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