Programming for Robotics with Arduino C++ 3, Challenge 2 solution (how coarse can an encoder be?) Idea: with the right motor 5 % weak and no heading hold the robot drives a long arc to about -43 degrees, which gives the odometry something large to measure. The simulator and the controller are given the same number of ticks per wheel turn (20, 90, 360 and 1440), and the odometry's final heading and position are compared with the simulator's truth (the robot starts at (0.3, 1.0) and its odometry at (0, 0), so the position error is odometry minus (truth minus start)). CODE: // x2.cpp -- challenge 2: how good is dead reckoning as the encoder gets coarser? (host build) // The robot drives with a weak right motor and NO heading hold, so it turns a long way and the odometry has something to measure. #include #include #include #include "harness.h" int main() { std::printf("odometry against the simulator's truth after the run (weak right motor, no heading hold, seed 1):\n"); std::printf(" ticks per rev mm per tick heading truth deg heading odometry deg heading error deg position error mm\n"); for (int tpr : {20, 90, 360, 1440}) { RobotParams rp; rp.gain_r = 0.95; rp.ticks_per_rev = tpr; SensorParams sp; Arena arena; SimHW world(rp, sp, arena, 1); DriveConfig cfg = default_config(); cfg.k_heading = 0.0f; cfg.ticks_per_rev = tpr; Outcome o = run_robot(world, cfg, 10000); double ex = o.odo_x - (o.x - 0.3), ey = o.odo_y - (o.y - 1.0); // the robot started at (0.3, 1.0) and its odometry at (0, 0) std::printf(" %13d %11.2f %17.2f %20.2f %+17.2f %17.1f\n", tpr, 2.0 * SIM_PI * 0.033 / tpr * 1000.0, o.th * 180.0 / SIM_PI, o.odo_th * 180.0 / SIM_PI, (o.odo_th - o.th) * 180.0 / SIM_PI, std::sqrt(ex * ex + ey * ey) * 1000.0); } return 0; } END CODE OUTPUT: odometry against the simulator's truth after the run (weak right motor, no heading hold, seed 1): ticks per rev mm per tick heading truth deg heading odometry deg heading error deg position error mm 20 10.37 -43.08 -43.56 -0.48 7.1 90 2.30 -43.08 -43.12 -0.04 0.8 360 0.58 -43.08 -43.12 -0.04 0.3 1440 0.14 -43.08 -43.07 +0.01 0.1 END OUTPUT Reading the output. The odometry's heading error is -0.48 degrees with 20 ticks a turn (10.37 mm of wheel travel per tick), -0.04 with 90 and with 360, and +0.01 with 1440; the position error is 7.1, 0.8, 0.3 and 0.1 mm over a path of about 2.5 m. Each wheel's count is rounded down, so the count can be short by up to one tick on each wheel at the end, and one tick of one wheel is a heading error of (distance per tick) / 0.15 m: by hand 3.96, 0.88, 0.22 and 0.055 degrees for the four encoders. The measured errors are inside those bounds (0.48, 0.04, 0.04 and 0.01), which is what quantisation alone predicts. The errors do not fall steadily (the 90 and 360 tick encoders give the same -0.04) because the final error depends on where the last tick of each wheel happened to fall, so a single run shows the bound, not a trend. What the millimetres per tick tell you is the resolution: with 10.37 mm per tick the robot cannot tell a 10 mm movement from none, and the control tick of 20 ms at 0.5 m/s moves 10 mm, so each tick would see about one count. Check on method: the simulated wheels never slip and the encoder never misses a tick or counts a tick twice, so this error is quantisation only. On a real robot the odometry error is dominated by what the simulator leaves out (slip, a wheel of slightly the wrong radius, a track that is not exactly 0.15 m), and these figures are a lower bound, not a forecast. One seed, one path.