Gyros drift. Everyone knows that and there are lots of ways to counter the drift. The autonomous flying vehicle folk will tell you that Kalman filters are the way to go – if you have time for all that maths. A complementary filter is simpler but will it help with steering a micromouse?

The idea of a complementary filter is fairly simple. You may have two kinds of sensor telling you the same basic information. Each has different characteristics. By combining the two, you can get the best of both worlds. For a flying vehicle, you can use a gyro and an accelerometer to control pitch and roll.

The gyro detects angular velocity. It is good at that and by integrating the output, you get roll angle. There will be some noise in the gyro signal but integration is great for removing noise. Unfortunately, the gyro will always have some small offset however hard you calibrate it. Worse, the offset will change with time and temperature. Better ADC resolution will not help because any conversion to digital will have a small quantisation error. When you integrate the gyro signal to give an angle, these offset and quantisation errors all get added up and the error in the angle will grow over time. This is what is meant by gyro drift. The short-term rate information (angular velocity) is good and reliable. The long-term sum is poor.

An accelerometer tells you which way gravity points. It is characteristically noisy and, at any given instant, it will probably indicate the wrong direction. However, in contrast to the gyro, it does not drift. How could it, the centre of the Earth does not move much in relation to the sensor. So, if you average the accelerometer output over a long period it gives a very accurate result.

Sensor fusion
These two sensors complement each other perfectly. Starting from zero, the gyro gives accurate angles over short periods of time, the accelerometer gives accurate angles over longer periods. What is needed is a way to combine these results into a single answer that is always a good estimate of the real answer.

Complementary Filters
The complementary filter does exactly what is needed. It provides a high-pass filter for the gyro and a low-pass filter for the accelerometer and adds the two answers together. The result is remarkably good for very little investment in effort. Rather than explain the theory here, try the links at the end of the post.

Use in a micromouse
A micromouse does not run long enough for gyro drift to become a real problem normally. During exploration, though, it can easily build an error of several degrees. The encoders on the wheels are noisy in as much as they sense rotation only with poor resolution. over time though, so long as there is little differential slip, the give a good indication of angle.

It looks like the gyro and the encoders together might make a good complementary pair for sensing yaw angle.

That, as they say, turns out not to be the case. At least, not on my mouse. You may be able to make it work where I failed.

Practical Considerations
There are two related problems. one is that the wheels are unlikely to be the same size so that, even when driving straight, the left and right encoders will give different numbers of counts. Over a few calibration runs, I determined that the right wheel put out about 1.2% more counts. The about is significant. Because of slip, that may vary between 1.15% and 1.3%. It does not sound like much but it amounts to a range of +/- 1deg per metre on top of a basic error of about 7.5 deg/metre. That is pretty poor.

Further, the difference in wheel counts is very much affected by slip. With the speeds and accelerations used by competition mice, slip is a big issue.

Sample Results
here are some sample results. The mouse was set to do a modest straight of 10 squares with top speed 2000mm/s and acceleration 8000mm/s/s. It did this very well. The distance was accurate and the mouse track – at least visually – was very good. It finished within 1mm of its intended lateral offset.

In the chart, the blue line is the complementary filter output calculated in Excel. For this ideal output, the ratio of left counts to right counts was adjusted until the finish angle came to be zero. That was a ratio of 1.01008. That is all very well for the finish angle but during the fast part of the run there is an error of nearly 2 degrees. This example is not typical but chosen to illustrate the problems. Other runs gave much better results. The error at speed is, I think a result of differential slip during acceleration and braking. Probably the right wheels were more (or less) dirty than the left wheels.

The red line is the mouse angle calculated by the on-board complementary filter. It was using a right:left ratio of 1.0125 obtained as an average of a couple of previous runs.

The green line is the mouse angle calculated by integrating the gyro output. There is some slight drift and the mouse angle ends up about 0.17 degrees to the left. This is wrong as it would have resulted in an error of 5mm in the mouse offset when the actual observed error at the end of the run was too small to measure accurately. Even so, it is way better than would be managed by using a complementary filter in this case.


The long and short of it is this: when everything is working perfectly, and at low speeds, the gyro and wheel encoders can be combined to give very useful improvements in estimation of a robot’s heading. When you start to race them, the situation gets bad quickly. If you can improve on this, do please add a comment.

All in all, I would rather be a little wrong most of the time than badly wrong at random.


This Post Has 2 Comments

  1. Rok

    For compensation of gyro yaw drift digital compass (magnetometer) is often used in quadcopters. It provides same kind of information as accelerometer – absolute but noisy.
    There are already solutions with all three sensor built in, the so called 9 dof sensors. mpu9150 from invensense or bno055 from bosch. And they also have software (sensor fusion) working, so they say it provides absolute information. I have only read about them, not any practical experience with them – don’t even know if or where to obtain these chips.

    Magnetometer that is often used in quadcopter society that is added to mpu6050 is HMC5883L from honeywell. Should be available from farnell, mauser etc.
    Well the general problem of magnetometer is external magnetic noise coming from motor, but with good filtering it might be a useful solution.
    Anyway you could have all 3 sensors combined 🙂 gyro, encoder and magnetometer.

  2. Peter Harrison

    I have an MPU6050 to play with but have not yet mounted it on a mouse or taken the trouble to see if the gyro is suitable for micromouse use. The proximity of the motors worries me but I need to do the experiments to see if it is a problem.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.