Programming for Robotics with Arduino C++ 1, Challenge 3 solution (how slow can the loop be?) Idea: run the wall robot of the chapter under the scheduler at three loop periods and four gains for 10 simulated seconds each, and record how far past the 0.25 m stop line it gets. The wall is 1.037 m away on purpose: the first version of this test put it at 1.000 m, every row reported 0.0 mm, and that was a coincidence of arithmetic (0.75 m is exactly 15 steps of 50 mm at the 100 ms period, and 3 steps of 250 mm at 500 ms) and not a sign that a slow loop is safe. A distance that is not a multiple of the step shows the effect. CODE: // x3.cpp -- challenge 3: how far past the stop line does the wall robot go, for different loop periods and gains? (host build, fake clock) #include #include "scheduler.h" #include "robot.h" static void trial(uint32_t period_ms, float gain) { SimHW world(1.037f); // 1.037 m, not 1.000: a round distance would land the robot on the line by coincidence WallRobot robot(world, 0.25f, gain, 0.5f); Scheduler<3> sch; sch.add(WallRobot::sense_task, &robot, period_ms); sch.add(WallRobot::think_task, &robot, period_ms); sch.add(WallRobot::act_task, &robot, period_ms); float closest = 9.0f; for (uint32_t t = 0; t <= 10000; ++t) { sch.run(t); world.advance(0.001f); if (world.range_m() < closest) closest = world.range_m(); } float past_mm = (0.25f - closest) * 1000.0f; std::printf(" %6u %6.1f %6.1f %9.1f %s\n", (unsigned)period_ms, gain, gain * period_ms / 1000.0f, past_mm > 0.0f ? past_mm : 0.0f, past_mm > 5.0f ? "PAST THE LINE" : "ok"); } int main() { std::printf(" period ms gain 1/s gain x period mm past the stop line\n"); const uint32_t periods[] = {20, 100, 500}; const float gains[] = {1.5f, 5.0f, 20.0f, 60.0f}; for (uint32_t p : periods) for (float g : gains) trial(p, g); return 0; } END CODE OUTPUT: period ms gain 1/s gain x period mm past the stop line 20 1.5 0.0 0.0 ok 20 5.0 0.1 0.0 ok 20 20.0 0.4 0.0 ok 20 60.0 1.2 1.4 ok 100 1.5 0.2 0.0 ok 100 5.0 0.5 0.0 ok 100 20.0 2.0 13.0 PAST THE LINE 100 60.0 6.0 13.0 PAST THE LINE 500 1.5 0.8 0.0 ok 500 5.0 2.5 55.5 PAST THE LINE 500 20.0 10.0 213.0 PAST THE LINE 500 60.0 30.0 213.0 PAST THE LINE END OUTPUT Reading the output. The problem starts when gain times period passes about 1: the controller asks for a step of (gain x period) times the remaining error, and when that is more than 1 the robot steps over the line. Below it (1.5/s at any period, 5/s at 100 ms, 20/s at 20 ms) the robot stops on the line to the nearest millimetre; at gain x period of 1.2 (60/s at 20 ms) it is 1.4 mm past, at 2.0 (20/s at 100 ms) 13.0 mm, and at 2.5 (5/s at 500 ms) 55.5 mm. Once the robot is past the line it cannot come back, because think() clamps the speed at zero, so the error is permanent, not a ringing that dies away. The overshoot is limited by how far the robot goes in one period at top speed: 10, 50 and 250 mm for 20, 100 and 500 ms, and the observed worst cases (1.4, 13.0 and 213.0 mm) are inside those. Rows with the same distance (13.0 for gains 20 and 60 at 100 ms, 213.0 at 500 ms) are the saturated case: the speed command is 0.5 m/s in both, so the gain makes no further difference. Check on method: a fake clock and a perfect world that advances every millisecond; the sensor and the motor have no delay or noise, which a real robot has, so real limits would be lower than these. The same stale-command effect also grows with the time between reading the sensor and acting (section 7): a scheduler that runs the three tasks late is, to the controller, a longer period.