Programming for Robotics with Arduino C++ 8, Challenge 3 solution (how long a watchdog timeout?) Idea: the heartbeat supervisor of e_wdt.cpp on a 1 ms clock: the control task (every 20 ms) and the echo task (every 60 ms) check in, and the model watchdog is fed only when both have. A blocking task stops loop() for B ms every 497 ms (so it falls at every phase against the heartbeats). For each timeout offered by the AVR watchdog and each B, the program says whether a healthy robot gets reset in 60 s, and separately how long it takes to catch a real fault (the control task dying at 1 s). CODE: // x3.cpp -- Challenge 3: how long a watchdog timeout? Too short: a long but harmless task makes the watchdog reset a healthy robot. Too long: a real hang goes unnoticed. (host model, 1 ms clock) // Supervisor policy of e_wdt.cpp (feed only when the control task, every 20 ms, and the echo task, every 60 ms, have both checked in). A blocking task (a slow serial dump) stops loop() for B ms // every 497 ms. Over 60 s: does a healthy robot get reset (FALSE RESET)? Then a real fault (the control task dies at 1 s): how long until the reset? // The AVR watchdog offers 15, 30, 60, 120, 250, 500 ms and 1, 2, 4, 8 s. #include #include #include "wdt.h" // returns the time of the first reset in ms (or -1); `block_ms` stops loop() for that long every 497 ms; `die` kills the control task at t = 1000 static long run(uint32_t timeout, uint32_t block_ms, bool die, uint32_t horizon) { WdtModel wdt(timeout); Heartbeats<2> hb; uint32_t block_until = 0; for (uint32_t t = 1; t <= horizon; ++t) { if (block_ms && t % 497 == 0) block_until = t + block_ms; bool alive = t >= block_until; if (alive) { if (t % 20 == 0 && !(die && t >= 1000)) hb.checkin(0); if (t % 60 == 0) hb.checkin(1); if (hb.all_alive()) { wdt.feed(t); hb.clear(); } } if (wdt.poll(t)) return (long)t; } return -1; } int main() { const uint32_t timeouts[] = {15, 30, 60, 120, 250, 500, 1000, 2000, 4000, 8000}; const uint32_t blocks[] = {0, 4, 17, 42, 208}; std::printf("watchdog timeout against a loop() that is blocked for B ms every 497 ms (supervisor policy), 60 s\n"); std::printf(" timeout (ms) B = 0 B = 4 B = 17 B = 42 B = 208 | time from a real fault to the reset (ms)\n"); for (uint32_t to : timeouts) { std::printf(" %12u", (unsigned)to); for (uint32_t b : blocks) std::printf(" %-7s", run(to, b, false, 60000) < 0 ? "ok" : "RESET"); long r = run(to, 0, true, 60000); if (r < 0) std::printf(" | never\n"); else if (r < 1000) std::printf(" | healthy robot reset at t = %ld ms\n", r); else std::printf(" | %ld\n", r - 1000); } return 0; } END CODE OUTPUT: watchdog timeout against a loop() that is blocked for B ms every 497 ms (supervisor policy), 60 s timeout (ms) B = 0 B = 4 B = 17 B = 42 B = 208 | time from a real fault to the reset (ms) 15 RESET RESET RESET RESET RESET | healthy robot reset at t = 15 ms 30 RESET RESET RESET RESET RESET | healthy robot reset at t = 30 ms 60 ok RESET RESET RESET RESET | 80 120 ok ok ok ok RESET | 140 250 ok ok ok ok RESET | 270 500 ok ok ok ok ok | 520 1000 ok ok ok ok ok | 1020 2000 ok ok ok ok ok | 2020 4000 ok ok ok ok ok | 4020 8000 ok ok ok ok ok | 8020 END OUTPUT Reading the output. The rule that fits every row is: the shortest safe timeout is a little more than B plus the longest heartbeat period, here 60 ms (the echo task). B = 4 ms needs more than 64 ms, so 60 ms resets and 120 ms is fine; B = 17 needs more than 77 (60 resets, 120 is fine); B = 42 needs more than 102 (120 is fine); B = 208 needs more than 268 (250 resets, 500 is fine). With no blocking at all, 60 ms just works, but the margin is zero: the supervisor can feed only once every 60 ms because the slowest heartbeat comes that often. Timeouts of 15 and 30 ms, shorter than that period, reset a perfectly healthy robot at t = 15 and 30 ms, before the first full set of check-ins. The other side is detection time: a real fault is caught after the timeout plus up to one heartbeat period: 80 ms at a 60 ms timeout, 140 at 120, 270 at 250, 520 at 500, 1020 at 1 s. At the robot's assumed top speed of 0.5 m/s, 520 ms is 26 cm travelled on the last command, and 140 ms is 7 cm. So the trade: with a 208 ms blocking task the smallest safe timeout is 500 ms; remove the blocking (section 5's pieces) and 120 ms becomes safe and a fault is caught almost four times sooner. Fixing the blocking is worth more than choosing the timeout. Check on method: it is a model with chosen periods; the chip's watchdog timeouts are nominal figures (its oscillator varies with temperature and supply voltage, which I did not measure), so margins of a few percent are not trustworthy: leave at least a factor of 1.5 over the rule.