Programming for Robotics with Arduino C++ 9, Challenge 3 solution (lookahead against speed) Idea: the raw A* path of the chapter, the simulator with 0.1 s of motor lag, an assumed 1 cm and 1 degree of localisation error, 20 runs for each of 16 combinations of speed and lookahead. Collisions are runs in which the robot's centre came closer than 75 mm to an obstacle. CODE: // x3.cpp -- Challenge 3: how large a lookahead can pure pursuit afford at each speed? (host build) // The raw A* path of e_pursuit.cpp (inflated map, 52 steps), the chapter-3 simulator (0.1 s motor lag), assumed 1 cm / 1 degree localisation error, 20 seeds per cell of the table. // Speed v in 0.15, 0.25, 0.35, 0.50 m/s against lookahead L in 0.10, 0.20, 0.30, 0.40 m. Each cell: runs with a collision (the centre closer than 75 mm to an obstacle), in %, and the time to reach the goal (s). #include #include #include #include "world.h" static double clearance(double x, double y) { double best = std::fmin(std::fmin(x, 3.0 - x), std::fmin(y, 2.0 - y)); for (const Box& b : BOXES) { double dx = std::fmax(std::fmax(b.x0 - x, 0.0), x - b.x1), dy = std::fmax(std::fmax(b.y0 - y, 0.0), y - b.y1); best = std::fmin(best, std::hypot(dx, dy)); } return best; } int main() { World truth; truth_grid(truth); World inf; inflate(truth, inf, 1); static AStar<30, 20, 300> as; uint16_t cells[600]; int16_t n = as.plan(inf, 3, 3, 27, 17, cells, 600); Pt path[64]; uint16_t m = (uint16_t)(n + 1); for (uint16_t i = 0; i < m; ++i) { path[i].x = ((cells[i] % 30) + 0.5f) * 0.1f; path[i].y = ((cells[i] / 30) + 0.5f) * 0.1f; } std::printf("pure pursuit, raw grid path of %d steps: collisions (%% of 20 runs) and time to the goal (s)\n", n); std::printf(" speed (m/s) L = 0.10 m L = 0.20 m L = 0.30 m L = 0.40 m\n"); for (float v : {0.15f, 0.25f, 0.35f, 0.50f}) { std::printf(" %11.2f", v); for (float L : {0.10f, 0.20f, 0.30f, 0.40f}) { int crash = 0, reached = 0; double tt = 0; for (int seed = 1; seed <= 20; ++seed) { Rng rng((uint32_t)seed * 31337u); RobotParams rp; rp.tau = 0.1; SimHW w(rp, SensorParams(), Arena(), (uint32_t)seed); w.x = path[0].x; w.y = path[0].y; w.th = 0.0; PurePursuit pp(L, v, 0.15f, 0.06f); bool bad = false; int tick = 0; for (; tick < 3000 && !pp.done(); ++tick) { Cmd c = pp.update((float)(w.x + 0.01 * rng.normal()), (float)(w.y + 0.01 * rng.normal()), (float)(w.th + 0.01745 * rng.normal()), path, (uint8_t)m); w.drive(c.vl, c.vr); for (int k = 0; k < 20; ++k) w.step(0.001); if (clearance(w.x, w.y) < 0.075) bad = true; } if (bad) ++crash; if (pp.done()) { ++reached; tt += tick * 0.02; } } std::printf(" %5.0f %% %6.1f s ", 100.0 * crash / 20, reached ? tt / reached : 0.0); } std::printf("\n"); } return 0; } END CODE OUTPUT: pure pursuit, raw grid path of 52 steps: collisions (% of 20 runs) and time to the goal (s) speed (m/s) L = 0.10 m L = 0.20 m L = 0.30 m L = 0.40 m 0.15 0 % 31.8 s 0 % 30.3 s 10 % 29.3 s 100 % 27.9 s 0.25 0 % 19.6 s 0 % 18.6 s 0 % 18.0 s 100 % 17.2 s 0.35 0 % 15.4 s 0 % 13.7 s 0 % 13.2 s 100 % 12.7 s 0.50 0 % 13.5 s 0 % 12.2 s 100 % 12.3 s 100 % 12.0 s END OUTPUT Reading the output. With a lookahead of 0.10 or 0.20 m there are no collisions at any speed up to 0.50 m/s. With 0.30 m the result depends on speed and is marginal: none at 0.25 and 0.35 m/s, 10 % at 0.15 m/s, 100 % at 0.50 m/s; with 0.40 m every run collides at every speed. So the largest safe lookahead is 0.20 m at 0.15 and at 0.50 m/s, and 0.30 m at 0.25 and 0.35 m/s, and the safe range is about 0.2 to 0.3 m, whatever the speed. The reason is geometry: the robot aims at a point a lookahead ahead and so cuts every corner by about that much, and the planner's inflation leaves about 0.1 m of clearance; the chapter's run at 0.25 m/s had a closest approach of 83 mm against the 75 mm limit at L = 0.30, which is why 0.3 m is on the edge and a few percent of noise tips it either way (the 10 % at 0.15 m/s). Speed enters through the lag of the wheels: at 0.50 m/s the motor lag (0.1 s) is 5 cm of travel before the turn starts, which is enough to turn the marginal 0.30 m into a collision in every run. The time falls as the lookahead grows: at 0.25 m/s from 19.6 s (L = 0.10) to 18.6, 18.0 and 17.2 s (L = 0.40), 12 % in all, but the last of these collides; so the lookahead buys little time, and the safe choice is the smallest that is still smooth. Faster is much more effective than a longer lookahead: 13.5 s at 0.50 m/s with L = 0.10. Check on method: 20 runs per cell, four lookaheads only (0.25 and 0.35 m were not tried, so the edge is somewhere between 0.2 and 0.4 m), one world, one path; the pose error is random noise, not the drift of a real encoder. The robot's radius of 75 mm is the number used throughout the chapter.