blob: 9e42ca892a5cdfdc630364d6319097270ca0ebae [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
Brian Silverman37a95d62018-11-09 16:08:32 -080041constexpr double kL = 5e-06;
James Kuszmaul998d3032018-09-08 15:41:41 -070042constexpr double kM = 0;
Brian Silverman37a95d62018-11-09 16:08:32 -080043constexpr double kR = 0.0079;
44constexpr float kAdiscrete_diagonal = 0.92404f;
James Kuszmaul998d3032018-09-08 15:41:41 -070045constexpr float kAdiscrete_offdiagonal = 0.0f;
Brian Silverman37a95d62018-11-09 16:08:32 -080046constexpr float kBdiscrete_inv_diagonal = 0.104002f;
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;
Brian Silverman37a95d62018-11-09 16:08:32 -080049constexpr double kMaxOneAmpDrivingVoltage = 0.0434948;
Brian Silvermand9566392018-06-10 15:02:03 -070050
51::Eigen::Matrix<float, 3, 3> A_discrete() {
52 ::Eigen::Matrix<float, 3, 3> r;
James Kuszmaul998d3032018-09-08 15:41:41 -070053 r << kAdiscrete_diagonal, kAdiscrete_offdiagonal, kAdiscrete_offdiagonal,
54 kAdiscrete_offdiagonal, kAdiscrete_diagonal, kAdiscrete_offdiagonal,
55 kAdiscrete_offdiagonal, kAdiscrete_offdiagonal, kAdiscrete_diagonal;
Brian Silvermand9566392018-06-10 15:02:03 -070056 return r;
57}
58
59::Eigen::Matrix<float, 3, 3> B_discrete_inverse() {
James Kuszmaul998d3032018-09-08 15:41:41 -070060 return ::Eigen::Matrix<float, 1, 3>::Constant(kBdiscrete_inv_diagonal)
61 .asDiagonal();
Brian Silvermand9566392018-06-10 15:02:03 -070062}
63
Brian Silvermand9566392018-06-10 15:02:03 -070064// Use FluxLinkageTable() to access this with a const so you don't accidentally
65// modify it.
66float flux_linkage_table[kCountsPerRevolution];
67
68void MakeFluxLinkageTable() {
69 for (int i = 0; i < kCountsPerRevolution; ++i) {
70 const double theta = static_cast<double>(i) /
71 static_cast<double>(kCountsPerRevolution) * 2.0 * M_PI;
72 flux_linkage_table[i] = K1 * sin(theta) - K2 * sin(theta * 5);
73 }
74}
75
76// theta doesn't have to be less than kCountsPerRevolution.
77::Eigen::Matrix<float, 3, 1> FluxLinkageAt(uint32_t theta) {
78 ::Eigen::Matrix<float, 3, 1> r;
79 r(0) = flux_linkage_table[theta % kCountsPerRevolution];
80 r(1) = flux_linkage_table[(theta + kPhaseBOffset) % kCountsPerRevolution];
81 r(2) = flux_linkage_table[(theta + kPhaseCOffset) % kCountsPerRevolution];
82 return r;
83}
84
85::Eigen::Matrix<float, 3, 3> MakeK() {
86 ::Eigen::Matrix<float, 3, 3> Vconv;
87 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 -070088 static constexpr float kControllerGain = 0.05f;
Brian Silvermand9566392018-06-10 15:02:03 -070089 return kControllerGain * (Vconv / 3.0f);
90}
91
92ComplexMatrix<3, 1> MakeE1Unrotated() {
93 ComplexMatrix<3, 1> rotation;
94 rotation << Complex(0, -1), Complex(::std::sqrt(3) / 2, 0.5),
95 Complex(-::std::sqrt(3) / 2, 0.5);
96 return K1 * rotation;
97}
98
99ComplexMatrix<3, 1> MakeE2Unrotated() {
100 ComplexMatrix<3, 1> rotation;
101 rotation << Complex(0, -1), Complex(-::std::sqrt(3) / 2, 0.5),
102 Complex(::std::sqrt(3) / 2, 0.5);
103 return K2 * rotation;
104}
105
106ComplexMatrix<3, 3> Hn(float omega, int scalar) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700107 const Complex a(static_cast<float>(kR),
108 omega * static_cast<float>(scalar * kL));
109 const Complex b(0, omega * static_cast<float>(scalar * kM));
Brian Silvermand9566392018-06-10 15:02:03 -0700110 const Complex temp1 = a + b;
111 const Complex temp2 = -b;
112 ComplexMatrix<3, 3> matrix;
113 matrix << temp1, temp2, temp2, temp2, temp1, temp2, temp2, temp2, temp1;
114 return matrix *
115 -(omega / static_cast<float>(Kv) / (a * a + a * b - 2.0f * b * b));
116}
117
118} // namespace
119
120MotorControlsImplementation::MotorControlsImplementation()
121 : E1Unrotated_(MakeE1Unrotated()), E2Unrotated_(MakeE2Unrotated()) {
122 MakeFluxLinkageTable();
123}
124
James Kuszmaul998d3032018-09-08 15:41:41 -0700125::std::array<float, 3> MotorControlsImplementation::DoIteration(
Brian Silvermand9566392018-06-10 15:02:03 -0700126 const float raw_currents[3], const uint32_t theta_in,
127 const float command_current) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700128 static constexpr float kCurrentSlewRate = 0.10f;
Brian Silvermand9566392018-06-10 15:02:03 -0700129 if (command_current > filtered_current_ + kCurrentSlewRate) {
130 filtered_current_ += kCurrentSlewRate;
131 } else if (command_current < filtered_current_ - kCurrentSlewRate) {
132 filtered_current_ -= kCurrentSlewRate;
133 } else {
134 filtered_current_ = command_current;
135 }
136 const float goal_current_in = filtered_current_;
137 const float max_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700138 (static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700139 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
140 static_cast<float>(kMaxOneAmpDrivingVoltage);
141 const float min_current =
James Kuszmaul998d3032018-09-08 15:41:41 -0700142 (-static_cast<float>(kVcc * kMaxDutyCycle) -
Brian Silvermand9566392018-06-10 15:02:03 -0700143 estimated_velocity_ / static_cast<float>(Kv / 2.0)) /
144 static_cast<float>(kMaxOneAmpDrivingVoltage);
145 const float goal_current =
146 ::std::max(min_current, ::std::min(max_current, goal_current_in));
147
148#if 0
149 const uint32_t theta =
150 (theta_in + static_cast<uint32_t>(estimated_velocity_ * 1.0f)) % 1024;
151#elif 0
152 const uint32_t theta =
153 (theta_in + kCountsPerRevolution - 160u) % kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700154#elif 0
Brian Silvermand9566392018-06-10 15:02:03 -0700155 const uint32_t theta =
156 (theta_in + kCountsPerRevolution +
157 ((estimated_velocity_ > 0) ? (kCountsPerRevolution - 10u) : 60u)) %
158 kCountsPerRevolution;
James Kuszmaul998d3032018-09-08 15:41:41 -0700159#elif 1
Brian Silvermand9566392018-06-10 15:02:03 -0700160 const uint32_t theta = theta_in;
161#endif
162
163 const ::Eigen::Matrix<float, 3, 1> measured_current =
164 (::Eigen::Matrix<float, 3, 1>() << scale_current_reading(raw_currents[0]),
165 scale_current_reading(raw_currents[1]),
166 scale_current_reading(raw_currents[2])).finished();
167
168 const ComplexMatrix<3, 1> E1 =
169 E1Unrotated_ *
170 ImaginaryExpInt<::std::ratio<1, constant_counts_per_revolution()>>(theta);
171 const ComplexMatrix<3, 1> E2 =
172 E2Unrotated_ *
173 ImaginaryExpInt<::std::ratio<5, constant_counts_per_revolution()>>(theta);
174
175 const float overall_measured_current =
176 ((E1 + E2).real().transpose() * measured_current /
177 static_cast<float>(kOneAmpScalar))(0);
James Kuszmaul521eb652018-10-17 19:09:33 -0700178 overall_measured_current_ = overall_measured_current;
Brian Silvermand9566392018-06-10 15:02:03 -0700179 const float current_error = goal_current - overall_measured_current;
180 estimated_velocity_ += current_error * 0.1f;
181 debug_[3] = theta;
182 const float omega = estimated_velocity_;
183
184 debug_[4] = max_current * 10;
185
186 const ::Eigen::Matrix<float, 3, 1> I_now = I_last_;
187 const ::Eigen::Matrix<float, 3, 1> I_next =
James Kuszmaul998d3032018-09-08 15:41:41 -0700188 FluxLinkageAt(theta +
189 static_cast<int32_t>(
190 2.0f * omega * kCountsPerRevolution /
191 static_cast<float>(2.0 * M_PI * SWITCHING_FREQUENCY))) *
192 goal_current;
Brian Silvermand9566392018-06-10 15:02:03 -0700193
194 const ComplexMatrix<3, 3> H1 = Hn(omega, 1);
195 const ComplexMatrix<3, 3> H2 = Hn(omega, 5);
196
James Kuszmaul998d3032018-09-08 15:41:41 -0700197 const ::std::complex<float> first_speed_delay =
198 ImaginaryExpFloat(omega / SWITCHING_FREQUENCY);
199 const ::std::complex<float> fifth_speed_delay =
200 ImaginaryExpFloat(omega * 5.0f / SWITCHING_FREQUENCY);
201 const ComplexMatrix<3, 1> H1E1 = first_speed_delay * H1 * E1;
202 const ComplexMatrix<3, 1> H2E2 = fifth_speed_delay * H2 * E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700203 const ComplexMatrix<3, 1> p_imaginary = H1E1 + H2E2;
204 const ComplexMatrix<3, 1> p_next_imaginary =
James Kuszmaul998d3032018-09-08 15:41:41 -0700205 first_speed_delay * H1E1 + fifth_speed_delay * H2E2;
Brian Silvermand9566392018-06-10 15:02:03 -0700206 const ::Eigen::Matrix<float, 3, 1> p = p_imaginary.real();
207 const ::Eigen::Matrix<float, 3, 1> p_next = p_next_imaginary.real();
208
209 const ::Eigen::Matrix<float, 3, 1> Vn_ff =
210 B_discrete_inverse() * (I_next - A_discrete() * (I_now - p) - p_next);
211 const ::Eigen::Matrix<float, 3, 1> Vn =
James Kuszmaul521eb652018-10-17 19:09:33 -0700212 Vn_ff + MakeK() * (I_prev_ - measured_current);
Brian Silvermand9566392018-06-10 15:02:03 -0700213
214 debug_[0] = (I_next)(0) * 100;
215 debug_[1] = (I_next)(1) * 100;
216 debug_[2] = (I_next)(2) * 100;
217
218 debug_[5] = Vn(0) * 100;
219 debug_[6] = Vn(1) * 100;
220 debug_[7] = Vn(2) * 100;
221
James Kuszmaul998d3032018-09-08 15:41:41 -0700222 ::Eigen::Matrix<float, 3, 1> times = Vn / kVcc;
Brian Silvermand9566392018-06-10 15:02:03 -0700223 {
224 const float min_time = times.minCoeff();
225 times -= ::Eigen::Matrix<float, 3, 1>::Constant(min_time);
226 }
227 {
228 const float max_time = times.maxCoeff();
229 const float scalar =
230 static_cast<float>(kMaxDutyCycle) /
231 ::std::max(static_cast<float>(kMaxDutyCycle), max_time);
232 times *= scalar;
233 }
234
James Kuszmaul521eb652018-10-17 19:09:33 -0700235 I_prev_ = I_now;
Brian Silvermand9566392018-06-10 15:02:03 -0700236 I_last_ = I_next;
237
238 // TODO(Austin): Figure out why we need the min here.
James Kuszmaul998d3032018-09-08 15:41:41 -0700239 return {::std::max(0.0f, times(0)), ::std::max(0.0f, times(1)),
240 ::std::max(0.0f, times(2))};
Brian Silvermand9566392018-06-10 15:02:03 -0700241}
242
243int16_t MotorControlsImplementation::Debug(uint32_t theta) {
244 return debug_[theta];
245}
246
247} // namespace motors
248} // namespace frc971