Programming for Robotics with Arduino C++ 7, Challenge 3 solution (how wide a calibration sweep) Idea: the chapter's line follower (0.25 m/s, kp 0.05 rad/s per mm) on the circular line, 30 s, 20 seeds. Before each run the array is moved sideways by -W..+W mm in 1 mm steps and LineCal learns each sensor's minimum and maximum; W = 0 means no sweep. Scored: the TRUE rms distance of the robot's centre from the line, the number of times the line was lost, laps, and the share of runs ending in FAULT. CODE: // x3.cpp -- Challenge 3: how wide must the calibration sweep be? (host build) // The line follower of the chapter (v 0.25 m/s, k_w 0.05 rad/s per mm), 30 s on the circular line, 20 seeds. Before the run the five-sensor array is swept sideways across the line // by +-W mm (W = 0 means no calibration at all) so that LineCal learns each sensor's minimum and maximum. Only W changes. #include #include #include #include "sim.h" #include "fsm.h" static void row(int W, int seeds) { double sq = 0, lost = 0, laps = 0, faults = 0; long n = 0; for (int seed = 1; seed <= seeds; ++seed) { RobotParams rp; rp.tau = 0.1; SimHW w(rp, SensorParams(), Arena(), seed); w.line.on = true; LineConfig c = {0.25f, 0.05f, 0.5f, 1.5f, 0.15f, 3000}; LineFollower f(c); w.x = w.line.cx + w.line.R; w.y = w.line.cy; w.th = SIM_PI / 2; int16_t raw[5]; if (W > 0) for (int i = -W; i <= W; ++i) { double x0 = w.x; w.x = x0 + i * 0.001; w.line_read(raw); f.cal().learn(raw); w.x = x0; } double angle = 0, prev_a = std::atan2(w.y - w.line.cy, w.x - w.line.cx); for (int tick = 0; tick < 1500; ++tick) { w.line_read(raw); Cmd cmd = f.update(raw, tick * 20); w.drive(cmd.vl, cmd.vr); for (int k = 0; k < 20; ++k) w.step(0.001); double e = w.line_error(); sq += e * e; ++n; double a = std::atan2(w.y - w.line.cy, w.x - w.line.cx); angle += std::remainder(a - prev_a, 2 * SIM_PI); prev_a = a; } lost += f.lost_events(); laps += angle / (2 * SIM_PI); if (f.state() == LineState::FAULT) faults += 1; } std::printf(" %9d %26.1f %23.2f %5.2f %16.0f\n", W, std::sqrt(sq / n) * 1000, lost / seeds, laps / seeds, 100.0 * faults / seeds); } int main() { std::printf("line follower, 30 s, mean over 20 runs\n"); std::printf(" sweep +-mm rms distance from line (mm) times the line was lost laps runs in FAULT (%%)\n"); for (int W : {0, 10, 20, 30, 40, 50, 70}) row(W, 20); return 0; } END CODE OUTPUT: line follower, 30 s, mean over 20 runs sweep +-mm rms distance from line (mm) times the line was lost laps runs in FAULT (%) 0 0.0 1.00 0.00 100 10 8.8 0.00 1.74 0 20 13.6 0.00 1.75 0 30 1.6 0.00 1.79 0 40 1.2 0.00 1.79 0 50 1.2 0.00 1.79 0 70 1.2 0.00 1.79 0 END OUTPUT Reading the output. With no calibration every sensor's range is zero, LineCal.scaled() returns 0 for all five, the line position's denominator is below its threshold, and the follower never sees the line: it goes to SEARCH at the first step and to FAULT after the 3 s time-out in every run. The rms distance is 0.0 mm and the laps are 0.00: the robot spins on the spot, centred on the line, so the number that looks perfect belongs to a robot that did not move. A score on a single quantity can be best for a robot that does nothing; lost events, laps and FAULT are in the table for that reason. With sweeps of 10 and 20 mm the follower works but badly (rms 8.8 and 13.6 mm, 1.74 laps): the sensors that sit 20 and 40 mm off the centre never saw the line in the middle of their response, so their learned maxima are too low and the scaling is wrong. At 30 mm it is 1.6 mm, and at 40 mm and more 1.2 mm, the same as the 70 mm of the chapter. The width at which it stops improving is 40 mm because the outermost sensors are 40 mm off the centre: the array has to be swept far enough that every sensor passes over the middle of the line, which means at least the largest offset of any sensor from the centre. Check on method: a simulated array with made-up gains and offsets, a clean circle, and a sweep of the array itself sideways with the robot's heading fixed; on a real robot the sweep is done by turning the robot over the line, which moves the outer sensors on an arc, and the width needed has to be checked on the robot. The numbers of each row are the mean of 20 seeds.