Programming for Robotics with Arduino C++ 2, Challenge 3 solution (when does a log line stall the robot?) Idea: model the transmit buffer of the Uno's Serial as accepting 64 bytes while empty, with print() waiting for room when the line does not fit, and the wire sending one byte every 10 bit-times (a start bit, 8 data bits and a stop bit). A task prints L bytes, then does 20 ms of other work, 100 times. The wait inside print() is the number of bytes that did not fit times the time of one byte. My first version also drained the buffer during the wait a second time and made the 9600 baud, 40 byte case look half as bad as the arithmetic says (a 40 byte line every 20 ms at 9600 baud needs 41.67 ms of wire time, so the steady wait must be about 21.67 ms); the second version agrees with that. CODE: // x3.cpp -- challenge 3: a task prints a line of L characters every 20 ms. How long does Serial.print BLOCK, by baud rate? (host model of the Uno's transmit buffer) // MODEL: the Uno core accepts 64 bytes without waiting when the buffer is empty (63 in the ring buffer, 1 in the transmit register), then print() spin-waits for space. // The wire drains one byte every 10 bit-times. Nothing else runs, so this measures only the waiting inside print(). #include #include int main() { const double ms_period = 20.0; std::printf("a line of L bytes every 20 ms for 2 s (100 lines): the worst time ONE print() call blocked, and the total time lost\n"); std::printf(" baud L worst block ms total blocked ms share of real time\n"); for (unsigned baud : {9600u, 57600u, 115200u}) { for (unsigned L : {20u, 40u, 64u, 100u, 200u}) { double byte_ms = 10.0 / baud * 1000.0; double buffered = 0.0; // bytes waiting (fractional: the wire drains continuously) double worst = 0, total = 0; for (int line = 0; line < 100; ++line) { double block = 0; double over = buffered + L - 64.0; // bytes that do not fit and must wait for the wire if (over > 0) { block = over * byte_ms; buffered = 64.0; } else buffered += L; worst = block > worst ? block : worst; total += block; double elapsed = ms_period; // the 20 ms of other work after print() returns; the drain DURING the wait already freed the space above buffered -= elapsed / byte_ms; if (buffered < 0) buffered = 0; } std::printf(" %6u %3u %14.2f %16.1f %6.1f %%\n", baud, L, worst, total, total / (2000.0 + total) * 100.0); } } return 0; } END CODE OUTPUT: a line of L bytes every 20 ms for 2 s (100 lines): the worst time ONE print() call blocked, and the total time lost baud L worst block ms total blocked ms share of real time 9600 20 0.83 36.7 1.8 % 9600 40 21.67 2120.0 51.5 % 9600 64 46.67 4620.0 69.8 % 9600 100 84.17 8370.0 80.7 % 9600 200 188.33 18786.7 90.4 % 57600 20 0.00 0.0 0.0 % 57600 40 0.00 0.0 0.0 % 57600 64 0.00 0.0 0.0 % 57600 100 6.25 625.0 23.8 % 57600 200 23.61 2361.1 54.1 % 115200 20 0.00 0.0 0.0 % 115200 40 0.00 0.0 0.0 % 115200 64 0.00 0.0 0.0 % 115200 100 3.12 312.5 13.5 % 115200 200 11.81 1180.6 37.1 % END OUTPUT Reading the output. Two different limits show up. (1) Throughput: at 9600 baud the wire carries 19.2 bytes per 20 ms, so a 40 byte line makes every print() wait 21.67 ms (51.5 % of real time) and a 200 byte line waits 188.33 ms; even 20 bytes waits a little (0.83 ms at worst, 1.8 % of real time) because 20 bytes need 20.83 ms to send. (2) Buffer size: at 57600 and 115200 baud the wire can carry 115 and 230 bytes per period, so throughput is no problem, but a single print() of more than 64 bytes must wait for the excess whatever the baud rate: 100 bytes waits 6.25 ms at 57600 and 3.12 ms at 115200, every time. Lines of up to 64 bytes never wait at those two rates. Check on method: this is a model of the buffer, built from my reading of the core's source (a 64 byte buffer and a write() that waits for space); no board was used, and the model has no interrupts, no other serial traffic and a perfectly steady 20 ms of other work. It also treats the byte in the transmit register as part of the 64. The design lesson does not depend on those details: keep a status line short, send it at a high baud rate, or send a few bytes per pass, and never let a control task wait on Serial.