Programming for Robotics with Arduino C++ 5, Challenge 1 solution (which speed estimator?) Idea: one simulated wheel at a constant true speed, a 20 microsecond clock, and four estimators fed the same tick counts: SpeedWindow updated every 20, 50 and 100 ms, and SpeedPeriod (time between the last two ticks, with the tick timestamped when it happens). The first second is ignored; the rms of (estimate - truth) / truth is printed for each. The delay column is arithmetic, not measured: half a window for a window estimator, and about one tick spacing for the period. CODE: // x1.cpp -- Challenge 1: which speed estimator for which speed? (host build, constant true speed, ideal wheel, 20 us simulated clock) // Four estimators on the same wheel: ticks counted over windows of 20, 50 and 100 ms, and the period of the last two ticks. // For each true speed: the rms error as a percentage of the speed, and the delay the estimator adds (half a window; or at most one tick spacing for the period). #include #include #include #include "sim.h" #include "motion.h" struct Err { double sq = 0; int n = 0; void add(double rel) { sq += rel * rel; ++n; } double pct() const { return 100.0 * std::sqrt(sq / n); } }; int main() { std::printf("rms error of the speed estimate as %% of the true speed (constant speed, 3 s, the first second ignored)\n"); std::printf(" true m/s ms/tick win 20 ms win 50 ms win 100 ms period | delay (arithmetic): win 20 win 100 period (ms)\n"); for (double v : {0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.5}) { RobotParams rp; rp.tau = 0.0; rp.pwm_quantise = false; SensorParams sp; Arena arena; SimHW w(rp, sp, arena, 1); const float pt = (float)(2.0 * SIM_PI * rp.wheel_r / rp.ticks_per_rev); SpeedWindow a20(pt), a50(pt), a100(pt); SpeedPeriod per(pt, 500000); Err e20, e50, e100, ep; int32_t last = 0; w.drive((float)v, (float)v); for (uint32_t us = 20; us <= 3000000; us += 20) { w.step(20e-6); int32_t l, r; w.read_ticks(l, r); if (l != last) { per.on_tick(l > last ? 1 : -1, us); last = l; } bool late = us > 1000000; if (us % 20000 == 0) { float s = a20.update(l, us); if (late) e20.add((s - v) / v); } if (us % 50000 == 0) { float s = a50.update(l, us); if (late) e50.add((s - v) / v); } if (us % 100000 == 0) { float s = a100.update(l, us); if (late) e100.add((s - v) / v); } if (us % 20000 == 0 && late) ep.add((per.speed(us) - v) / v); } std::printf(" %8.2f %7.1f %9.1f %9.1f %10.1f %6.1f | %17.0f %8.0f %12.1f\n", v, pt / v * 1000, e20.pct(), e50.pct(), e100.pct(), ep.pct(), 10.0, 50.0, pt / v * 1000); } return 0; } END CODE OUTPUT: rms error of the speed estimate as % of the true speed (constant speed, 3 s, the first second ignored) true m/s ms/tick win 20 ms win 50 ms win 100 ms period | delay (arithmetic): win 20 win 100 period (ms) 0.01 57.6 137.4 38.1 25.0 0.0 | 10 50 57.6 0.02 28.8 66.0 25.0 14.4 0.0 | 10 50 28.8 0.05 11.5 25.3 11.0 5.3 0.0 | 10 50 11.5 0.10 5.8 14.4 5.4 2.7 0.0 | 10 50 5.8 0.20 2.9 3.4 2.7 1.3 0.0 | 10 50 2.9 0.30 1.9 4.7 0.8 0.6 0.0 | 10 50 1.9 0.50 1.2 2.8 1.1 0.5 0.9 | 10 50 1.2 END OUTPUT Reading the output. Below 0.05 m/s the period method is the only usable one: a tick is 57.6 ms apart at 0.01 m/s, so a 20 ms window is wrong by 137 % and even a 100 ms window by 25 %, while the period is exact (0.0 %) because the quantity it measures is long compared with the 20 microsecond clock. At 0.1 m/s the 100 ms window is at 2.7 % but averages over 100 ms (50 ms of delay), the 20 ms window is at 14.4 % and the period method is still at 0.0 %. At 0.5 m/s the period method is the only one with a visible clock error (0.9 %: ticks are 1.2 ms apart and the clock step is 20 microseconds, 1.7 % of that), and the 100 ms window, 0.5 %, now wins on accuracy at a delay of 50 ms; at that speed the 20 ms window is 2.8 %. What the period method gives up: a timestamp for every tick, taken inside the interrupt routine; a rule for when no tick comes (here a time-out after which the speed is reported as 0); and, at high speed, it becomes noisy because it uses only two ticks. A reasonable design for a robot that must go from creeping to full speed is the period method below some speed and a window above it, switching with a margin so the choice does not flicker. Check on method: the wheel is ideal, with uniformly spaced ticks, and a real encoder disc has uneven slots, which makes the period method noisy at every speed (each slot has its own width) and the window method not, because it averages over many slots. The 20 microsecond clock is coarser than the Uno's 4 microsecond micros(), so the period method's error at 0.5 m/s is overstated. Not measured on a board.