Programming for Robotics with Arduino C++ 10, Challenge 1 solution (the best time constant against the vibration) Idea: the tilt model of the chapter (two sine waves, a step of 0.4 rad at 30 s, an assumed gyro offset up to 0.03 rad/s and noise, an assumed accelerometer noise of 0.05 rad), with only the frequency of the 0.12 rad vibration changed, and every time constant of the list tried on 40 seeded runs for each frequency. The score is the rms tilt error over the whole 60 s run. CODE: // x1.cpp -- challenge 1: the best complementary-filter time constant against the frequency of the accelerometer's vibration (host build; the sensor models are ASSUMED). // Same tilt model as e_comp.cpp (two sine waves and a step of 0.4 rad at 30 s, gyro offset up to 0.03 rad/s, 0.01 rad/s noise, accelerometer noise 0.05 rad), 40 runs; only the vibration changes: 0.12 rad at the frequency in the first column. #include #include #include #include "sim.h" #include "est.h" static double truth(double t) { return 0.35 * std::sin(2 * SIM_PI * t / 8.0) + 0.12 * std::sin(2 * SIM_PI * t / 2.5) + (t >= 30.0 ? 0.4 : 0.0); } static double truth_rate(double t) { return 0.35 * 2 * SIM_PI / 8.0 * std::cos(2 * SIM_PI * t / 8.0) + 0.12 * 2 * SIM_PI / 2.5 * std::cos(2 * SIM_PI * t / 2.5); } static double run(double tau, double vib_hz, int seeds) { double sq = 0; long n = 0; for (int seed = 1; seed <= seeds; ++seed) { Rng rng((uint32_t)seed * 7777u + 5u); double bias = (rng.uniform() * 2 - 1) * 0.03; Complementary f((float)tau, 0.01f, (float)truth(0)); for (int i = 1; i <= 6000; ++i) { double t = i * 0.01; double rate = truth_rate(t) + bias + 0.01 * rng.normal(); double acc = truth(t) + 0.05 * rng.normal() + 0.12 * std::sin(2 * SIM_PI * vib_hz * t); double e = f.update((float)rate, (float)acc, 0.01f) - truth(t); sq += e * e; ++n; } } return std::sqrt(sq / n) * 57.2958; } int main() { const double taus[] = {0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0}; std::printf("complementary filter, rms tilt error (degrees) for each tau; the best of each row is marked *\n"); std::printf(" vibration "); for (double tau : taus) std::printf(" tau %-5.2f", tau); std::printf(" best tau\n"); for (double hz : {0.5, 2.0, 5.0, 20.0}) { double e[7]; int best = 0; for (int i = 0; i < 7; ++i) { e[i] = run(taus[i], hz, 40); if (e[i] < e[best]) best = i; } std::printf(" %5.1f Hz ", hz); for (int i = 0; i < 7; ++i) std::printf(" %7.2f%c ", e[i], i == best ? '*' : ' '); std::printf(" %.2f s\n", taus[best]); } return 0; } END CODE OUTPUT: complementary filter, rms tilt error (degrees) for each tau; the best of each row is marked * vibration tau 0.05 tau 0.10 tau 0.20 tau 0.50 tau 1.00 tau 2.00 tau 5.00 best tau 0.5 Hz 4.89 4.71 4.22 3.04 2.73* 3.59 6.54 1.00 s 2.0 Hz 4.12 3.08 2.05 1.74* 2.33 3.51 6.53 0.50 s 5.0 Hz 2.64 1.68 1.28* 1.60 2.30 3.51 6.53 0.20 s 20.0 Hz 1.23 0.99* 1.06 1.57 2.30 3.51 6.53 0.10 s END OUTPUT Reading the output. The best time constant falls as the vibration gets faster: 1.00 s at 0.5 Hz (2.73 degrees), 0.50 s at 2 Hz (1.74), 0.20 s at 5 Hz (1.28) and 0.10 s at 20 Hz (0.99). The reason is that the filter is a low-pass filter on the accelerometer: its cut-off is at about 1 / (2 pi tau) hertz, 0.16 Hz for tau = 1 s and 1.6 Hz for tau = 0.1 s, and the longest tau that still has time to forget the vibration is the best, because a longer one hands more of the job to the gyro and the gyro's offset has longer to drift the angle. The two columns at the right do not change from 5 Hz to 20 Hz (3.51 and 6.53): at tau = 2 s and 5 s the vibration is filtered out completely whatever its frequency, and what is left is the gyro's drift. The slow vibration is the hard case, and its row has the worst best-error of the four (2.73 degrees): at 0.5 Hz, a period of 2 s, the vibration is in the same band as the real 2.5 s tilt wave, so no time constant can remove the one and keep the other. The faster vibrations are almost free: at 20 Hz a tau of 0.1 s gets under a degree. A real robot's motor vibration is usually fast, which is a reason for mounting the accelerometer on soft pads and filtering it before the angle is computed: the problem becomes easier with a better sensor, not a better filter. Check on method: the score is the rms over the whole run, so the step at 30 s counts for only a small part of it; a short tau follows a real step quickly and a long one slowly (the chapter's first table: 3.12 degrees against 10.47 in the 5 s after the step), and a robot that has to follow fast tilts would choose a shorter tau than the whole-run score does. One sine wave stands for the vibration; a real one has many frequencies.