Programming for Robotics with Arduino C++ 12, Challenge 3 solution (how big must the event log be?) Idea: log every range measurement (every 60 ms), every change of the commanded speed at the supervisor's 20 ms step and every change of the fault mask, while the range sensor sticks at 2.5 m at t = 2 s and the fault latches. Then replay the same events through ring buffers of 8 to 255 entries and look at what the dump holds at the moment the fault latches. CODE: // x3.cpp -- challenge 3: how big must the event log be? (host build; the robot, the fault and the logging policy are assumptions) // The range sensor sticks at 2.5 m at t = 2 s; the supervisor (stuck window 1000 ms) latches the fault. Everything is logged as it happens: each range measurement (every 60 ms), // each change of the commanded speed at the supervisor's 20 ms step, and each change of the fault mask. When the fault latches, the ring buffer is dumped. // For logs of 8 ... 255 events: how much history does the dump hold, does it reach back past the moment the sensor stuck, and what does it cost in RAM (7 bytes an event on the AVR)? #include #include #include #include #include "sim.h" #include "safety.h" #include "ringlog.h" struct Ev { uint32_t t; uint8_t code; int16_t value; }; template static void replay(const std::vector& evs, uint32_t latch_ms, uint32_t onset_ms) { RingLog log; for (const Ev& e : evs) if (e.t <= latch_ms) log.add(e.t, e.code, e.value); const LogEvent& oldest = log.at(0); double span = (latch_ms - oldest.t_ms) / 1000.0; double before_onset = ((double)onset_ms - oldest.t_ms) / 1000.0; bool sees_healthy = oldest.t_ms < onset_ms; std::printf(" %7d %9d %20.2f %26.2f %s\n", (int)N, 7 * (int)N, span, before_onset, sees_healthy ? "yes" : "no"); } int main() { RobotParams rp; SensorParams sp; Arena arena; SimHW sim(rp, sp, arena, 1u * 2654435761u + 12345u); SafetyConfig cfg = {250, 300, 0.02f, 1000, 400, 0.15f, 0.4f}; Supervisor sup(cfg); std::vector evs; float last_range = 3.0f; Cmd cmd = {0.0f, 0.0f}, applied = {0.0f, 0.0f}; float prev_v = -1.0f; uint8_t prev_mask = 0; uint32_t latch_ms = 0; for (long k = 0; k < 2000 && !latch_ms; ++k) { 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 (t >= 2.0) r = 2.5f; sup.range_sample(now, r); evs.push_back({now, 1, (int16_t)std::lround(r * 1000.0f)}); 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); if (std::fabs(applied.vl - prev_v) > 0.005f) { evs.push_back({now, 2, (int16_t)std::lround(applied.vl * 1000.0f)}); prev_v = applied.vl; } if (sup.faults() != prev_mask) { evs.push_back({now, 3, (int16_t)sup.faults()}); prev_mask = sup.faults(); if (!latch_ms) latch_ms = now; } } sim.drive(applied.vl, applied.vr); sim.step(0.005); } uint32_t onset = 2000; int n_before = 0, n_in = 0; for (const Ev& e : evs) { if (e.t < onset) ++n_before; else if (e.t <= latch_ms) ++n_in; } std::printf("the sensor sticks at t = 2.000 s; the fault latches at t = %.3f s; %d events were logged before the onset and %d between the onset and the latch (%.1f events per second near the end)\n", latch_ms / 1000.0, n_before, n_in, n_in / ((latch_ms - onset) / 1000.0)); std::printf(" log size RAM (bytes) history in the dump (s) reach before the onset (s; negative: starts after it) reaches past the onset?\n"); replay<8>(evs, latch_ms, onset); replay<16>(evs, latch_ms, onset); replay<32>(evs, latch_ms, onset); replay<64>(evs, latch_ms, onset); replay<128>(evs, latch_ms, onset); replay<255>(evs, latch_ms, onset); return 0; } END CODE OUTPUT: the sensor sticks at t = 2.000 s; the fault latches at t = 3.060 s; 35 events were logged before the onset and 20 between the onset and the latch (18.9 events per second near the end) log size RAM (bytes) history in the dump (s) reach before the onset (s; negative: starts after it) reaches past the onset? 8 56 0.30 -0.76 no 16 112 0.78 -0.28 no 32 224 1.74 0.68 yes 64 448 3.06 2.00 yes 128 896 3.06 2.00 yes 255 1785 3.06 2.00 yes END OUTPUT Reading the output. The fault latches at 3.060 s: 1.06 s after the sensor stuck (the 1 s stuck window plus the sampling). Before the onset 35 events were logged and between the onset and the latch 20, which is 18.9 events per second near the end. A ring of 8 events (56 bytes) holds only the last 0.30 s and a ring of 16 (112 bytes) 0.78 s: neither reaches back to the healthy readings before the sensor stuck, so the dump would show a sensor that is already stuck and nothing before it. A ring of 32 (224 bytes) holds 1.74 s and reaches 0.68 s before the onset: it shows the last healthy readings, which is what you need to see that the value froze. 64 events (448 bytes) hold the whole 3.06 s run; 128 and 255 hold no more, because the run produced only 55 events, and cost 896 and 1,785 bytes of RAM. Choice: 32 events (224 bytes) for this logging policy, which is 11 % of the Uno's 2 KB; the robot sketch of the chapter uses 24 (168 bytes) as a compromise, which (24 was not run; it lies between the rows for 16 and 32) probably does not reach back past the onset with the 1 s stuck window, so I would use 32. That is a mismatch between the sketch and the table, and it is mine: the sketch was sized before this experiment, and the sketch was only compiled, not run. The real lesson is that the size depends on the logging policy and on the detection delay: log less often (only changes) and the same ring reaches further back; make the stuck window longer and it must be bigger. Check on method: simulated events at the rates the policy produces; 7 bytes an event is the layout of RingLog on the AVR; one fault, one run.