Programming for Robotics with Arduino C++ 7, Challenge 2 solution (how long to trust the last echo) Idea: the chapter's switch avoider (safe handling), 60 s, 100 seeds, front sensor read every 60 ms (every third control tick) and held in between, with drop_limit swept over 1, 2, 3, 6, 10 and 20 at 2 % and 30 % dropouts. Scored: collisions per run, the share of the 600 arena cells visited, and the share of time not in CRUISE. CODE: // x2.cpp -- Challenge 2: how long should the avoider trust the last good front reading when echoes go missing? (host build) // Same avoider and arena as the chapter (60 s, 100 seeds, front sensor read every 60 ms, safe handling), sweeping drop_limit (readings held before "assume something is there") at 2 % and 30 % dropouts. #include #include #include #include #include "sim.h" #include "fsm.h" static void row(double dropout, int limit, int seeds) { double bumps = 0, cov = 0, spin = 0; for (int seed = 1; seed <= seeds; ++seed) { RobotParams rp; rp.tau = 0.1; SensorParams sp; sp.dropout = dropout; SimHW w(rp, sp, Arena(), seed); w.th = (seed % 8) * 0.785; AvoidConfig c = {0.3f, 0.25f, 0.2f, 2.0f, 0.15f, 400, 400, 1200, (uint8_t)limit, true}; Avoider av(c, (uint32_t)seed); Sense s = {-1.0f, -1.0f, false}; std::set cells; bool was = false; int not_cruise = 0; for (int tick = 0; tick < 3000; ++tick) { if (tick % 3 == 0) s.front_m = w.range_m(); s.bump = w.bumped(); Cmd cmd = av.update(s, tick * 20); w.drive(cmd.vl, cmd.vr); for (int k = 0; k < 20; ++k) w.step(0.001); bool b = w.bumped(); if (b && !was) bumps += 1; was = b; if (av.state() != AvoidState::CRUISE) ++not_cruise; if (w.x > 0 && w.x < 3 && w.y > 0 && w.y < 2) cells.insert((int)(w.x / 0.1) * 100 + (int)(w.y / 0.1)); } cov += cells.size() / 600.0; spin += not_cruise / 3000.0; } std::printf(" %7.0f %% %10d %18.2f %17.1f %24.1f\n", dropout * 100, limit, bumps / seeds, 100.0 * cov / seeds, 100.0 * spin / seeds); } int main() { std::printf("obstacle avoider, safe handling, 60 s, mean over 100 runs\n"); std::printf(" dropouts drop_limit collisions per run arena visited (%%) time not in CRUISE (%%)\n"); for (double dp : {0.02, 0.30}) for (int lim : {1, 2, 3, 6, 10, 20}) row(dp, lim, 100); return 0; } END CODE OUTPUT: obstacle avoider, safe handling, 60 s, mean over 100 runs dropouts drop_limit collisions per run arena visited (%) time not in CRUISE (%) 2 % 1 0.01 16.6 45.0 2 % 2 0.00 16.6 44.8 2 % 3 0.02 22.5 28.9 2 % 6 0.01 22.7 28.4 2 % 10 0.01 22.7 28.5 2 % 20 0.01 22.7 28.5 30 % 1 0.14 2.7 89.0 30 % 2 0.02 2.6 87.8 30 % 3 0.00 8.0 66.3 30 % 6 0.00 16.4 44.6 30 % 10 0.01 20.6 33.9 30 % 20 0.01 22.7 28.5 END OUTPUT Reading the output. The unit first: drop_limit counts control ticks of 20 ms, and a sensor reading arrives every third tick, so the limits are 1/3, 2/3, 1, 2, 3.3 and 6.7 sensor readings. NearCheck keeps the old reading in front_m between readings, so after ONE missing echo the value is -1 for three ticks and its drop counter reaches 3. A limit of 1 or 2 therefore calls 'near' during those ticks: any single dropout, which happens on 2 % of readings, stops the robot. That is the whole explanation of the first two rows of the 2 % group: coverage 16.6 % against 22.5 % to 22.7 %, and 45 % of the time not in CRUISE against 28 to 29 %. A limit of 3 is the smallest that survives a single dropout. At 30 % dropouts the same effect is stronger: two missing readings in a row (9 % of the time) trip a limit of 3, and coverage is only 8.0 % at 3 ticks, 16.4 % at 6, 20.6 % at 10 and 22.7 % at 20 (about the 2 % result, because seven missing readings in a row, 0.3 to the 7th, almost never happens). Collisions are 0.14, 0.02, 0.00, 0.00, 0.01 and 0.01 per run; with 100 runs per row these are a handful of events, and I read only the first row (0.14) as clearly different. The chapter's naive rule had 0.10 collisions per run at 30 % dropouts over 200 runs and a coverage of 22.4 %; a hold of 20 ticks has almost the same coverage and fewer collisions, because holding the last reading remembers that something was near, while the naive rule forgets it at the first dropout. I would not rank them on these numbers alone. Mistake behind this challenge: the chapter's first draft, my own comments and the first summary of the experiment all described drop_limit as a number of sensor readings, while the code counts control ticks; I found it because limits of 1 and 2 behaved the same way and worse than 3. What went wrong: the unit was in my head, not in the name or the test. How to avoid it: put the unit in the name (drop_limit_ticks), say how a held value is refreshed, and test the smallest settings by hand. Check on method: the obstacles are straight walls that the sensor sees almost every time, so the collision counts are tiny; with soft or angled obstacles the cost of a wrong hold would be larger.