blob: d6afab163bfc85169d33c7ee97f1f5c80e6e5e9e [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 Kuszmaul521eb652018-10-17 19:09:33 -070038// 3.6 and 1.15 are adjustments from calibrations.
39constexpr double Kv = 22000.0 * 2.0 * M_PI / 60.0 / 30.0 * 3.6 * 1.15;
Brian Silvermand9566392018-06-10 15:02:03 -070040
James Kuszmaul521eb652018-10-17 19:09:33 -070041constexpr double kL = 3e-06;
James Kuszmaul998d3032018-09-08 15:41:41 -070042constexpr double kM = 0;
James Kuszmaul521eb652018-10-17 19:09:33 -070043constexpr double kR = 0.01008;
44constexpr float kAdiscrete_diagonal = 0.845354f;
James Kuszmaul998d3032018-09-08 15:41:41 -070045constexpr float kAdiscrete_offdiagonal = 0.0f;
James Kuszmaul521eb652018-10-17 19:09:33 -070046constexpr float kBdiscrete_inv_diagonal = 0.0651811f;
James Kuszmaul998d3032018-09-08 15:41:41 -070047constexpr float kBdiscrete_inv_offdiagonal = 0.0f;
James Kuszmaul521eb652018-10-17 19:09:33 -070048constexpr double kOneAmpScalar = 1.46785;
49constexpr double kMaxOneAmpDrivingVoltage = 0.0265038;
Brian Silvermand9566392018-06-10 15:02:03 -070050
Brian Silvermand9566392018-06-10 15:02:03 -070051
52::Eigen::Matrix<float, 3, 3> A_discrete() {
53 ::Eigen::Matrix<float, 3, 3> r;
James Kuszmaul998d3032018-09-08 15:41:41 -070054 r << kAdiscrete_diagonal, kAdiscrete_offdiagonal, kAdiscrete_offdiagonal,
55 kAdiscrete_offdiagonal, kAdiscrete_diagonal, kAdiscrete_offdiagonal,
56 kAdiscrete_offdiagonal, kAdiscrete_offdiagonal, kAdiscrete_diagonal;
Brian Silvermand9566392018-06-10 15:02:03 -070057 return r;
58}
59
60::Eigen::Matrix<float, 3, 3> B_discrete_inverse() {
James Kuszmaul998d3032018-09-08 15:41:41 -070061 return ::Eigen::Matrix<float, 1, 3>::Constant(kBdiscrete_inv_diagonal)
62 .asDiagonal();
Brian Silvermand9566392018-06-10 15:02:03 -070063}
64
Brian Silvermand9566392018-06-10 15:02:03 -070065// Use FluxLinkageTable() to access this with a const so you don't accidentally
66// modify it.
67float flux_linkage_table[kCountsPerRevolution];
68
69void MakeFluxLinkageTable() {
70 for (int i = 0; i < kCountsPerRevolution; ++i) {
71 const double theta = static_cast<double>(i) /
72 static_cast<double>(kCountsPerRevolution) * 2.0 * M_PI;
73 flux_linkage_table[i] = K1 * sin(theta) - K2 * sin(theta * 5);
74 }
75}
76
77// theta doesn't have to be less than kCountsPerRevolution.
78::Eigen::Matrix<float, 3, 1> FluxLinkageAt(uint32_t theta) {
79 ::Eigen::Matrix<float, 3, 1> r;
80 r(0) = flux_linkage_table[theta % kCountsPerRevolution];
81 r(1) = flux_linkage_table[(theta + kPhaseBOffset) % kCountsPerRevolution];
82 r(2) = flux_linkage_table[(theta + kPhaseCOffset) % kCountsPerRevolution];
83 return r;
84}
85
86::Eigen::Matrix<float, 3, 3> MakeK() {
87 ::Eigen::Matrix<float, 3, 3> Vconv;
88 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 -070089 static constexpr float kControllerGain = 0.05f;
Brian Silvermand9566392018-06-10 15:02:03 -070090 return kControllerGain * (Vconv / 3.0f);
91}
92
93ComplexMatrix<3, 1> MakeE1Unrotated() {
94 ComplexMatrix<3, 1> rotation;
95 rotation << Complex(0, -1), Complex(::std::sqrt(3) / 2, 0.5),
96 Complex(-::std::sqrt(3) / 2, 0.5);
97 return K1 * rotation;
98}
99
100ComplexMatrix<3, 1> MakeE2Unrotated() {
101 ComplexMatrix<3, 1> rotation;
102 rotation << Complex(0, -1), Complex(-::std::sqrt(3) / 2, 0.5),
103 Complex(::std::sqrt(3) / 2, 0.5);
104 return K2 * rotation;
105}
106
107ComplexMatrix<3, 3> Hn(float omega, int scalar) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700108 const Complex a(static_cast<float>(kR),
109 omega * static_cast<float>(scalar * kL));
110 const Complex b(0, omega * static_cast<float>(scalar * kM));
Brian Silvermand9566392018-06-10 15:02:03 -0700111 const Complex temp1 = a + b;
112 const Complex temp2 = -b;
113 ComplexMatrix<3, 3> matrix;
114 matrix << temp1, temp2, temp2, temp2, temp1, temp2, temp2, temp2, temp1;
115 return matrix *
116 -(omega / static_cast<float>(Kv) / (a * a + a * b - 2.0f * b * b));
117}
118
119} // namespace
120
121MotorControlsImplementation::MotorControlsImplementation()
122 : E1Unrotated_(MakeE1Unrotated()), E2Unrotated_(MakeE2Unrotated()) {
123 MakeFluxLinkageTable();
124}
125
James Kuszmaul998d3032018-09-08 15:41:41 -0700126::std::array<float, 3> MotorControlsImplementation::DoIteration(
Brian Silvermand9566392018-06-10 15:02:03 -0700127 const float raw_currents[3], const uint32_t theta_in,
128 const float command_current) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700129 static constexpr float kCurrentSlewRate = 0.10f;
Brian Silvermand9566392018-06-10 15:02:03 -0700130 if (command_current > filtered_current_ + kCurrentSlewRate) {
131 filtered_current_ += kCurrentSlewRate;
132 } else if (command_current < filtered_current_ - kCurrentSlewRate) {
133 filtered_current_ -= kCurrentSlewRate;
134 } else {
135 filtered_current_ = command_current;
136 }
137 const float goal_current_in = filtered_current_;
138 const float max_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700139 (static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700140 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
141 static_cast<float>(kMaxOneAmpDrivingVoltage);
142 const float min_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700143 (-static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700144 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
145 static_cast<float>(kMaxOneAmpDrivingVoltage);
146 const float goal_current =
147 ::std::max(min_current, ::std::min(max_current, goal_current_in));
148
149#if 0
150 const uint32_t theta =
151 (theta_in + static_cast<uint32_t>(estimated_velocity_ * 1.0f)) % 1024;
152#elif 0
153 const uint32_t theta =
154 (theta_in + kCountsPerRevolution - 160u) % kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700155#elif 0
Brian Silvermand9566392018-06-10 15:02:03 -0700156 const uint32_t theta =
157 (theta_in + kCountsPerRevolution +
158 ((estimated_velocity_ > 0) ? (kCountsPerRevolution - 10u) : 60u)) %
159 kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700160#elif 1
Brian Silvermand9566392018-06-10 15:02:03 -0700161 const uint32_t theta = theta_in;
162#endif
163
164 const ::Eigen::Matrix<float, 3, 1> measured_current =
165 (::Eigen::Matrix<float, 3, 1>() << scale_current_reading(raw_currents[0]),
166 scale_current_reading(raw_currents[1]),
167 scale_current_reading(raw_currents[2])).finished();
168
169 const ComplexMatrix<3, 1> E1 =
170 E1Unrotated_ *
171 ImaginaryExpInt<::std::ratio<1, constant_counts_per_revolution()>>(theta);
172 const ComplexMatrix<3, 1> E2 =
173 E2Unrotated_ *
174 ImaginaryExpInt<::std::ratio<5, constant_counts_per_revolution()>>(theta);
175
176 const float overall_measured_current =
177 ((E1 + E2).real().transpose() * measured_current /
178 static_cast<float>(kOneAmpScalar))(0);
James Kuszmaul521eb652018-10-17 19:09:33 -0700179 overall_measured_current_ = overall_measured_current;
Brian Silvermand9566392018-06-10 15:02:03 -0700180 const float current_error = goal_current - overall_measured_current;
181 estimated_velocity_ += current_error * 0.1f;
182 debug_[3] = theta;
183 const float omega = estimated_velocity_;
184
185 debug_[4] = max_current * 10;
186
187 const ::Eigen::Matrix<float, 3, 1> I_now = I_last_;
188 const ::Eigen::Matrix<float, 3, 1> I_next =
James Kuszmaul998d3032018-09-08 15:41:41 -0700189 FluxLinkageAt(theta +
190 static_cast<int32_t>(
191 2.0f * omega * kCountsPerRevolution /
192 static_cast<float>(2.0 * M_PI * SWITCHING_FREQUENCY))) *
193 goal_current;
Brian Silvermand9566392018-06-10 15:02:03 -0700194
195 const ComplexMatrix<3, 3> H1 = Hn(omega, 1);
196 const ComplexMatrix<3, 3> H2 = Hn(omega, 5);
197
James Kuszmaul998d3032018-09-08 15:41:41 -0700198 const ::std::complex<float> first_speed_delay =
199 ImaginaryExpFloat(omega / SWITCHING_FREQUENCY);
200 const ::std::complex<float> fifth_speed_delay =
201 ImaginaryExpFloat(omega * 5.0f / SWITCHING_FREQUENCY);
202 const ComplexMatrix<3, 1> H1E1 = first_speed_delay * H1 * E1;
203 const ComplexMatrix<3, 1> H2E2 = fifth_speed_delay * H2 * E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700204 const ComplexMatrix<3, 1> p_imaginary = H1E1 + H2E2;
205 const ComplexMatrix<3, 1> p_next_imaginary =
James Kuszmaul998d3032018-09-08 15:41:41 -0700206 first_speed_delay * H1E1 + fifth_speed_delay * H2E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700207 const ::Eigen::Matrix<float, 3, 1> p = p_imaginary.real();
208 const ::Eigen::Matrix<float, 3, 1> p_next = p_next_imaginary.real();
209
210 const ::Eigen::Matrix<float, 3, 1> Vn_ff =
211 B_discrete_inverse() * (I_next - A_discrete() * (I_now - p) - p_next);
212 const ::Eigen::Matrix<float, 3, 1> Vn =
James Kuszmaul521eb652018-10-17 19:09:33 -0700213 Vn_ff + MakeK() * (I_prev_ - measured_current);
Brian Silvermand9566392018-06-10 15:02:03 -0700214
215 debug_[0] = (I_next)(0) * 100;
216 debug_[1] = (I_next)(1) * 100;
217 debug_[2] = (I_next)(2) * 100;
218
219 debug_[5] = Vn(0) * 100;
220 debug_[6] = Vn(1) * 100;
221 debug_[7] = Vn(2) * 100;
222
James Kuszmaul998d3032018-09-08 15:41:41 -0700223 ::Eigen::Matrix<float, 3, 1> times = Vn / kVcc;
Brian Silvermand9566392018-06-10 15:02:03 -0700224 {
225 const float min_time = times.minCoeff();
226 times -= ::Eigen::Matrix<float, 3, 1>::Constant(min_time);
227 }
228 {
229 const float max_time = times.maxCoeff();
230 const float scalar =
231 static_cast<float>(kMaxDutyCycle) /
232 ::std::max(static_cast<float>(kMaxDutyCycle), max_time);
233 times *= scalar;
234 }
235
James Kuszmaul521eb652018-10-17 19:09:33 -0700236 I_prev_ = I_now;
Brian Silvermand9566392018-06-10 15:02:03 -0700237 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