Programming for Robotics with Arduino C++ 2, Challenge 1 solution (how fast must the echo be polled?) Idea: the Echo state machine finds the width of the echo pulse from the poll that sees it rise to the poll that sees it fall, so each edge is seen up to one poll late and the width can be wrong by up to one poll interval. One microsecond of echo is 0.1715 mm of distance (343 m/s halved), so 10 mm allows about 58 microseconds. The program tests that with the real state machine against a fake sensor, over 67 distances and 8 phases of the poll (where the polls fall relative to the sensor's pulse), because a single phase could land luckily. CODE: // x1.cpp -- challenge 1: how often must the echo state machine be polled to keep the distance error under 10 mm? (host build, fake clock and fake sensor) #include #include #include #include "echo.h" #include "conv.h" struct FakeSensor { float d_m; uint32_t t_fall; bool prev, armed; explicit FakeSensor(float d) : d_m(d), t_fall(0), prev(false), armed(false) {} bool echo(uint32_t now, bool trig) { if (prev && !trig) { t_fall = now; armed = true; } prev = trig; if (!armed || d_m > 4.0f) return false; return now >= t_fall && now < t_fall + m_to_echo_us(d_m); } }; static float measure(float d, uint32_t poll_us, uint32_t phase_us) { // phase_us: where the polls fall relative to the sensor, 0..poll_us-1 Echo e; FakeSensor s(d); bool trig = false; uint32_t t = 1000 + phase_us; e.start(); for (;;) { Echo::Status st = e.update(t, s.echo(t, trig), trig); if (st != Echo::BUSY) return st == Echo::READY ? echo_us_to_m(e.width_us()) : -1.0f; t += poll_us; } } int main() { std::printf("worst error over 67 distances (0.10..3.40 m) and 8 phases of the poll, by poll interval:\n"); std::printf(" poll every worst error mm under 10 mm?\n"); for (uint32_t poll : {20u, 40u, 50u, 58u, 60u, 80u, 200u}) { double worst = 0; for (int i = 0; i <= 66; ++i) for (uint32_t ph = 0; ph < 8; ++ph) { float d = 0.10f + 0.05f * i; double err = std::fabs(measure(d, poll, ph * poll / 8) - d) * 1000.0; if (err > worst) worst = err; } std::printf(" %6u us %13.1f %s\n", (unsigned)poll, worst, worst < 10.0 ? "yes" : "no"); } return 0; } END CODE OUTPUT: worst error over 67 distances (0.10..3.40 m) and 8 phases of the poll, by poll interval: poll every worst error mm under 10 mm? 20 us 3.3 yes 40 us 6.7 yes 50 us 8.4 yes 58 us 9.8 yes 60 us 10.1 no 80 us 13.5 no 200 us 34.1 no END OUTPUT Reading the output. The worst error grows in step with the poll interval: 3.3 mm at 20 us, 6.7 at 40, 8.4 at 50, 9.8 at 58 us (just inside the limit), 10.1 at 60 (just outside), 13.5 at 80 and 34.1 at 200. The arithmetic said 10 / 0.1715 = 58.3 us, and the experiment agrees to the step. So for 10 mm the board's loop has to look at the echo pin at least every 58 microseconds. Check on method: this is a fake sensor with a clean pulse and a clock with 1 microsecond resolution. The Uno's micros() counts in steps of 4 microseconds (its core multiplies the timer count by 4), which alone is 0.69 mm and has to be added, and a real sensor has noise and a start-up delay that this model does not. Whether the real loop() can pass the echo pin every 58 us was NOT measured: a pass that includes a scheduler run and an I2C read can take far longer, and then the polling error is larger than the table says. That is the case for a hardware timer capture or a pin-change interrupt.