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
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
12#include <sstream>
13#include <units/current.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 * Requires Phoenix Pro;
25 * Request a specified motor current (field oriented control).
26 *
27 * This control request will drive the motor to the requested motor (stator) current value. This leverages
28 * field oriented control (FOC), which means greater peak power than what is documented. This scales to
29 * torque based on Motor's kT constant.
30 */
32{
33 bool ApplyConfigsOnRequest{false};
34
35 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
36 {
37 if (req.get() != this)
38 {
39 auto const reqCast = dynamic_cast<TorqueCurrentFOC *>(req.get());
40 if (reqCast != nullptr)
41 {
42 *reqCast = *this;
43 }
44 else
45 {
46 req = std::make_shared<TorqueCurrentFOC>(*this);
47 }
48 }
49
50 std::stringstream ss;
51
52 std::string strs{ss.str()};
53 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
54 ApplyConfigsOnRequest = false;
55 return c_ctre_phoenix6_RequestControlTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Output.to<double>(), MaxAbsDutyCycle.to<double>(), Deadband.to<double>(), OverrideCoastDurNeutral);
56 }
57
58public:
59 /**
60 * Amount of motor current in Amperes
61 */
62 units::current::ampere_t Output;
63 /**
64 * The maximum absolute motor output that can be applied, which effectively
65 * limits the velocity. For example, 0.50 means no more than 50% output in
66 * either direction. This is useful for preventing the motor from spinning to
67 * its terminal velocity when there is no external torque applied unto the
68 * rotor. Note this is absolute maximum, so the value should be between zero
69 * and one.
70 */
71 units::dimensionless::scalar_t MaxAbsDutyCycle;
72 /**
73 * Deadband in Amperes. If torque request is within deadband, the bridge output
74 * is neutral. If deadband is set to zero then there is effectively no deadband.
75 * Note if deadband is zero, a free spinning motor will spin for quite a while
76 * as the firmware attempts to hold the motor's bemf. If user expects motor to
77 * cease spinning quickly with a demand of zero, we recommend a deadband of one
78 * Ampere. This value will be converted to an integral value of amps.
79 */
80 units::current::ampere_t Deadband;
81 /**
82 * Set to true to coast the rotor when output is zero (or within deadband). Set
83 * to false to use the NeutralMode configuration setting (default). This flag
84 * exists to provide the fundamental behavior of this control when output is
85 * zero, which is to provide 0A (zero torque).
86 */
88
89
90
91 /**
92 * \brief The timeout when sending configs associated with this control
93 */
94 units::time::second_t ConfigTimeout{0.1_s};
95
96 /**
97 * \brief The period at which this control will update at.
98 * This is designated in Hertz, with a minimum of 20 Hz
99 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
100 *
101 * If this field is set to 0 Hz, the control request will
102 * be sent immediately as a one-shot frame. This may be useful
103 * for advanced applications that require outputs to be
104 * synchronized with data acquisition. In this case, we
105 * recommend not exceeding 50 ms between control calls.
106 */
107 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
108
109 /**
110 * \brief Requires Phoenix Pro;
111 * Request a specified motor current (field
112 * oriented control).
113 *
114 * \details This control request will drive the motor to the requested motor
115 * (stator) current value. This leverages field oriented control
116 * (FOC), which means greater peak power than what is documented. This
117 * scales to torque based on Motor's kT constant.
118 *
119 * \param Output Amount of motor current in Amperes
120 * \param MaxAbsDutyCycle The maximum absolute motor output that can be
121 * applied, which effectively limits the velocity. For
122 * example, 0.50 means no more than 50% output in
123 * either direction. This is useful for preventing
124 * the motor from spinning to its terminal velocity
125 * when there is no external torque applied unto the
126 * rotor. Note this is absolute maximum, so the value
127 * should be between zero and one.
128 * \param Deadband Deadband in Amperes. If torque request is within
129 * deadband, the bridge output is neutral. If deadband is set
130 * to zero then there is effectively no deadband. Note if
131 * deadband is zero, a free spinning motor will spin for
132 * quite a while as the firmware attempts to hold the motor's
133 * bemf. If user expects motor to cease spinning quickly with
134 * a demand of zero, we recommend a deadband of one Ampere.
135 * This value will be converted to an integral value of amps.
136 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
137 * is zero (or within deadband). Set to false
138 * to use the NeutralMode configuration
139 * setting (default). This flag exists to
140 * provide the fundamental behavior of this
141 * control when output is zero, which is to
142 * provide 0A (zero torque).
143 */
144 TorqueCurrentFOC(units::current::ampere_t Output, units::dimensionless::scalar_t MaxAbsDutyCycle, units::current::ampere_t Deadband, bool OverrideCoastDurNeutral) : ControlRequest{"TorqueCurrentFOC"},
145 Output{std::move(Output)},
147 Deadband{std::move(Deadband)},
149 {}
150
151 /**
152 * \brief Requires Phoenix Pro;
153 * Request a specified motor current (field
154 * oriented control).
155 *
156 * \details This control request will drive the motor to the requested motor
157 * (stator) current value. This leverages field oriented control
158 * (FOC), which means greater peak power than what is documented. This
159 * scales to torque based on Motor's kT constant.
160 *
161 * \param Output Amount of motor current in Amperes
162 */
163 TorqueCurrentFOC(units::current::ampere_t Output) : TorqueCurrentFOC{Output, 1.0, 0.0_A, false}
164 {}
165
166 /**
167 * \brief Modifies this Control Request's Output parameter and returns itself for
168 * method-chaining and easier to use request API.
169 * \param newOutput Parameter to modify
170 * \returns Itself
171 */
172 TorqueCurrentFOC& WithOutput(units::current::ampere_t newOutput)
173 {
174 Output = std::move(newOutput);
175 return *this;
176 }
177
178 /**
179 * \brief Modifies this Control Request's MaxAbsDutyCycle parameter and returns itself for
180 * method-chaining and easier to use request API.
181 * \param newMaxAbsDutyCycle Parameter to modify
182 * \returns Itself
183 */
184 TorqueCurrentFOC& WithMaxAbsDutyCycle(units::dimensionless::scalar_t newMaxAbsDutyCycle)
185 {
186 MaxAbsDutyCycle = std::move(newMaxAbsDutyCycle);
187 return *this;
188 }
189
190 /**
191 * \brief Modifies this Control Request's Deadband parameter and returns itself for
192 * method-chaining and easier to use request API.
193 * \param newDeadband Parameter to modify
194 * \returns Itself
195 */
196 TorqueCurrentFOC& WithDeadband(units::current::ampere_t newDeadband)
197 {
198 Deadband = std::move(newDeadband);
199 return *this;
200 }
201
202 /**
203 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
204 * method-chaining and easier to use request API.
205 * \param newOverrideCoastDurNeutral Parameter to modify
206 * \returns Itself
207 */
208 TorqueCurrentFOC& WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
209 {
210 OverrideCoastDurNeutral = std::move(newOverrideCoastDurNeutral);
211 return *this;
212 }
213 /**
214 * \brief Sets the period at which this control will update at.
215 * This is designated in Hertz, with a minimum of 20 Hz
216 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
217 *
218 * If this field is set to 0 Hz, the control request will
219 * be sent immediately as a one-shot frame. This may be useful
220 * for advanced applications that require outputs to be
221 * synchronized with data acquisition. In this case, we
222 * recommend not exceeding 50 ms between control calls.
223 *
224 * \param newUpdateFreqHz Parameter to modify
225 * \returns Itself
226 */
227 TorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
228 {
229 UpdateFreqHz = newUpdateFreqHz;
230 return *this;
231 }
232 /**
233 * Returns a string representation of the object.
234 *
235 * \returns a string representation of the object.
236 */
237 std::string ToString() const override
238 {
239 std::stringstream ss;
240 ss << "class: TorqueCurrentFOC" << std::endl;
241 ss << "Output: " << Output.to<double>() << std::endl;
242 ss << "MaxAbsDutyCycle: " << MaxAbsDutyCycle.to<double>() << std::endl;
243 ss << "Deadband: " << Deadband.to<double>() << std::endl;
244 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
245 return ss.str();
246 }
247
248 /**
249 * \brief Forces configs to be applied the next time this is used in a setControl.
250 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
251 * properly set when they are not already set
252 */
253 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
254
255 /**
256 * \brief Gets information about this control request.
257 *
258 * \returns Map of control parameter names and corresponding applied values
259 */
260 std::map<std::string, std::string> GetControlInfo() const override
261 {
262 std::map<std::string, std::string> controlInfo;
263 std::stringstream ss;
264 controlInfo["Name"] = GetName();
265 ss << Output.to<double>(); controlInfo["Output"] = ss.str(); ss.str(std::string{});
266 ss << MaxAbsDutyCycle.to<double>(); controlInfo["MaxAbsDutyCycle"] = ss.str(); ss.str(std::string{});
267 ss << Deadband.to<double>(); controlInfo["Deadband"] = ss.str(); ss.str(std::string{});
268 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
269 return controlInfo;
270 }
271};
272
273}
274}
275}
276
CTREXPORT int c_ctre_phoenix6_RequestControlTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Output, double MaxAbsDutyCycle, double Deadband, bool OverrideCoastDurNeutral)
CTREXPORT int c_ctre_phoenix6_requestConfigApply(const char *canbus, uint32_t ecuEncoding, double timeoutSeconds, const char *str, uint32_t strlen, bool forceApply)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Requires Phoenix Pro; Request a specified motor current (field oriented control).
Definition: TorqueCurrentFOC.hpp:32
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:196
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: TorqueCurrentFOC.hpp:94
TorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: TorqueCurrentFOC.hpp:227
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: TorqueCurrentFOC.hpp:260
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:172
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: TorqueCurrentFOC.hpp:253
units::current::ampere_t Output
Amount of motor current in Amperes.
Definition: TorqueCurrentFOC.hpp:62
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: TorqueCurrentFOC.hpp:87
TorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: TorqueCurrentFOC.hpp:208
TorqueCurrentFOC & WithMaxAbsDutyCycle(units::dimensionless::scalar_t newMaxAbsDutyCycle)
Modifies this Control Request's MaxAbsDutyCycle parameter and returns itself for method-chaining and ...
Definition: TorqueCurrentFOC.hpp:184
units::current::ampere_t Deadband
Deadband in Amperes.
Definition: TorqueCurrentFOC.hpp:80
TorqueCurrentFOC(units::current::ampere_t Output, units::dimensionless::scalar_t MaxAbsDutyCycle, units::current::ampere_t Deadband, bool OverrideCoastDurNeutral)
Requires Phoenix Pro; Request a specified motor current (field oriented control).
Definition: TorqueCurrentFOC.hpp:144
TorqueCurrentFOC(units::current::ampere_t Output)
Requires Phoenix Pro; Request a specified motor current (field oriented control).
Definition: TorqueCurrentFOC.hpp:163
std::string ToString() const override
Returns a string representation of the object.
Definition: TorqueCurrentFOC.hpp:237
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: TorqueCurrentFOC.hpp:107
units::dimensionless::scalar_t MaxAbsDutyCycle
The maximum absolute motor output that can be applied, which effectively limits the velocity.
Definition: TorqueCurrentFOC.hpp:71
Definition: ManualEvent.hpp:12