CTRE Phoenix 6 C++ 25.3.0
Loading...
Searching...
No Matches
TorqueCurrentFOC.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/current.h>
13#include <units/dimensionless.h>
14#include <units/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Requires Phoenix Pro;
24 * Request a specified motor current (field oriented control).
25 *
26 * This control request will drive the motor to the requested motor (stator) current value. This leverages
27 * field oriented control (FOC), which means greater peak power than what is documented. This scales to
28 * torque based on Motor's kT constant.
29 */
31{
32 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
33 {
34 if (req.get() != this)
35 {
36 auto const reqCast = dynamic_cast<TorqueCurrentFOC *>(req.get());
37 if (reqCast != nullptr)
38 {
39 *reqCast = *this;
40 }
41 else
42 {
43 req = std::make_shared<TorqueCurrentFOC>(*this);
44 }
45 }
46
48 }
49
50public:
51 /**
52 * \brief Amount of motor current in Amperes
53 *
54 * - Units: A
55 *
56 */
57 units::current::ampere_t Output;
58 /**
59 * \brief The maximum absolute motor output that can be applied, which
60 * effectively limits the velocity. For example, 0.50 means no more than 50%
61 * output in either direction. This is useful for preventing the motor from
62 * spinning to its terminal velocity when there is no external torque applied
63 * unto the rotor. Note this is absolute maximum, so the value should be
64 * between zero and one.
65 *
66 * - Units: fractional
67 *
68 */
69 units::dimensionless::scalar_t MaxAbsDutyCycle = 1.0;
70 /**
71 * \brief Deadband in Amperes. If torque request is within deadband, the bridge
72 * output is neutral. If deadband is set to zero then there is effectively no
73 * deadband. Note if deadband is zero, a free spinning motor will spin for quite
74 * a while as the firmware attempts to hold the motor's bemf. If user expects
75 * motor to cease spinning quickly with a demand of zero, we recommend a
76 * deadband of one Ampere. This value will be converted to an integral value of
77 * amps.
78 *
79 * - Units: A
80 *
81 */
82 units::current::ampere_t Deadband = 0.0_A;
83 /**
84 * \brief Set to true to coast the rotor when output is zero (or within
85 * deadband). Set to false to use the NeutralMode configuration setting
86 * (default). This flag exists to provide the fundamental behavior of this
87 * control when output is zero, which is to provide 0A (zero torque).
88 */
90 /**
91 * \brief Set to true to force forward limiting. This allows users to use other
92 * limit switch sensors connected to robot controller. This also allows use of
93 * active sensors that require external power.
94 */
95 bool LimitForwardMotion = false;
96 /**
97 * \brief Set to true to force reverse limiting. This allows users to use other
98 * limit switch sensors connected to robot controller. This also allows use of
99 * active sensors that require external power.
100 */
101 bool LimitReverseMotion = false;
102 /**
103 * \brief Set to true to ignore hardware limit switches and the
104 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
105 * motion.
106 *
107 * This can be useful on mechanisms such as an intake/feeder, where a limit
108 * switch stops motion while intaking but should be ignored when feeding to a
109 * shooter.
110 *
111 * The hardware limit faults and Forward/ReverseLimit signals will still report
112 * the values of the limit switches regardless of this parameter.
113 */
115 /**
116 * \brief Set to true to delay applying this control request until a timesync
117 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
118 * nondeterministic network delays in exchange for a larger but deterministic
119 * control latency.
120 *
121 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
122 * Additionally, when this is enabled, the UpdateFreqHz of this request should
123 * be set to 0 Hz.
124 */
125 bool UseTimesync = false;
126
127 /**
128 * \brief The period at which this control will update at.
129 * This is designated in Hertz, with a minimum of 20 Hz
130 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
131 *
132 * If this field is set to 0 Hz, the control request will
133 * be sent immediately as a one-shot frame. This may be useful
134 * for advanced applications that require outputs to be
135 * synchronized with data acquisition. In this case, we
136 * recommend not exceeding 50 ms between control calls.
137 */
138 units::frequency::hertz_t UpdateFreqHz{100_Hz};
139
140 /**
141 * \brief Requires Phoenix Pro;
142 * Request a specified motor current (field oriented control).
143 *
144 * \details This control request will drive the motor to the requested motor
145 * (stator) current value. This leverages field oriented control
146 * (FOC), which means greater peak power than what is documented. This
147 * scales to torque based on Motor's kT constant.
148 *
149 * \param Output Amount of motor current in Amperes
150 */
151 TorqueCurrentFOC(units::current::ampere_t Output) : ControlRequest{"TorqueCurrentFOC"},
152 Output{std::move(Output)}
153 {}
154
155 /**
156 * \brief Modifies this Control Request's Output parameter and returns itself for
157 * method-chaining and easier to use request API.
158 *
159 * Amount of motor current in Amperes
160 *
161 * - Units: A
162 *
163 *
164 * \param newOutput Parameter to modify
165 * \returns Itself
166 */
167 TorqueCurrentFOC &WithOutput(units::current::ampere_t newOutput)
168 {
169 Output = std::move(newOutput);
170 return *this;
171 }
172
173 /**
174 * \brief Modifies this Control Request's MaxAbsDutyCycle parameter and returns itself for
175 * method-chaining and easier to use request API.
176 *
177 * The maximum absolute motor output that can be applied, which effectively
178 * limits the velocity. For example, 0.50 means no more than 50% output in
179 * either direction. This is useful for preventing the motor from spinning to
180 * its terminal velocity when there is no external torque applied unto the
181 * rotor. Note this is absolute maximum, so the value should be between zero
182 * and one.
183 *
184 * - Units: fractional
185 *
186 *
187 * \param newMaxAbsDutyCycle Parameter to modify
188 * \returns Itself
189 */
190 TorqueCurrentFOC &WithMaxAbsDutyCycle(units::dimensionless::scalar_t newMaxAbsDutyCycle)
191 {
192 MaxAbsDutyCycle = std::move(newMaxAbsDutyCycle);
193 return *this;
194 }
195
196 /**
197 * \brief Modifies this Control Request's Deadband parameter and returns itself for
198 * method-chaining and easier to use request API.
199 *
200 * Deadband in Amperes. If torque request is within deadband, the bridge output
201 * is neutral. If deadband is set to zero then there is effectively no deadband.
202 * Note if deadband is zero, a free spinning motor will spin for quite a while
203 * as the firmware attempts to hold the motor's bemf. If user expects motor to
204 * cease spinning quickly with a demand of zero, we recommend a deadband of one
205 * Ampere. This value will be converted to an integral value of amps.
206 *
207 * - Units: A
208 *
209 *
210 * \param newDeadband Parameter to modify
211 * \returns Itself
212 */
213 TorqueCurrentFOC &WithDeadband(units::current::ampere_t newDeadband)
214 {
215 Deadband = std::move(newDeadband);
216 return *this;
217 }
218
219 /**
220 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
221 * method-chaining and easier to use request API.
222 *
223 * Set to true to coast the rotor when output is zero (or within deadband). Set
224 * to false to use the NeutralMode configuration setting (default). This flag
225 * exists to provide the fundamental behavior of this control when output is
226 * zero, which is to provide 0A (zero torque).
227 *
228 * \param newOverrideCoastDurNeutral Parameter to modify
229 * \returns Itself
230 */
231 TorqueCurrentFOC &WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
232 {
233 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
234 return *this;
235 }
236
237 /**
238 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
239 * method-chaining and easier to use request API.
240 *
241 * Set to true to force forward limiting. This allows users to use other limit
242 * switch sensors connected to robot controller. This also allows use of active
243 * sensors that require external power.
244 *
245 * \param newLimitForwardMotion Parameter to modify
246 * \returns Itself
247 */
248 TorqueCurrentFOC &WithLimitForwardMotion(bool newLimitForwardMotion)
249 {
250 LimitForwardMotion = std::move(newLimitForwardMotion);
251 return *this;
252 }
253
254 /**
255 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
256 * method-chaining and easier to use request API.
257 *
258 * Set to true to force reverse limiting. This allows users to use other limit
259 * switch sensors connected to robot controller. This also allows use of active
260 * sensors that require external power.
261 *
262 * \param newLimitReverseMotion Parameter to modify
263 * \returns Itself
264 */
265 TorqueCurrentFOC &WithLimitReverseMotion(bool newLimitReverseMotion)
266 {
267 LimitReverseMotion = std::move(newLimitReverseMotion);
268 return *this;
269 }
270
271 /**
272 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
273 * method-chaining and easier to use request API.
274 *
275 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
276 * LimitReverseMotion parameters, instead allowing motion.
277 *
278 * This can be useful on mechanisms such as an intake/feeder, where a limit
279 * switch stops motion while intaking but should be ignored when feeding to a
280 * shooter.
281 *
282 * The hardware limit faults and Forward/ReverseLimit signals will still report
283 * the values of the limit switches regardless of this parameter.
284 *
285 * \param newIgnoreHardwareLimits Parameter to modify
286 * \returns Itself
287 */
288 TorqueCurrentFOC &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
289 {
290 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
291 return *this;
292 }
293
294 /**
295 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
296 * method-chaining and easier to use request API.
297 *
298 * Set to true to delay applying this control request until a timesync boundary
299 * (requires Phoenix Pro and CANivore). This eliminates the impact of
300 * nondeterministic network delays in exchange for a larger but deterministic
301 * control latency.
302 *
303 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
304 * Additionally, when this is enabled, the UpdateFreqHz of this request should
305 * be set to 0 Hz.
306 *
307 * \param newUseTimesync Parameter to modify
308 * \returns Itself
309 */
310 TorqueCurrentFOC &WithUseTimesync(bool newUseTimesync)
311 {
312 UseTimesync = std::move(newUseTimesync);
313 return *this;
314 }
315 /**
316 * \brief Sets the period at which this control will update at.
317 * This is designated in Hertz, with a minimum of 20 Hz
318 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
319 *
320 * If this field is set to 0 Hz, the control request will
321 * be sent immediately as a one-shot frame. This may be useful
322 * for advanced applications that require outputs to be
323 * synchronized with data acquisition. In this case, we
324 * recommend not exceeding 50 ms between control calls.
325 *
326 * \param newUpdateFreqHz Parameter to modify
327 * \returns Itself
328 */
329 TorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
330 {
331 UpdateFreqHz = newUpdateFreqHz;
332 return *this;
333 }
334 /**
335 * \brief Returns a string representation of the object.
336 *
337 * \returns a string representation of the object.
338 */
339 std::string ToString() const override
340 {
341 std::stringstream ss;
342 ss << "Control: TorqueCurrentFOC" << std::endl;
343 ss << " Output: " << Output.to<double>() << " A" << std::endl;
344 ss << " MaxAbsDutyCycle: " << MaxAbsDutyCycle.to<double>() << " fractional" << std::endl;
345 ss << " Deadband: " << Deadband.to<double>() << " A" << std::endl;
346 ss << " OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
347 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
348 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
349 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
350 ss << " UseTimesync: " << UseTimesync << std::endl;
351 return ss.str();
352 }
353
354 /**
355 * \brief Gets information about this control request.
356 *
357 * \returns Map of control parameter names and corresponding applied values
358 */
359 std::map<std::string, std::string> GetControlInfo() const override
360 {
361 std::map<std::string, std::string> controlInfo;
362 std::stringstream ss;
363 controlInfo["Name"] = GetName();
364 ss << Output.to<double>(); controlInfo["Output"] = ss.str(); ss.str(std::string{});
365 ss << MaxAbsDutyCycle.to<double>(); controlInfo["MaxAbsDutyCycle"] = ss.str(); ss.str(std::string{});
366 ss << Deadband.to<double>(); controlInfo["Deadband"] = ss.str(); ss.str(std::string{});
367 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
368 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
369 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
370 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
371 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
372 return controlInfo;
373 }
374};
375
376}
377}
378}
379
CTREXPORT int c_ctre_phoenix6_RequestControlTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, double Output, double MaxAbsDutyCycle, double Deadband, bool OverrideCoastDurNeutral, 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
Requires Phoenix Pro; Request a specified motor current (field oriented control).
Definition TorqueCurrentFOC.hpp:31
TorqueCurrentFOC & WithDeadband(units::current::ampere_t newDeadband)
Modifies this Control Request's Deadband parameter and returns itself for method-chaining and easier ...
Definition TorqueCurrentFOC.hpp:213
TorqueCurrentFOC & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition TorqueCurrentFOC.hpp:248
TorqueCurrentFOC & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition TorqueCurrentFOC.hpp:310
TorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition TorqueCurrentFOC.hpp:329
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition TorqueCurrentFOC.hpp:359
TorqueCurrentFOC & WithOutput(units::current::ampere_t newOutput)
Modifies this Control Request's Output parameter and returns itself for method-chaining and easier to...
Definition TorqueCurrentFOC.hpp:167
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition TorqueCurrentFOC.hpp:114
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition TorqueCurrentFOC.hpp:125
TorqueCurrentFOC & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition TorqueCurrentFOC.hpp:265
units::current::ampere_t Output
Amount of motor current in Amperes.
Definition TorqueCurrentFOC.hpp:57
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition TorqueCurrentFOC.hpp:89
TorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition TorqueCurrentFOC.hpp:231
TorqueCurrentFOC & WithMaxAbsDutyCycle(units::dimensionless::scalar_t newMaxAbsDutyCycle)
Modifies this Control Request's MaxAbsDutyCycle parameter and returns itself for method-chaining and ...
Definition TorqueCurrentFOC.hpp:190
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition TorqueCurrentFOC.hpp:101
units::current::ampere_t Deadband
Deadband in Amperes.
Definition TorqueCurrentFOC.hpp:82
TorqueCurrentFOC & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition TorqueCurrentFOC.hpp:288
bool LimitForwardMotion
Set to true to force forward limiting.
Definition TorqueCurrentFOC.hpp:95
TorqueCurrentFOC(units::current::ampere_t Output)
Requires Phoenix Pro; Request a specified motor current (field oriented control).
Definition TorqueCurrentFOC.hpp:151
std::string ToString() const override
Returns a string representation of the object.
Definition TorqueCurrentFOC.hpp:339
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition TorqueCurrentFOC.hpp:138
units::dimensionless::scalar_t MaxAbsDutyCycle
The maximum absolute motor output that can be applied, which effectively limits the velocity.
Definition TorqueCurrentFOC.hpp:69
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
Definition span.hpp:401