Programming for Robotics with Arduino C++ 3, Challenge 1 solution (filter at the seam) Idea: the one-way controller ratchets toward the most extreme noisy reading, so a smoother reading removes the ratchet. Instead of editing drive.h, a wrapper class with the same four hardware functions sits between the simulator and the robot and returns the average of the last N valid ranges (a dropout is passed on as a dropout). The robot code does not know the wrapper is there. 200 seeds per N, at 20 mm of noise and 5 % dropouts, ideal motors. CODE: // x1.cpp -- challenge 1: remove the noise bias of the one-way controller WITHOUT letting the robot reverse, by filtering at the hardware seam (host build) // A wrapper around the simulator averages the last N valid ranges before drive.h ever sees them. drive.h is not touched. #include #include #include #include "scheduler.h" #include "drive.h" #include "sim.h" #include "harness.h" template class AveragedRange { // the same four functions as the hardware, plus a moving average on range_m() public: AveragedRange(HW& hw, int n) : hw_(hw), n_(n), count_(0), next_(0) { for (int i = 0; i < 16; ++i) v_[i] = 0; } float range_m() { float m = hw_.range_m(); if (m < 0.0f) return m; // a dropout is passed on as a dropout v_[next_] = m; next_ = (next_ + 1) % n_; if (count_ < n_) ++count_; float sum = 0; for (int i = 0; i < count_; ++i) sum += v_[i]; return sum / count_; } void read_ticks(int32_t& l, int32_t& r) { hw_.read_ticks(l, r); } void drive(float vl, float vr) { hw_.drive(vl, vr); } bool bumped() { return hw_.bumped(); } private: HW& hw_; int n_, count_, next_; float v_[16]; }; int main() { std::printf("sigma 20 mm, dropouts 5 %%, ideal motors, 200 seeds each; error = true distance to the wall at the end minus 250 mm\n"); std::printf(" readings averaged mean error mm sd mm worst (most negative) mm mean time to settle s\n"); for (int n : {1, 3, 5, 9}) { double sum = 0, sum2 = 0, worst = 1e9, settle_sum = 0; int ok = 0; for (int seed = 1; seed <= 200; ++seed) { RobotParams rp; SensorParams sp; sp.sigma_m = 0.020; sp.dropout = 0.05; Arena arena; SimHW world(rp, sp, arena, (uint32_t)seed); AveragedRange seam(world, n); DriveRobot > robot(seam, default_config()); Scheduler<3> sch; sch.add(DriveRobot >::sense_task, &robot, 20); sch.add(DriveRobot >::think_task, &robot, 20); sch.add(DriveRobot >::act_task, &robot, 20); double settle = 0; for (uint32_t t = 0; t < 12000; ++t) { sch.run(t); world.step(0.001); double v = (world.wheel_l() + world.wheel_r()) / 2; if (v > 0.005) settle = t / 1000.0; } if (robot.fault() != FAULT_NONE) continue; double err = (world.ray_distance() - 0.25) * 1000.0; sum += err; sum2 += err * err; ++ok; if (err < worst) worst = err; settle_sum += settle; } double mean = sum / ok, sd = std::sqrt(sum2 / ok - mean * mean); std::printf(" %17d %+13.1f %5.1f %+24.1f %21.2f\n", n, mean, sd, worst, settle_sum / ok); } return 0; } END CODE OUTPUT: sigma 20 mm, dropouts 5 %, ideal motors, 200 seeds each; error = true distance to the wall at the end minus 250 mm readings averaged mean error mm sd mm worst (most negative) mm mean time to settle s 1 -17.4 1.8 -22.8 11.37 3 -5.9 1.7 -11.1 10.96 5 -2.5 1.8 -8.0 10.42 9 -0.1 1.7 -5.6 9.70 END OUTPUT Reading the output. The mean error goes from -17.4 mm with no averaging to -5.9 (N = 3), -2.5 (N = 5) and -0.1 mm (N = 9), and the worst run from -22.8 to -5.6 mm, without the robot ever reversing. The spread of the final position, 1.7 to 1.8 mm, does not change: averaging removed the bias, which came from the ratchet, and left the scatter, which comes from the last few readings before the robot stops. The mean time to settle falls from 11.37 s to 9.70 s, but that column is limited by the 12 s length of the run (a robot still creeping when the run ends counts as settling at the end) and is only indicative. Check on method: the noise is Gaussian at an assumed size. An average of N readings is also a delay of about (N - 1) / 2 control ticks: at N = 9 the range the robot acts on is on average four ticks (80 ms) old. At the gentle approach speed of this controller that cost nothing visible here; a faster robot, or a sharper controller, would turn that delay into overshoot, which was not tested. Programming for Robotics with Arduino C++ 4 builds the proper filters and counts their lag.