Loading [MathJax]/extensions/tex2jax.js
CTRE Phoenix 6 C++ 23.10.0-alpha-8
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DifferentialMotionMagicDutyCycle.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
12#include <sstream>
13#include <units/angle.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, and
24 * PID to a differential position setpoint.
25 *
26 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
27 * Acceleration, and Jerk value specified via the Motion Magic® configuration values. Target position can be
28 * changed on-the-fly and Motion Magic® will do its best to adjust the profile. This control mode is duty
29 * cycle based, so relevant closed-loop gains will use fractional duty cycle for the numerator: +1.0
30 * represents full forward output.
31 */
33{
34 bool ApplyConfigsOnRequest{false};
35
36 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
37 {
38 if (req.get() != this)
39 {
40 auto const reqCast = dynamic_cast<DifferentialMotionMagicDutyCycle *>(req.get());
41 if (reqCast != nullptr)
42 {
43 *reqCast = *this;
44 }
45 else
46 {
47 req = std::make_shared<DifferentialMotionMagicDutyCycle>(*this);
48 }
49 }
50
51 std::stringstream ss;
52
53 std::string strs{ss.str()};
54 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
55 ApplyConfigsOnRequest = false;
57 }
58
59public:
60 /**
61 * Average position to drive toward in rotations.
62 */
63 units::angle::turn_t TargetPosition;
64 /**
65 * Differential position to drive toward in rotations.
66 */
67 units::angle::turn_t DifferentialPosition;
68 /**
69 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
70 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
71 * improves motor performance by leveraging torque (current) control. However,
72 * this may be inconvenient for applications that require specifying duty cycle
73 * or voltage. CTR-Electronics has developed a hybrid method that combines the
74 * performances gains of FOC while still allowing applications to provide duty
75 * cycle or voltage demand. This not to be confused with simple sinusoidal
76 * control or phase voltage control which lacks the performance gains.
77 */
79 /**
80 * Select which gains are applied to the primary controller by selecting the
81 * slot. Use the configuration api to set the gain values for the selected slot
82 * before enabling this feature. Slot must be within [0,2].
83 */
85 /**
86 * Select which gains are applied to the differential controller by selecting
87 * the slot. Use the configuration api to set the gain values for the selected
88 * slot before enabling this feature. Slot must be within [0,2].
89 */
91 /**
92 * 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
100
101 /**
102 * \brief The timeout when sending configs associated with this control
103 */
104 units::time::second_t ConfigTimeout{0.1_s};
105
106 /**
107 * \brief The period at which this control will update at.
108 * This is designated in Hertz, with a minimum of 20 Hz
109 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
110 *
111 * If this field is set to 0 Hz, the control request will
112 * be sent immediately as a one-shot frame. This may be useful
113 * for advanced applications that require outputs to be
114 * synchronized with data acquisition. In this case, we
115 * recommend not exceeding 50 ms between control calls.
116 */
117 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
118
119 /**
120 * \brief Requests Motion Magic® to target a final position using a motion
121 * profile, and PID to a differential position setpoint.
122 *
123 * \details Motion Magic® produces a motion profile in real-time while
124 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
125 * value specified via the Motion Magic® configuration values. Target
126 * position can be changed on-the-fly and Motion Magic® will do its
127 * best to adjust the profile. This control mode is duty cycle based,
128 * so relevant closed-loop gains will use fractional duty cycle for the
129 * numerator: +1.0 represents full forward output.
130 *
131 * \param TargetPosition Average position to drive toward in rotations.
132 * \param DifferentialPosition Differential position to drive toward in
133 * rotations.
134 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
135 * Pro), which increases peak power by ~15%. Set to false to
136 * use trapezoidal commutation. FOC improves motor
137 * performance by leveraging torque (current) control.
138 * However, this may be inconvenient for applications that
139 * require specifying duty cycle or voltage.
140 * CTR-Electronics has developed a hybrid method that
141 * combines the performances gains of FOC while still
142 * allowing applications to provide duty cycle or voltage
143 * demand. This not to be confused with simple sinusoidal
144 * control or phase voltage control which lacks the
145 * performance gains.
146 * \param TargetSlot Select which gains are applied to the primary controller
147 * by selecting the slot. Use the configuration api to set
148 * the gain values for the selected slot before enabling
149 * this feature. Slot must be within [0,2].
150 * \param DifferentialSlot Select which gains are applied to the differential
151 * controller by selecting the slot. Use the
152 * configuration api to set the gain values for the
153 * selected slot before enabling this feature. Slot
154 * must be within [0,2].
155 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
156 * output is zero (or within deadband). Set
157 * to false to use the NeutralMode
158 * configuration setting (default). This flag
159 * exists to provide the fundamental behavior
160 * of this control when output is zero, which
161 * is to provide 0V to the motor.
162 */
163 DifferentialMotionMagicDutyCycle(units::angle::turn_t TargetPosition, units::angle::turn_t DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, bool OverrideBrakeDurNeutral) : ControlRequest{"DifferentialMotionMagicDutyCycle"},
164 TargetPosition{std::move(TargetPosition)},
166 EnableFOC{std::move(EnableFOC)},
167 TargetSlot{std::move(TargetSlot)},
170 {}
171
172 /**
173 * \brief Requests Motion Magic® to target a final position using a motion
174 * profile, and PID to a differential position setpoint.
175 *
176 * \details Motion Magic® produces a motion profile in real-time while
177 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
178 * value specified via the Motion Magic® configuration values. Target
179 * position can be changed on-the-fly and Motion Magic® will do its
180 * best to adjust the profile. This control mode is duty cycle based,
181 * so relevant closed-loop gains will use fractional duty cycle for the
182 * numerator: +1.0 represents full forward output.
183 *
184 * \param TargetPosition Average position to drive toward in rotations.
185 * \param DifferentialPosition Differential position to drive toward in
186 * rotations.
187 */
189 {}
190
191 /**
192 * \brief Modifies this Control Request's TargetPosition parameter and returns itself for
193 * method-chaining and easier to use request API.
194 * \param newTargetPosition Parameter to modify
195 * \returns Itself
196 */
197 DifferentialMotionMagicDutyCycle& WithTargetPosition(units::angle::turn_t newTargetPosition)
198 {
199 TargetPosition = std::move(newTargetPosition);
200 return *this;
201 }
202
203 /**
204 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
205 * method-chaining and easier to use request API.
206 * \param newDifferentialPosition Parameter to modify
207 * \returns Itself
208 */
209 DifferentialMotionMagicDutyCycle& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
210 {
211 DifferentialPosition = std::move(newDifferentialPosition);
212 return *this;
213 }
214
215 /**
216 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
217 * method-chaining and easier to use request API.
218 * \param newEnableFOC Parameter to modify
219 * \returns Itself
220 */
222 {
223 EnableFOC = std::move(newEnableFOC);
224 return *this;
225 }
226
227 /**
228 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
229 * method-chaining and easier to use request API.
230 * \param newTargetSlot Parameter to modify
231 * \returns Itself
232 */
234 {
235 TargetSlot = std::move(newTargetSlot);
236 return *this;
237 }
238
239 /**
240 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
241 * method-chaining and easier to use request API.
242 * \param newDifferentialSlot Parameter to modify
243 * \returns Itself
244 */
246 {
247 DifferentialSlot = std::move(newDifferentialSlot);
248 return *this;
249 }
250
251 /**
252 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
253 * method-chaining and easier to use request API.
254 * \param newOverrideBrakeDurNeutral Parameter to modify
255 * \returns Itself
256 */
258 {
259 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
260 return *this;
261 }
262 /**
263 * \brief Sets the period at which this control will update at.
264 * This is designated in Hertz, with a minimum of 20 Hz
265 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
266 *
267 * If this field is set to 0 Hz, the control request will
268 * be sent immediately as a one-shot frame. This may be useful
269 * for advanced applications that require outputs to be
270 * synchronized with data acquisition. In this case, we
271 * recommend not exceeding 50 ms between control calls.
272 *
273 * \param newUpdateFreqHz Parameter to modify
274 * \returns Itself
275 */
276 DifferentialMotionMagicDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
277 {
278 UpdateFreqHz = newUpdateFreqHz;
279 return *this;
280 }
281 /**
282 * Returns a string representation of the object.
283 *
284 * \returns a string representation of the object.
285 */
286 std::string ToString() const override
287 {
288 std::stringstream ss;
289 ss << "class: DifferentialMotionMagicDutyCycle" << std::endl;
290 ss << "TargetPosition: " << TargetPosition.to<double>() << std::endl;
291 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
292 ss << "EnableFOC: " << EnableFOC << std::endl;
293 ss << "TargetSlot: " << TargetSlot << std::endl;
294 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
295 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
296 return ss.str();
297 }
298
299 /**
300 * \brief Forces configs to be applied the next time this is used in a setControl.
301 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
302 * properly set when they are not already set
303 */
304 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
305
306 /**
307 * \brief Gets information about this control request.
308 *
309 * \returns Map of control parameter names and corresponding applied values
310 */
311 std::map<std::string, std::string> GetControlInfo() const override
312 {
313 std::map<std::string, std::string> controlInfo;
314 std::stringstream ss;
315 controlInfo["Name"] = GetName();
316 ss << TargetPosition.to<double>(); controlInfo["TargetPosition"] = ss.str(); ss.str(std::string{});
317 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
318 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
319 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
320 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
321 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
322 return controlInfo;
323 }
324};
325
326}
327}
328}
329
CTREXPORT int c_ctre_phoenix6_requestConfigApply(const char *canbus, uint32_t ecuEncoding, double timeoutSeconds, const char *str, uint32_t strlen, bool forceApply)
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialMotionMagicDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double TargetPosition, double DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, bool OverrideBrakeDurNeutral)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requests Motion Magic® to target a final position using a motion profile, and PID to a differential p...
Definition: DifferentialMotionMagicDutyCycle.hpp:33
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialMotionMagicDutyCycle.hpp:84
DifferentialMotionMagicDutyCycle & WithTargetPosition(units::angle::turn_t newTargetPosition)
Modifies this Control Request's TargetPosition parameter and returns itself for method-chaining and e...
Definition: DifferentialMotionMagicDutyCycle.hpp:197
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialMotionMagicDutyCycle.hpp:97
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialMotionMagicDutyCycle.hpp:90
DifferentialMotionMagicDutyCycle & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialMotionMagicDutyCycle.hpp:233
DifferentialMotionMagicDutyCycle & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialMotionMagicDutyCycle.hpp:245
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: DifferentialMotionMagicDutyCycle.hpp:304
DifferentialMotionMagicDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialMotionMagicDutyCycle.hpp:257
units::angle::turn_t TargetPosition
Average position to drive toward in rotations.
Definition: DifferentialMotionMagicDutyCycle.hpp:63
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialMotionMagicDutyCycle.hpp:78
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: DifferentialMotionMagicDutyCycle.hpp:104
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialMotionMagicDutyCycle.hpp:286
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialMotionMagicDutyCycle.hpp:311
DifferentialMotionMagicDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialMotionMagicDutyCycle.hpp:276
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialMotionMagicDutyCycle.hpp:117
DifferentialMotionMagicDutyCycle & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialMotionMagicDutyCycle.hpp:209
units::angle::turn_t DifferentialPosition
Differential position to drive toward in rotations.
Definition: DifferentialMotionMagicDutyCycle.hpp:67
DifferentialMotionMagicDutyCycle(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: DifferentialMotionMagicDutyCycle.hpp:188
DifferentialMotionMagicDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialMotionMagicDutyCycle.hpp:221
DifferentialMotionMagicDutyCycle(units::angle::turn_t TargetPosition, units::angle::turn_t DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, bool OverrideBrakeDurNeutral)
Requests Motion Magic® to target a final position using a motion profile, and PID to a differential p...
Definition: DifferentialMotionMagicDutyCycle.hpp:163
Definition: ManualEvent.hpp:12