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