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