Simvik

Drone: a tilt angle that does not drift

Fuse a drifting gyroscope with a noisy accelerometer into one stable tilt angle, in integers. A hard embedded C problem (Sensors and sensor fusion), run and…

hard · Sensors and sensor fusion · complementary-filter · gyro · accelerometer · product

Product: Drone flight controller (Aerospace and drones)

Requirement. The flight controller levels the drone from its tilt angle. The gyroscope is smooth but its small bias makes the angle drift without limit; the accelerometer never drifts but jumps with every vibration. A complementary filter takes the best of both.

Each update, with angles in hundredths of a degree and the gyro rate in hundredths of a degree per second:

  1. predicted = angle + gyro × dt_ms / 1000 (rounded to the nearest)
  2. new angle = (98 × predicted + 2 × accel) / 100 (rounded to the nearest)

Round halves away from zero. Return the new angle.

Starter code

int32_t tilt_update(int32_t angle_cd, int32_t gyro_cdps, int32_t accel_cd, uint16_t dt_ms) {
  return angle_cd + gyro_cdps * dt_ms / 1000;
}

Example tests

Level and still, then turning

printf("%d %d\n", tilt_update(0, 0, 0, 10), tilt_update(0, 1000, 0, 10));

Expected output:

0 10

A gyro with a bias, for ten seconds

int32_t a = 0;
for (int i = 0; i < 1000; i++) a = tilt_update(a, 150, 0, 10);
printf("%d\n", a);

Expected output:

74

Learn it first

More Sensors and sensor fusion problems