Programming for Robotics with Arduino C++ 10, Challenge 3 solution (bits for the gains) Idea: the heading-and-offset filter of the chapter in integers (micro-radians), with the gain format as a parameter: a gain is stored as round(K * 2^F) and applied as (error * gain) >> F, for F from 4 to 20, 100 seeded runs of 120 s with the gyro and compass models of the chapter (no compass disturbances here). The settled gains K0 and K1 come from running the float filter on the PC. The float Kalman filter on the same runs is the last line. The product uses 64 bits on the host, so this tests only the rounding of the gains. CODE: // x3.cpp -- challenge 3: how many fractional bits do the fixed-point gains of HeadingFixed need? (host build; the sensor models are those of e_heading.cpp, ASSUMED, without the compass disturbances) // The filter below is HeadingFixed with the gain format as a parameter: gain = round(K * 2^F), update th += (y * gain) >> F, in micro-radians. On the host the product uses 64 bits, // so this tests the QUANTISATION of the gains only; whether the product fits in 32 bits on the Uno is a separate question (it is why est.h shifts y by 4 before multiplying). #include #include #include #include "sim.h" #include "est.h" struct Gen { int32_t th, b, k0, k1; int f; Gen(int fbits, float K0, float K1) : th(0), b(0), k0((int32_t)std::lround(K0 * (1 << fbits))), k1((int32_t)std::lround(K1 * (1 << fbits))), f(fbits) {} void predict(int32_t rate) { th += (rate - b) / 100; } void update(int32_t z) { int32_t y = z - th; th += (int32_t)(((int64_t)y * k0) >> f); b += (int32_t)(((int64_t)y * k1) >> f); } }; static void settled_gains(float& k0, float& k1) { Kalman2 k(-1.0f, 2.5e-7f, 2.5e-7f, 1.0f, 0.01f); for (int i = 0; i < 400; ++i) { for (int j = 0; j < 10; ++j) k.predict(0.0f, 0.01f); k.update_x0(0.0f, 0.0081f); } k0 = k.k0(); k1 = k.k1(); } int main() { float K0, K1; settled_gains(K0, K1); std::printf("settled gains: K0 = %.6f, K1 = %.7f\n", K0, K1); std::printf(" F (bits) K0 stored K1 stored rms error (deg) offset error at the end (deg/s)\n"); for (int F : {4, 6, 8, 10, 12, 14, 16, 20}) { double sq = 0, berr = 0; long n = 0; for (int seed = 1; seed <= 100; ++seed) { Rng rng((uint32_t)seed * 4271u + 3u); double bias = (rng.uniform() * 2 - 1) * 0.02, th = 0.0; Gen g(F, K0, K1); for (int i = 1; i <= 12000; ++i) { double t = i * 0.01, rate = t < 2.0 ? 0.0 : 0.8 * std::sin(2 * SIM_PI * (t - 2.0) / 20.0); th += rate * 0.01; bias += 0.0005 * std::sqrt(0.01) * rng.normal(); double gyro = rate + bias + 0.005 * rng.normal(); g.predict((int32_t)std::lround(gyro * 1e6)); if (i % 10 == 0) g.update((int32_t)std::lround((th + 0.09 * rng.normal()) * 1e6)); double e = g.th * 1e-6 - th; sq += e * e; ++n; if (i == 12000) berr += std::fabs(g.b * 1e-6 - bias); } } std::printf(" %7d %9d %9d %15.3f %31.4f\n", F, (int)std::lround(K0 * (1 << F)), (int)std::lround(K1 * (1 << F)), std::sqrt(sq / n) * 57.2958, berr / 100 * 57.2958); } { // the float Kalman filter for reference double sq = 0, berr = 0; long n = 0; for (int seed = 1; seed <= 100; ++seed) { Rng rng((uint32_t)seed * 4271u + 3u); double bias = (rng.uniform() * 2 - 1) * 0.02, th = 0.0; Kalman2 kf(-1.0f, 2.5e-7f, 2.5e-7f, 1.0f, 0.01f); for (int i = 1; i <= 12000; ++i) { double t = i * 0.01, rate = t < 2.0 ? 0.0 : 0.8 * std::sin(2 * SIM_PI * (t - 2.0) / 20.0); th += rate * 0.01; bias += 0.0005 * std::sqrt(0.01) * rng.normal(); double gyro = rate + bias + 0.005 * rng.normal(); kf.predict((float)gyro, 0.01f); if (i % 10 == 0) kf.update_x0((float)(th + 0.09 * rng.normal()), 0.0081f); double e = kf.x0() - th; sq += e * e; ++n; if (i == 12000) berr += std::fabs(kf.x1() - bias); } } std::printf(" %-8s %9s %9s %15.3f %31.4f\n", "float", "-", "-", std::sqrt(sq / n) * 57.2958, berr / 100 * 57.2958); } return 0; } END CODE OUTPUT: settled gains: K0 = 0.018665, K1 = -0.0017427 F (bits) K0 stored K1 stored rms error (deg) offset error at the end (deg/s) 4 0 0 48.496 0.6938 6 1 0 4.387 0.6938 8 5 0 3.554 0.6938 10 19 -2 1.022 0.0709 12 76 -7 1.058 0.0706 14 306 -29 1.046 0.0707 16 1223 -114 1.051 0.0706 20 19571 -1827 1.051 0.0707 float - - 0.835 0.0701 END OUTPUT Reading the output. The settled gains are K0 = 0.018665 (heading) and K1 = -0.0017427 (gyro offset). With 4 bits both round to zero, the filter is the gyro alone and the error is 48.5 degrees rms. With 6 and 8 bits K0 is stored (as 1/64 and 5/256, 16 % low and 5 % high) but K1 is 0, and the offset error is 0.69 degrees per second, the same as with no filter at all: the filter never learned the offset and the heading drifts (4.39 and 3.55 degrees rms). K1 is the gain that decides: it needs 0.0017427 x 2^F to be at least 0.5 to be anything but zero, which is 9 bits or more. At 10 bits K1 = -2 (-0.00195, 12 % high) and the error drops to 1.02 degrees and 0.071 degrees per second; from 12 bits (K0 = 76, K1 = -7) to 20 bits the results do not change (1.05 degrees, 0.0706). So 10 bits is the minimum, 12 is a comfortable choice and 16 (the Q16 of est.h) adds nothing in accuracy. What more bits cost is overflow, not accuracy. With F = 12 and the error clamped to 2 000 000 (micro-radians, about 2 radians), the product is at most 2 000 000 x 76 = 152 million, well inside a 32-bit integer; with F = 16 and K0 = 1223 it is 2.4 thousand million, which overflows, and that is why est.h shifts the error right by 4 before multiplying. I did that arithmetic by hand and did not run the integer version on the Uno. The float filter is better than any of the integer ones: 0.835 degrees against 1.05, even at 20 bits. So the gap is not the gain format. I did not isolate its cause; the two candidates are that the integer filter's gains are frozen at their settled values while the float filter's are larger in the first seconds, when it knows least, and the truncation of rate / 100 (up to 1 micro-radian in every 10 ms step). A run with the float gains replaced by the settled ones would tell them apart. Check on method: the offset error is measured at the end of the run only; the sensor models are the assumed ones of the chapter; one setting of q and R.