Robotics Engineering DISCUSSION

Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

Started by dattakakade complementary filterKalman filterIMU sensor fusiongyro driftbalancing robot
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

dattakakade Robotics Engineering Forum
#1

I am building a two-wheel balancing robot with a 6-axis IMU (gyro plus accelerometer) read at 100 Hz. The angle from the accelerometer alone is very noisy when the motors run, and the angle from integrating the gyro slowly drifts away.

Most tutorials jump straight to a Kalman filter, others use a one-line complementary filter. What does each one actually do with the two sensors, and is the Kalman filter worth the extra work for a single tilt angle?

Community replies 4

Re: Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

#2

The two sensors fail in opposite ways, which is why they are combined. The gyro measures angular rate, so integrating it gives a smooth angle, but any bias is integrated too: a bias of only 0.5°/s becomes 30° after one minute. The accelerometer gives an absolute angle from gravity, atan2(ax, az), with no drift, but it also measures every linear acceleration and vibration. A horizontal acceleration of 0.1 g shifts the computed tilt by atan(0.1), about 5.7°.

So trust the gyro over short times and the accelerometer over long times.

Re: Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

#3

The complementary filter does exactly that in one line: angle = a × (angle + gyro × dt) + (1 - a) × accAngle. It is a high-pass on the integrated gyro and a low-pass on the accelerometer with the same crossover. The time constant is τ = a × dt / (1 - a). With a = 0.98 and dt = 0.01 s, τ is 0.49 s: faster changes come from the gyro, slower ones from the accelerometer.

Tune a by looking at the result: too low and motor vibration leaks in, too high and gyro bias shows up as a steady angle offset.

Re: Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

#4

A Kalman filter for this problem usually has two states, the angle and the gyro bias. It predicts with the gyro, corrects with the accelerometer, and computes the blending gain from the noise variances you give it. The real benefit over the fixed filter is that it estimates the bias and removes it, so there is no steady offset.

For one axis with constant noise settings the Kalman gain converges to a constant, and the filter then behaves very much like a complementary filter with a bias estimator. The quality of the result depends on the Q and R values, which you still end up tuning by experiment.

Re: Complementary filter or Kalman filter for the tilt angle of a two-wheel balancing robot?

#5

For a balancing robot the complementary filter is normally enough, and it is easier to debug. Two details matter more than the choice of filter. Measure the gyro bias at start-up by averaging a second or two of readings while the robot is held still, and subtract it. And use the real elapsed time for dt rather than an assumed loop period, because a loop that runs at 9 ms when the code assumes 10 ms makes every gyro step about 10 percent too large.

Set the IMU's built-in low-pass filter to a few tens of hertz so the accelerometer does not alias motor vibration.

TEP COMMUNITY