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