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