ROBOTICS 101Concept explainer

Complementary Filter for Tilt

The accelerometer gives a tilt that is right on average but jumpy. The gyroscope gives a tilt that is smooth but slowly drifts. A complementary filter trusts the gyro for quick changes and the accelerometer for the long run.

The filter

Each step, take the previous estimate, add the gyro's rate times the time step, then nudge it a little toward the accelerometer's tilt:

$$\hat\theta_k = \alpha\left(\hat\theta_{k-1} + \omega_k\,\Delta t\right) + (1 - \alpha)\,\theta_{\text{accel},k}$$

The gyro path acts as a high-pass filter and the accelerometer path as a low-pass filter, and the two halves add up to one, which is where the name comes from.

What to notice

The gray accelerometer line is noisy, and it jumps whenever the robot jolts. The amber gyro-only line is smooth but walks away from the truth, because of the bias. The red fused line follows the truth closely. Set α to 1 and the filter becomes the gyro alone, so it drifts. Set α to 0 and it becomes the accelerometer alone, so it is noisy. Values near 0.98 work well for many robots.

This one-line filter runs on countless drones and balancing robots. A Kalman filter does the same job, but picks the blend for you from the noise levels.