blob: 7fac76fdf30ae5819b0dea2c7ebc8dab1db1abdd [file] [log] [blame]
Brian Silvermand9566392018-06-10 15:02:03 -07001#include "motors/fet12/motor_controls.h"
2
3#include "motors/peripheral/configuration.h"
4
5namespace frc971 {
6namespace motors {
7namespace {
8
9template <int kRows, int kCols>
10using ComplexMatrix = MotorControlsImplementation::ComplexMatrix<kRows, kCols>;
11
12using Complex = ::std::complex<float>;
13
14constexpr int kCountsPerRevolution =
15 MotorControlsImplementation::constant_counts_per_revolution();
16
17#if 1
18constexpr double kMaxDutyCycle = 0.98;
19#elif 1
20constexpr double kMaxDutyCycle = 0.6;
21#elif 0
22constexpr double kMaxDutyCycle = 0.2;
23#endif
24
25constexpr int kPhaseBOffset = kCountsPerRevolution / 3;
26constexpr int kPhaseCOffset = 2 * kCountsPerRevolution / 3;
27
James Kuszmaul998d3032018-09-08 15:41:41 -070028constexpr double K1_unscaled = 1.0;
29constexpr double K2_unscaled = 1.0 / -6.4786;
Brian Silvermand9566392018-06-10 15:02:03 -070030
31// Make the amplitude of the fundamental 1 for ease of math.
32constexpr double K2 = K2_unscaled / K1_unscaled;
James Kuszmaul998d3032018-09-08 15:41:41 -070033constexpr double K1 = 1.0;
Brian Silvermand9566392018-06-10 15:02:03 -070034
35// volts
James Kuszmaul998d3032018-09-08 15:41:41 -070036constexpr double kVcc = 31.5;
Brian Silvermand9566392018-06-10 15:02:03 -070037
James Kuszmaul998d3032018-09-08 15:41:41 -070038constexpr double Kv = 22000.0 * 2.0 * M_PI / 60.0 / 30.0 * 3.6;
Brian Silvermand9566392018-06-10 15:02:03 -070039
James Kuszmaul998d3032018-09-08 15:41:41 -070040constexpr double kL = 6e-06;
41constexpr double kM = 0;
42constexpr double kR = 0.0084;
43constexpr float kAdiscrete_diagonal = 0.932394f;
44constexpr float kAdiscrete_offdiagonal = 0.0f;
45constexpr float kBdiscrete_inv_diagonal = 0.124249f;
46constexpr float kBdiscrete_inv_offdiagonal = 0.0f;
Brian Silvermand9566392018-06-10 15:02:03 -070047
James Kuszmaul998d3032018-09-08 15:41:41 -070048// The number to divide the product of the unit BEMF and the per phase current
49// by to get motor current.
50constexpr double kOneAmpScalar = 1.46426;
51constexpr double kMaxOneAmpDrivingVoltage = 0.024884;
Brian Silvermand9566392018-06-10 15:02:03 -070052
Brian Silvermand9566392018-06-10 15:02:03 -070053
54::Eigen::Matrix<float, 3, 3> A_discrete() {
55 ::Eigen::Matrix<float, 3, 3> r;
James Kuszmaul998d3032018-09-08 15:41:41 -070056 r << kAdiscrete_diagonal, kAdiscrete_offdiagonal, kAdiscrete_offdiagonal,
57 kAdiscrete_offdiagonal, kAdiscrete_diagonal, kAdiscrete_offdiagonal,
58 kAdiscrete_offdiagonal, kAdiscrete_offdiagonal, kAdiscrete_diagonal;
Brian Silvermand9566392018-06-10 15:02:03 -070059 return r;
60}
61
62::Eigen::Matrix<float, 3, 3> B_discrete_inverse() {
James Kuszmaul998d3032018-09-08 15:41:41 -070063 return ::Eigen::Matrix<float, 1, 3>::Constant(kBdiscrete_inv_diagonal)
64 .asDiagonal();
Brian Silvermand9566392018-06-10 15:02:03 -070065}
66
Brian Silvermand9566392018-06-10 15:02:03 -070067// Use FluxLinkageTable() to access this with a const so you don't accidentally
68// modify it.
69float flux_linkage_table[kCountsPerRevolution];
70
71void MakeFluxLinkageTable() {
72 for (int i = 0; i < kCountsPerRevolution; ++i) {
73 const double theta = static_cast<double>(i) /
74 static_cast<double>(kCountsPerRevolution) * 2.0 * M_PI;
75 flux_linkage_table[i] = K1 * sin(theta) - K2 * sin(theta * 5);
76 }
77}
78
79// theta doesn't have to be less than kCountsPerRevolution.
80::Eigen::Matrix<float, 3, 1> FluxLinkageAt(uint32_t theta) {
81 ::Eigen::Matrix<float, 3, 1> r;
82 r(0) = flux_linkage_table[theta % kCountsPerRevolution];
83 r(1) = flux_linkage_table[(theta + kPhaseBOffset) % kCountsPerRevolution];
84 r(2) = flux_linkage_table[(theta + kPhaseCOffset) % kCountsPerRevolution];
85 return r;
86}
87
88::Eigen::Matrix<float, 3, 3> MakeK() {
89 ::Eigen::Matrix<float, 3, 3> Vconv;
90 Vconv << 2.0f, -1.0f, -1.0f, -1.0f, 2.0f, -1.0f, -1.0f, -1.0f, 2.0f;
James Kuszmaul998d3032018-09-08 15:41:41 -070091 static constexpr float kControllerGain = 0.05f;
Brian Silvermand9566392018-06-10 15:02:03 -070092 return kControllerGain * (Vconv / 3.0f);
93}
94
95ComplexMatrix<3, 1> MakeE1Unrotated() {
96 ComplexMatrix<3, 1> rotation;
97 rotation << Complex(0, -1), Complex(::std::sqrt(3) / 2, 0.5),
98 Complex(-::std::sqrt(3) / 2, 0.5);
99 return K1 * rotation;
100}
101
102ComplexMatrix<3, 1> MakeE2Unrotated() {
103 ComplexMatrix<3, 1> rotation;
104 rotation << Complex(0, -1), Complex(-::std::sqrt(3) / 2, 0.5),
105 Complex(::std::sqrt(3) / 2, 0.5);
106 return K2 * rotation;
107}
108
109ComplexMatrix<3, 3> Hn(float omega, int scalar) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700110 const Complex a(static_cast<float>(kR),
111 omega * static_cast<float>(scalar * kL));
112 const Complex b(0, omega * static_cast<float>(scalar * kM));
Brian Silvermand9566392018-06-10 15:02:03 -0700113 const Complex temp1 = a + b;
114 const Complex temp2 = -b;
115 ComplexMatrix<3, 3> matrix;
116 matrix << temp1, temp2, temp2, temp2, temp1, temp2, temp2, temp2, temp1;
117 return matrix *
118 -(omega / static_cast<float>(Kv) / (a * a + a * b - 2.0f * b * b));
119}
120
121} // namespace
122
123MotorControlsImplementation::MotorControlsImplementation()
124 : E1Unrotated_(MakeE1Unrotated()), E2Unrotated_(MakeE2Unrotated()) {
125 MakeFluxLinkageTable();
126}
127
James Kuszmaul998d3032018-09-08 15:41:41 -0700128::std::array<float, 3> MotorControlsImplementation::DoIteration(
Brian Silvermand9566392018-06-10 15:02:03 -0700129 const float raw_currents[3], const uint32_t theta_in,
130 const float command_current) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700131 static constexpr float kCurrentSlewRate = 0.10f;
Brian Silvermand9566392018-06-10 15:02:03 -0700132 if (command_current > filtered_current_ + kCurrentSlewRate) {
133 filtered_current_ += kCurrentSlewRate;
134 } else if (command_current < filtered_current_ - kCurrentSlewRate) {
135 filtered_current_ -= kCurrentSlewRate;
136 } else {
137 filtered_current_ = command_current;
138 }
139 const float goal_current_in = filtered_current_;
140 const float max_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700141 (static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700142 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
143 static_cast<float>(kMaxOneAmpDrivingVoltage);
144 const float min_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700145 (-static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700146 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
147 static_cast<float>(kMaxOneAmpDrivingVoltage);
148 const float goal_current =
149 ::std::max(min_current, ::std::min(max_current, goal_current_in));
150
151#if 0
152 const uint32_t theta =
153 (theta_in + static_cast<uint32_t>(estimated_velocity_ * 1.0f)) % 1024;
154#elif 0
155 const uint32_t theta =
156 (theta_in + kCountsPerRevolution - 160u) % kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700157#elif 0
Brian Silvermand9566392018-06-10 15:02:03 -0700158 const uint32_t theta =
159 (theta_in + kCountsPerRevolution +
160 ((estimated_velocity_ > 0) ? (kCountsPerRevolution - 10u) : 60u)) %
161 kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700162#elif 1
Brian Silvermand9566392018-06-10 15:02:03 -0700163 const uint32_t theta = theta_in;
164#endif
165
166 const ::Eigen::Matrix<float, 3, 1> measured_current =
167 (::Eigen::Matrix<float, 3, 1>() << scale_current_reading(raw_currents[0]),
168 scale_current_reading(raw_currents[1]),
169 scale_current_reading(raw_currents[2])).finished();
170
171 const ComplexMatrix<3, 1> E1 =
172 E1Unrotated_ *
173 ImaginaryExpInt<::std::ratio<1, constant_counts_per_revolution()>>(theta);
174 const ComplexMatrix<3, 1> E2 =
175 E2Unrotated_ *
176 ImaginaryExpInt<::std::ratio<5, constant_counts_per_revolution()>>(theta);
177
178 const float overall_measured_current =
179 ((E1 + E2).real().transpose() * measured_current /
180 static_cast<float>(kOneAmpScalar))(0);
181 const float current_error = goal_current - overall_measured_current;
182 estimated_velocity_ += current_error * 0.1f;
183 debug_[3] = theta;
184 const float omega = estimated_velocity_;
185
186 debug_[4] = max_current * 10;
187
188 const ::Eigen::Matrix<float, 3, 1> I_now = I_last_;
189 const ::Eigen::Matrix<float, 3, 1> I_next =
James Kuszmaul998d3032018-09-08 15:41:41 -0700190 FluxLinkageAt(theta +
191 static_cast<int32_t>(
192 2.0f * omega * kCountsPerRevolution /
193 static_cast<float>(2.0 * M_PI * SWITCHING_FREQUENCY))) *
194 goal_current;
Brian Silvermand9566392018-06-10 15:02:03 -0700195
196 const ComplexMatrix<3, 3> H1 = Hn(omega, 1);
197 const ComplexMatrix<3, 3> H2 = Hn(omega, 5);
198
James Kuszmaul998d3032018-09-08 15:41:41 -0700199 const ::std::complex<float> first_speed_delay =
200 ImaginaryExpFloat(omega / SWITCHING_FREQUENCY);
201 const ::std::complex<float> fifth_speed_delay =
202 ImaginaryExpFloat(omega * 5.0f / SWITCHING_FREQUENCY);
203 const ComplexMatrix<3, 1> H1E1 = first_speed_delay * H1 * E1;
204 const ComplexMatrix<3, 1> H2E2 = fifth_speed_delay * H2 * E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700205 const ComplexMatrix<3, 1> p_imaginary = H1E1 + H2E2;
206 const ComplexMatrix<3, 1> p_next_imaginary =
James Kuszmaul998d3032018-09-08 15:41:41 -0700207 first_speed_delay * H1E1 + fifth_speed_delay * H2E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700208 const ::Eigen::Matrix<float, 3, 1> p = p_imaginary.real();
209 const ::Eigen::Matrix<float, 3, 1> p_next = p_next_imaginary.real();
210
211 const ::Eigen::Matrix<float, 3, 1> Vn_ff =
212 B_discrete_inverse() * (I_next - A_discrete() * (I_now - p) - p_next);
213 const ::Eigen::Matrix<float, 3, 1> Vn =
214 Vn_ff + MakeK() * (I_now - measured_current);
215
216 debug_[0] = (I_next)(0) * 100;
217 debug_[1] = (I_next)(1) * 100;
218 debug_[2] = (I_next)(2) * 100;
219
220 debug_[5] = Vn(0) * 100;
221 debug_[6] = Vn(1) * 100;
222 debug_[7] = Vn(2) * 100;
223
James Kuszmaul998d3032018-09-08 15:41:41 -0700224 ::Eigen::Matrix<float, 3, 1> times = Vn / kVcc;
Brian Silvermand9566392018-06-10 15:02:03 -0700225 {
226 const float min_time = times.minCoeff();
227 times -= ::Eigen::Matrix<float, 3, 1>::Constant(min_time);
228 }
229 {
230 const float max_time = times.maxCoeff();
231 const float scalar =
232 static_cast<float>(kMaxDutyCycle) /
233 ::std::max(static_cast<float>(kMaxDutyCycle), max_time);
234 times *= scalar;
235 }
236
237 I_last_ = I_next;
238
239 // TODO(Austin): Figure out why we need the min here.
James Kuszmaul998d3032018-09-08 15:41:41 -0700240 return {::std::max(0.0f, times(0)), ::std::max(0.0f, times(1)),
241 ::std::max(0.0f, times(2))};
Brian Silvermand9566392018-06-10 15:02:03 -0700242}
243
244int16_t MotorControlsImplementation::Debug(uint32_t theta) {
245 return debug_[theta];
246}
247
248} // namespace motors
249} // namespace frc971