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