Programming for Robotics with Arduino C++ 10, Challenge 2 solution (tune the position and speed filter) Idea: the chapter's wall run (range every 60 ms with 5 mm of noise and 2 % dropouts; no odometry), the speed process noise q1 swept from 10 to 100 000 and the range noise R the filter is told swept over 4, 25 and 250 (the true value is 25), 200 seeded runs. Each cell gives the rms position error, the rms speed error and the lag after the robot has stopped at 10 s: the time until the speed estimate has fallen below 10 mm/s (4 s counted for a run that never did). CODE: // x2.cpp -- challenge 2: tune the position/speed filter of the chapter. Same wall run as e_posvel.cpp, range only (no odometry), 200 runs (the sensor models are ASSUMED). // The speed process noise q1 and the range noise R the filter is TOLD (the true noise is 5 mm, R = 25) are swept; each cell gives the rms position and speed error over the 12 s run, // and the lag: the time after the robot has stopped (t = 10 s) until the speed estimate has fallen below 10 mm/s (mean over the runs; a run that has not done so within 4 s counts as 4). #include #include #include #include "sim.h" #include "est.h" static double speed_at(double t) { if (t < 2.0) return 100.0 * t; if (t < 8.0) return 200.0; if (t < 10.0) return 200.0 - 100.0 * (t - 8.0); return 0.0; } struct Out { double pos, spd, lag; }; static Out run(double q1, double r, int seeds) { double sp = 0, ss = 0, lag = 0; long n = 0; for (int seed = 1; seed <= seeds; ++seed) { Rng rng((uint32_t)seed * 911u + 17u); double x = 300.0; double settled = -1.0; Kalman2 kf(1.0f, 0.01f, (float)q1, 100.0f, 10000.0f); kf.set(300.0f, 0.0f); for (int i = 1; i <= 700; ++i) { // 14 s double t = i * 0.02, v = speed_at(t); x += v * 0.02; kf.predict(0.0f, 0.02f); if (i % 3 == 0 && rng.uniform() >= 0.02) kf.update_x0((float)(x + 5.0 * rng.normal()), (float)r); if (t <= 12.0) { double ep = kf.x0() - x, ev = kf.x1() - v; sp += ep * ep; ss += ev * ev; ++n; } if (t > 10.0 && settled < 0 && std::fabs(kf.x1()) < 10.0) settled = t - 10.0; } lag += settled < 0 ? 4.0 : settled; } Out o = {std::sqrt(sp / n), std::sqrt(ss / n), lag / seeds}; return o; } int main() { std::printf("position/speed filter, range only; rows: q1 (speed process noise), columns: the R the filter is told (true 25)\n"); std::printf(" each cell: position rms mm / speed rms mm/s / lag after the stop (s)\n"); std::printf(" q1 "); for (double r : {4.0, 25.0, 250.0}) std::printf(" R = %-5.0f ", r); std::printf("\n"); for (double q1 : {10.0, 100.0, 1000.0, 10000.0, 100000.0}) { std::printf(" %-7.0f ", q1); for (double r : {4.0, 25.0, 250.0}) { Out o = run(q1, r, 200); std::printf(" %6.2f / %6.2f / %5.2f ", o.pos, o.spd, o.lag); } std::printf("\n"); } return 0; } END CODE OUTPUT: position/speed filter, range only; rows: q1 (speed process noise), columns: the R the filter is told (true 25) each cell: position rms mm / speed rms mm/s / lag after the stop (s) q1 R = 4 R = 25 R = 250 10 7.88 / 30.28 / 0.57 16.70 / 42.62 / 1.11 40.15 / 62.71 / 2.41 100 3.78 / 19.78 / 0.23 6.53 / 26.70 / 0.48 17.10 / 43.27 / 1.11 1000 3.67 / 20.45 / 0.13 3.54 / 17.57 / 0.18 6.60 / 26.67 / 0.48 10000 4.65 / 38.95 / 0.27 3.80 / 21.61 / 0.14 3.50 / 17.13 / 0.18 100000 6.07 / 82.07 / 0.57 4.89 / 45.19 / 0.29 3.77 / 21.22 / 0.14 END OUTPUT Reading the output. Cells with the same ratio q1 / R give nearly the same result, whatever the two numbers are. Ratio 40: q1 = 1000 with R = 25 gives 3.54 mm, 17.57 mm/s and 0.18 s of lag; q1 = 10 000 with R = 250 gives 3.50, 17.13 and 0.18. Ratio 400: 3.80 / 21.61 / 0.14 and 3.77 / 21.22 / 0.14. Ratio 4: 6.53 / 26.70 / 0.48 and 6.60 / 26.67 / 0.48. So the filter has one real knob, the ratio of how much the speed may change to how much a reading may be wrong; the two numbers separately matter only for the filter's own reported uncertainty, which this run does not use. The trade is lag against noise. A low ratio (0.04 to 4) trusts the model's constant speed: the filter is smooth but slow to notice the robot has stopped (2.41 s of lag at ratio 0.04, 1.11 s at 0.4, 0.48 s at 4) and its errors are large because it follows the braking late. A high ratio trusts the readings: the lag is short (0.13 s at ratio 250) but the noise gets through, and the speed error climbs (38.95 mm/s at 2500, 82.07 at 25 000; the position error rises from 3.5 to 6.07 mm). The best of the table is near ratio 40, 3.5 mm and 17 mm/s, about 0.18 s of lag. Told the wrong R (4 or 250 instead of 25) the filter is just as good as long as q1 is scaled by the same factor, and bad if it is not: q1 = 10 against R = 250, a ratio of 0.04, is 40 mm and 63 mm/s off. Check on method: the lag measure depends on the 10 mm/s threshold I chose and on a robot that stops exactly once; the optimum ratio belongs to this speed profile (a deceleration of 100 mm/s squared), and a robot that brakes harder would need a higher one; the grid of q1 and R is coarse, so 'about 40' is not a measured optimum.