Programming for Robotics with Arduino C++ 12, Challenge 1 solution (how long may a command live?) Idea: part A hangs the controller at 6.5 s into a run that drives at 0.30 m/s toward a wall 3 m from the start, with a supervisor whose command timeout is swept from 50 ms to 4 s, and measures how far the robot travels after the hang (50 runs per timeout). Part B has no fault but a controller that is sometimes late (5 % of its runs 150 ms late, 1 % 400 ms late) and counts how often the timeout expires a command that was only late. CODE: // x1.cpp -- challenge 1: how long may a command live? (host build; the robot, the controller's delays and the numbers are assumptions) // Part A: the controller hangs at t = 6.5 s, when the robot is about 0.75 m from the wall and still at cruise speed; the command timeout of the supervisor is swept. How far does the robot travel after the hang, and does it hit the wall? // Part B: no fault, but the controller is sometimes late (5 % of its 60 ms runs are 150 ms late, 1 % are 400 ms late: a slow task, a long interrupt): how often does the timeout expire a command that was only late, // and how long are the motors held at zero by it (per 10 s run, mean of 50 runs)? #include #include #include #include "sim.h" #include "safety.h" static void part_a(uint32_t timeout_ms, double& travelled, int& hit_runs, int runs) { travelled = 0; hit_runs = 0; for (int seed = 1; seed <= runs; ++seed) { RobotParams rp; SensorParams sp; Arena arena; sp.sigma_m = 0.005; SimHW sim(rp, sp, arena, (uint32_t)seed * 2654435761u + 12345u); ExpiringCmd cmd(timeout_ms); double x_fault = 0; bool hit = false; bool hung = false; float last_range = 3.0f; for (long k = 0; k < 2800; ++k) { double t = k * 0.005; uint32_t now = (uint32_t)std::lround(t * 1000.0); if (!hung && t >= 6.5) { hung = true; x_fault = sim.x; } if (k % 12 == 0 && !hung) { float r = sim.range_m(); if (r >= 0.0f) last_range = r; float v = last_range - 0.30f; if (v < 0.0f) v = 0.0f; if (v > 0.30f) v = 0.30f; Cmd c = {v, v}; cmd.set(c, now); } Cmd o = cmd.get(now); sim.drive(o.vl, o.vr); sim.step(0.005); if (sim.ray_distance() <= 0.0) { hit = true; break; } } travelled += (sim.x - x_fault) / runs; if (hit) ++hit_runs; } } static void part_b(uint32_t timeout_ms, double& expiries, double& zero_ms, int runs) { expiries = 0; zero_ms = 0; for (int seed = 1; seed <= runs; ++seed) { Rng rng((uint32_t)seed * 2654435761u + 99u); for (int i = 0; i < 4; ++i) rng.next(); ExpiringCmd cmd(timeout_ms); Cmd go = {0.3f, 0.3f}; uint32_t next_run = 0, last_run = 0; bool was_expired = false; for (uint32_t now = 0; now < 10000; ++now) { if (now >= next_run) { cmd.set(go, now); last_run = now; double u = rng.uniform(); uint32_t late = u < 0.01 ? 400 : (u < 0.06 ? 150 : 0); next_run = now + 60 + late; } (void)last_run; bool e = cmd.expired(now); if (e && !was_expired) expiries += 1.0 / runs; if (e) zero_ms += 1.0 / runs; was_expired = e; } } } int main() { std::printf("part A: the controller hangs at t = 6.5 s (cruise 0.30 m/s, wall at 3.0 m, 14 s run, 50 runs)\n"); std::printf(" timeout (ms) distance travelled after the hang (m) runs that hit the wall\n"); for (uint32_t to : {50u, 100u, 250u, 500u, 1000u, 2000u, 4000u}) { double d; int h; part_a(to, d, h, 50); std::printf(" %12u %37.3f %17d/50\n", to, d, h); } std::printf("\npart B: no fault, a controller that is sometimes late (5 %% of runs 150 ms late, 1 %% 400 ms late), 10 s, 50 runs\n"); std::printf(" timeout (ms) expiries per run time held at zero per run (ms)\n"); for (uint32_t to : {50u, 100u, 250u, 500u, 1000u, 2000u}) { double e, z; part_b(to, e, z, 50); std::printf(" %12u %16.2f %29.0f\n", to, e, z); } return 0; } END CODE OUTPUT: part A: the controller hangs at t = 6.5 s (cruise 0.30 m/s, wall at 3.0 m, 14 s run, 50 runs) timeout (ms) distance travelled after the hang (m) runs that hit the wall 50 0.036 0/50 100 0.054 0/50 250 0.099 0/50 500 0.174 0/50 1000 0.324 0/50 2000 0.624 0/50 4000 0.719 50/50 part B: no fault, a controller that is sometimes late (5 % of runs 150 ms late, 1 % 400 ms late), 10 s, 50 runs timeout (ms) expiries per run time held at zero per run (ms) 50 141.00 2787 100 8.00 1190 250 1.30 268 500 0.00 0 1000 0.00 0 2000 0.00 0 END OUTPUT Reading the output. Part A: the distance after the hang grows with the timeout: 0.036, 0.054, 0.099, 0.174, 0.324 and 0.624 m for 50, 100, 250, 500, 1000 and 2000 ms, and no run hits the wall. With 4000 ms the robot travels 0.719 m and all 50 runs hit the wall, so somewhere between 2 and 4 s the timeout stops being a protection at all in this geometry. Part B pulls the other way: a 50 ms timeout expires 141 times in a 10 s run and holds the motors at zero for 2.8 s of it; 100 ms expires 8 times (1.2 s at zero); 250 ms expires 1.3 times per run; from 500 ms up there are no spurious expiries with a controller that is at most 400 ms late. So the timeout is squeezed from both sides: from below by how late the controller really is (here 400 ms, so more than 400 ms), from above by the distance you can afford to coast (here about 0.17 m at 500 ms, 0.32 m at 1 s). 500 ms is the smallest value in this sweep with no spurious expiries and costs 0.17 m of travel. The lateness model is an assumption of mine; on a real robot, measure how late the loop is, and add margin. A first run of this experiment put the hang where the robot was already slowing for the wall, and then no timeout could hit the wall at all, which made the 4000 ms case look as harmless as 50 ms. What went wrong: the fault was injected where it did not matter. How to avoid it: put the fault where it does the most harm (here during cruise, 6.5 s) and include a value that is meant to fail (4000 ms) so the experiment can show a difference. Check on method: simulated time with an assumed motor lag; 50 runs per row; one wall, one speed; nothing was run on a board and no time was measured on one.