Programming for Robotics with Arduino C++ 8, Challenge 2 solution (how big a piece?) Idea: the real Scheduler class on a virtual clock in microseconds, with a control task (every 20 ms, 1.2 ms of work, an assumed time) and a dump task every 41 ms that costs 1042 us per character (9600 baud, 10 bit-times per character, transmit buffer full). 41 ms is not a multiple of 20 ms, so over 60 s the dump falls at every phase against the control task. The piece size is swept. CODE: // x2.cpp -- Challenge 2: how big a piece of a long serial dump can a cooperative scheduler afford? (host build; virtual clock; times ASSUMED, not measured) // A 200-character dump at 9600 baud costs 1042 us per character when the transmit buffer is full. It is sent in pieces of `chunk` characters by a task every 41 ms // (41 ms is not a multiple of the 20 ms control period, so over 60 s the pieces land at every phase against the control task, as in e_sched.cpp). // Reported: the control task's worst lateness, the time the whole dump takes (ceil(200 / chunk) pieces x 41 ms) and the share of processor time spent on the dump. #include #include #include #include "scheduler.h" static uint32_t g_now = 0; struct Ctx { uint32_t cost_us; }; static void run_task(void* c, uint32_t) { g_now += static_cast(c)->cost_us; } int main() { std::printf("200 characters at 9600 baud, sent in pieces every 41 ms; control task every 20 ms (1.2 ms of work), 60 s of virtual time\n"); std::printf(" characters per piece worst control lateness (ms) time for the whole dump (ms) share of processor time (%%)\n"); for (int chunk : {1, 2, 3, 5, 10, 20, 50, 200}) { g_now = 0; Ctx control = {1200}, piece = {(uint32_t)(chunk * 1042)}; Scheduler<2> sch; sch.add(run_task, &control, 20000); sch.add(run_task, &piece, 41000); while (g_now < 60000000u) { sch.run(g_now); g_now += 10; } int pieces = (200 + chunk - 1) / chunk; double share = 100.0 * (chunk * 1042.0) / 41000.0; std::printf(" %20d %27.2f %28d %26.1f\n", chunk, sch.task(0).worst_late / 1000.0, pieces * 41, share); } return 0; } END CODE OUTPUT: 200 characters at 9600 baud, sent in pieces every 41 ms; control task every 20 ms (1.2 ms of work), 60 s of virtual time characters per piece worst control lateness (ms) time for the whole dump (ms) share of processor time (%) 1 0.06 8200 2.5 2 1.10 4100 5.1 3 2.14 2747 7.6 5 4.22 1640 12.7 10 9.43 820 25.4 20 19.85 410 50.8 50 53.29 164 127.1 200 209.58 41 508.3 END OUTPUT Reading the output. The control task's worst lateness is the cost of one piece minus about 1 ms (the phases are 1 ms apart): 0.06 ms for 1 character, 1.10 for 2, 2.14 for 3, 4.22 for 5, 9.43 for 10, 19.85 for 20. The largest piece that keeps the lateness under 2.5 ms is 3 characters. What it gives up: the 200-character dump takes 67 pieces of 41 ms, 2.7 s instead of one piece, and 7.6 % of the processor's time goes to the dump (3.1 ms in every 41 ms), more than the control task itself uses (6 %: 1.2 ms in 20 ms). The last two rows (50 and 200 characters) are not pieces that can keep a 41 ms period at all: they cost 52 and 208 ms, more than the period, so the share printed (127 % and 508 %) is the processor time the task is asking for, not what it gets; the dump then takes longer than the 'time' column says, which is only a lower bound for them, and the control task misses whole periods. How I would use it: send small pieces, or let the hardware do the waiting. The Arduino core's HardwareSerial has a transmit buffer, so print() returns at once while there is room in it and the characters leave in the background on an interrupt; I did not test the Uno's buffer here, and the figures above are for the case where it is full, which is the worst case. A faster baud rate has the same effect as smaller pieces: 3.5 ms per 40 characters at 115200. Check on method: the task times are assumed, the scheduler is the real class but the clock is virtual, and a real loop() also has the echo task, the interrupt overhead and the time of the ISRs, which add to the lateness.