Programming for Robotics with Arduino C++ 7, Challenge 1 solution (the wall follower's gain) Idea: the first program runs the wall-follower machine of the chapter (100 s at 0.3 m/s, 60 seeds, 2 % dropouts) for six values of kp and scores the true side error twice: over the whole run, and only in FOLLOW at least 2 s (100 control ticks) after the machine last left CORNER. The second program traces the true side error (mm) and heading (degrees, in brackets) every 0.5 s along the first wall at kp 4, once with a perfect sensor and once with the default noise and dropouts. CODE: // x1.cpp -- Challenge 1: how much of the wall follower's side error is the controller's gain, and how much is the corners? (host build) // The machine of the chapter (FOLLOW, CORNER, SEARCH, FAULT), 100 s at 0.3 m/s, 60 seeds, 2 % dropouts; only the proportional gain kp (rad/s per metre of side error) changes. // The side error (true distance to the wall minus 0.30 m) is scored twice: over the whole run, and only over the control steps in which the machine is in FOLLOW and at least 2 s after it last left CORNER. #include #include #include #include "sim.h" #include "fsm.h" static void row(float kp, int seeds) { double laps = 0, bumps = 0, sq_all = 0, sq_f = 0; long n_all = 0, n_f = 0, n_corner = 0, n_total = 0; for (int seed = 1; seed <= seeds; ++seed) { RobotParams rp; rp.tau = 0.1; SimHW w(rp, SensorParams(), Arena(), seed); w.x = 0.5; w.y = 0.3; w.th = 0.0; WallConfig c = {0.3f, 0.30f, kp, 0.40f, 0.70f, 1.5f, 0.15f, 0.8f, 0.15f, 300, 3000, 3000, true}; WallFollower f(c); Sense s = {-1.0f, -1.0f, false}; bool was = false; double angle = 0, prev_a = std::atan2(w.y - 1.0, w.x - 1.5); int since_corner = 1000; for (int tick = 0; tick < 5000; ++tick) { if (tick % 3 == 0) { s.front_m = w.range_m(); s.right_m = w.range_right_m(); } s.bump = w.bumped(); Cmd cmd = f.update(s, tick * 20); w.drive(cmd.vl, cmd.vr); for (int k = 0; k < 20; ++k) w.step(0.001); bool b = w.bumped(); if (b && !was) bumps += 1; was = b; double a = std::atan2(w.y - 1.0, w.x - 1.5); angle += std::remainder(a - prev_a, 2 * SIM_PI); prev_a = a; if (f.state() == WallState::CORNER) { since_corner = 0; ++n_corner; } else ++since_corner; ++n_total; if (tick > 250 && w.x > 0 && w.x < 3 && w.y > 0 && w.y < 2) { double d = w.ray_from(w.x + 0.075 * std::sin(w.th), w.y - 0.075 * std::cos(w.th), w.th - SIM_PI / 2); double e = std::fmin(d, 1.0) - 0.30; sq_all += e * e; ++n_all; if (f.state() == WallState::FOLLOW && since_corner > 100) { sq_f += e * e; ++n_f; } } } laps += angle / (2 * SIM_PI); } std::printf(" %5.1f %5.2f %10.2f %14.1f %24.1f %17.1f\n", kp, laps / seeds, bumps / seeds, std::sqrt(sq_all / n_all) * 1000, n_f ? std::sqrt(sq_f / n_f) * 1000 : 0.0, 100.0 * n_corner / n_total); } int main() { std::printf("wall follower, 100 s, 2 %% dropouts, mean over 60 runs\n"); std::printf(" kp laps collisions rms, whole run rms, FOLLOW only (mm) time in CORNER (%%)\n"); for (float kp : {1.0f, 2.0f, 4.0f, 8.0f, 16.0f, 32.0f}) row(kp, 60); return 0; } END CODE OUTPUT: wall follower, 100 s, 2 % dropouts, mean over 60 runs kp laps collisions rms, whole run rms, FOLLOW only (mm) time in CORNER (%) 1.0 3.73 0.00 167.1 184.2 10.9 2.0 3.73 0.00 113.2 126.6 11.3 4.0 3.52 0.00 106.3 109.4 14.0 8.0 3.10 0.00 72.3 75.6 18.9 16.0 2.96 0.00 76.8 58.7 26.6 32.0 2.87 0.00 66.1 45.5 26.7 END OUTPUT CODE: // d_wall.cpp -- diagnostic: the TRUE side error (mm) and heading (deg) every 0.5 s along the first wall, with and without sensor noise/dropouts (kp 4). #include #include #include "sim.h" #include "fsm.h" static void trace(const char* name, double sigma, double dropout) { RobotParams rp; rp.tau = 0.1; SensorParams sp; sp.sigma_m = sigma; sp.dropout = dropout; SimHW w(rp, sp, Arena(), 3); w.x = 0.5; w.y = 0.3; w.th = 0.0; WallConfig c = {0.3f, 0.30f, 4.0f, 0.40f, 0.70f, 1.5f, 0.15f, 0.8f, 0.15f, 300, 3000, 3000, true}; WallFollower f(c); Sense s = {-1.0f, -1.0f, false}; std::printf("%s\n t(s):", name); for (int tick = 0; tick < 500; ++tick) { if (tick % 3 == 0) { s.front_m = w.range_m(); s.right_m = w.range_right_m(); } s.bump = w.bumped(); Cmd cmd = f.update(s, tick * 20); w.drive(cmd.vl, cmd.vr); for (int k = 0; k < 20; ++k) w.step(0.001); if (tick % 25 == 24) { double d = w.ray_from(w.x + 0.075 * std::sin(w.th), w.y - 0.075 * std::cos(w.th), w.th - SIM_PI / 2); std::printf(" %4.0f(%+4.0f)", (d - 0.30) * 1000, w.th * 57.2958); } } std::puts(""); } int main() { trace("noise-free, no dropouts", 0.0, 0.0); trace("default noise and 2% dropouts", 0.005, 0.02); return 0; } END CODE OUTPUT: noise-free, no dropouts t(s): -65( +7) -27( +15) 21( +17) 59( +14) 80( +8) 86( -1) 79( -10) 56( -17) 14( -22) -48( -21) -106( -12) -127( +2) -92( +16) -13( +25) 82( +33) 30( +68) -27( +70) -84( +76) -115( +88) -97(+102) default noise and 2% dropouts t(s): -64( +8) -23( +15) 27( +18) 66( +15) 85( +7) 89( -2) 80( -11) 56( -18) 11( -23) -56( -22) -119( -13) -139( +3) -97( +19) -6( +28) 83( +32) 29( +69) -24( +72) -76( +77) -104( +88) -89(+100) END OUTPUT Reading the output. The sweep: laps fall from 3.73 to 2.87 as kp goes from 1 to 32 and the share of time in CORNER rises from 10.9 % to 26.7 %. The rms side error does not fall to something small at any gain: 167 mm at kp 1, 106 at kp 4 (the chapter's value), 66 at kp 32 over the whole run; in FOLLOW only it is 184, 109 and 45.5 mm. So the corners are not the main source of the error at kp 4 (106 against 109), and a larger gain helps but not by much. The trace says why. With a perfect sensor the true side error swings from -65 to +86 to -127 mm while the heading swings between -22 and +25 degrees, and the trace with default noise and dropouts is within about 12 mm of it at every point, so the sensor is not the cause. A turn rate proportional to the distance error makes the heading the integral of the error and the lateral position the integral of the heading: y'' = -v * kp * y, an oscillator with no damping term, of period 2 pi / sqrt(v kp). For v = 0.3 m/s the periods are 11.5 s (kp 1), 5.7 s (kp 4) and 2.0 s (kp 32). In the kp-4 trace the error crosses zero going up at about 1.3 s and 7.1 s, 5.8 s apart, against 5.7 s predicted; it is one seed and one trace, so the agreement to 0.1 s is partly luck. At kp 1 the period (11.5 s) is longer than a wall (the 3 m wall takes 10 s at 0.3 m/s), so the robot never completes a swing, which fits the large error at the bottom of the sweep. What the sweep does not explain: why the FOLLOW-only error falls steadily with kp while the whole-run error does not, and why the share of time in CORNER rises with kp. A guess is that a bigger swing toward the wall more often trips the front-distance test of the corner rule, but I did not test it. The cure that the theory gives is a damping term (an estimate of the heading, or of the rate of change of the side distance, in the steering law); I did not build one, so this solution does not say how much it would help. Check on method: one arena, one speed, a simulated side sensor 75 mm to the right of the centre, 60 seeds per row; the error is the TRUE distance to the wall, which the robot never sees. The 'FOLLOW only' filter drops the 2 s after each corner, which is also a choice: a different window would give different numbers.