Programming for Robotics with Arduino C++ 12, Challenge 2 solution (the stuck detector's thresholds) Idea: for a change threshold of 10, 20 and 40 mm and a window of 250 ms to 4 s, the supervisor is run on 100 healthy approaches to the wall (they end with a slow creep, so the robot is moving slowly while the range hardly changes) and on 100 runs in which the range sensor sticks at 2.5 m from t = 2 s. Each cell shows false alarms out of 100 healthy runs, then the mean detection delay after the sensor sticks. CODE: // x2.cpp -- challenge 2: the stuck-sensor detector's two thresholds (host build; the robot, the sensor and the fault are assumptions). // A healthy run drives at a wall and stops 0.30 m from it, so it ends with a slow creep; the fault run has the range sensor stuck at 2.5 m from t = 2 s. // For every pair of (change threshold delta, window) the supervisor is run on 100 healthy runs (how many are stopped by the stuck detector, a FALSE ALARM) // and on 100 fault runs (how long after the sensor sticks the detector fires, a DETECTION DELAY; "never" if it did not within 8 s). #include #include #include #include #include "sim.h" #include "safety.h" struct Res { bool alarm; double delay; }; static Res run(bool fault, float delta, uint32_t window, uint32_t seed) { RobotParams rp; SensorParams sp; Arena arena; SimHW sim(rp, sp, arena, seed * 2654435761u + 12345u); SafetyConfig cfg = {250, 300, delta, window, 400, 0.15f, 0.4f}; Supervisor sup(cfg); float last_range = 3.0f; Cmd cmd = {0.0f, 0.0f}, applied = {0.0f, 0.0f}; for (long k = 0; k < 2000; ++k) { // 10 s double t = k * 0.005; uint32_t now = (uint32_t)std::lround(t * 1000.0); if (k % 12 == 0) { float r = sim.range_m(); if (fault && t >= 2.0) r = 2.5f; sup.range_sample(now, r); 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.vl = cmd.vr = v; sup.command(cmd, now); } int32_t tl, tr; sim.read_ticks(tl, tr); if (k % 4 == 0) applied = sup.step(now, tl, tr); sim.drive(applied.vl, applied.vr); sim.step(0.005); if (sup.faults() & F_STUCK_RANGE) { Res r = {true, t - (fault ? 2.0 : 0.0)}; return r; } } Res r = {false, -1.0}; return r; } int main() { const float deltas[3] = {0.010f, 0.020f, 0.040f}; const uint32_t windows[5] = {250, 500, 1000, 2000, 4000}; std::printf("stuck detector: false alarms in 100 healthy runs / mean detection delay in 100 stuck-sensor runs (s)\n"); std::printf(" delta (mm) \\ window (ms)"); for (uint32_t w : windows) std::printf(" %12u", w); std::printf("\n"); for (float d : deltas) { std::printf(" %24.0f", d * 1000.0f); for (uint32_t w : windows) { int alarms = 0, det = 0; double sum = 0; for (uint32_t seed = 1; seed <= 100; ++seed) { if (run(false, d, w, seed).alarm) ++alarms; Res f = run(true, d, w, seed); if (f.alarm) { ++det; sum += f.delay; } } char buf[32]; if (det) std::snprintf(buf, sizeof buf, "%d / %.2f", alarms, sum / det); else std::snprintf(buf, sizeof buf, "%d / never", alarms); if (det && det < 100) std::snprintf(buf + std::strlen(buf), sizeof buf - std::strlen(buf), "*"); std::printf(" %12s", buf); } std::printf("\n"); } std::printf(" (* = not all 100 stuck-sensor runs were detected)\n"); return 0; } END CODE OUTPUT: stuck detector: false alarms in 100 healthy runs / mean detection delay in 100 stuck-sensor runs (s) delta (mm) \ window (ms) 250 500 1000 2000 4000 10 34 / 0.30 0 / 0.56 0 / 1.06 0 / 2.06 0 / 4.06 20 98 / 0.28 10 / 0.56 0 / 1.06 0 / 2.06 0 / 4.06 40 100 / 0.26 44 / 0.56 0 / 1.06 0 / 2.06 0 / 4.06 (* = not all 100 stuck-sensor runs were detected) END OUTPUT Reading the output. A 250 ms window is useless: 34, 98 and 100 false alarms of 100 for 10, 20 and 40 mm. At 500 ms the threshold matters: 0 false alarms at 10 mm, 10 at 20 mm and 44 at 40 mm, all with a 0.56 s detection delay. From 1 s up there are no false alarms at any threshold, and the delay is the window plus about 0.06 s (1.06, 2.06, 4.06 s). Detection was complete (100 of 100) in every cell, since no cell is marked with an asterisk. The pair I would use is 20 mm with 1 s: no false alarms in 100 healthy runs and a delay of 1.06 s, which is 0.32 m of travel at 0.3 m/s. 10 mm with 500 ms has the same zero false alarms and half the delay (0.56 s, 0.17 m) in this simulator, but the simulator's range noise is something I chose, and a 10 mm threshold sits close to the noise of a real ultrasonic sensor, so I would not trust it without measuring that sensor's noise first; 20 mm with 1 s leaves a margin. The robot of the chapter uses 1 s. The detection delay is a distance you travel blind: at 0.3 m/s, 1.06 s is 0.32 m, so the stuck detector can only guard a stopping distance that is longer than that, or the speed has to drop when the range is old (the stale-range rule). A false alarm is not free either: the supervisor latches, and the robot stops short of its goal until a person resets it. Check on method: the healthy runs end in a creep at about 0.06 m/s near 0.36 m, which is exactly where a stuck detector is weakest; a robot that never creeps would see fewer false alarms. 100 runs per cell; the noise model, the fault (a clean constant 2.5 m) and the robot are assumptions.