Open Source Robotics

A curated collection of high-quality open source robotics projects, tools, and software to propel the robotics community forward.

Complementary Filter

  • Category: State estimation & navigation › Localisation
  • Tags: state-estimation, imu, attitude-estimation, sensor-fusion, filtering

A simple estimator that merges two sensors with complementary noise by low-pass filtering one and high-pass filtering the other; commonly used to fuse gyroscope and accelerometer data into attitude.

Definition

A complementary filter combines two measurements of the same quantity whose errors lie in different frequency bands. One sensor is trusted at low frequencies and passed through a low-pass filter, the other is trusted at high frequencies and passed through a high-pass filter, and the two filters are chosen to sum to one so that the true signal passes through unchanged. The classic case is attitude estimation from an IMU: integrating the gyroscope gives smooth, responsive angles that drift because of bias, while the tilt computed from the accelerometer is noisy and disturbed by vibration and acceleration but does not drift. Blending them gives roll and pitch that are both responsive and stable.

In its simplest discrete form, the filter updates an angle as θ ← α(θ + ω·Δt) + (1 − α)·θ_acc, where α close to one sets the crossover time constant. Nonlinear versions work directly on rotation matrices or quaternions to stay valid at large angles; the explicit complementary filter of Mahony, Hamel and Pflimlin (2008) also estimates gyroscope bias through an integral term, and Madgwick's filter uses a gradient-descent correction step. Heading cannot be corrected by the accelerometer, because gravity carries no information about rotation about the vertical axis, so yaw needs a magnetometer, vision or another reference.

The key assumption is that, averaged over time, the accelerometer measures the reaction to gravity. An accelerometer actually measures specific force, the difference between the body's acceleration and gravity, so during sustained acceleration, such as a long coordinated turn, the tilt reference is biased. On multirotors in flight, the horizontal accelerometer readings mostly reflect rotor drag rather than tilt, which limits how well the assumption holds. Compared with a Kalman filter, a complementary filter has fixed gains, no explicit uncertainty model and very low computational cost, which is why it is common on small flight controllers and hobby IMU boards. For linear problems with stationary noise, a steady-state Kalman filter takes the same complementary form, with gains chosen from the noise model.

Why it matters

It is often the first sensor-fusion method engineers implement, and it remains a practical choice wherever compute is tight or a simple, tunable attitude estimate is enough. Understanding its gravity assumption also explains why attitude estimates degrade during sustained accelerations.

Key applications

  • Attitude estimation on small flight controllers and IMU boards
  • Balancing robots and inverted pendulum platforms
  • Tilt sensing for camera gimbals and handheld devices
  • Fusing barometer and accelerometer data for altitude and climb rate
  • Fallback attitude estimation alongside more complex filters

In practice

A two-wheeled balancing robot reads a six-axis IMU inside a fast control loop on a microcontroller. The pitch angle computed with atan2 from the accelerometer axes jumps whenever the motors jerk the chassis, while the integrated gyroscope rate drifts steadily. The engineer subtracts a gyroscope bias measured at startup, then blends the two with a complementary filter, choosing a crossover time constant long enough to ignore motor jerks but short enough to cancel the drift. The same idea appears in research systems: in work on quadrotors flying after a rotor failure, a complementary filter supplies pitch and roll from the IMU alone, while onboard vision supplies position and heading.

Common misconceptions

  • The accelerometer measures gravity — it measures specific force, which equals the gravity reaction only when the body is not accelerating
  • A gyroscope and accelerometer are enough for full 3D orientation — yaw is unobservable from gravity and drifts without a magnetometer or other heading reference
  • A complementary filter is a crude alternative to a Kalman filter — a steady-state Kalman filter for a linear problem has the same structure; the difference is that its gains come from a noise model

Who should know this

  • Embedded and flight controller developers
  • Hobbyists building drones and balancing robots
  • State estimation engineers
  • Robotics students learning sensor fusion

Related technologies

  • Kalman Filter
  • Mahony Filter
  • Madgwick Filter
  • Attitude and Heading Reference System (AHRS)
  • Quaternion Integration

Related terms

  • IMU
  • Sensor Fusion
  • Extended Kalman Filter
  • Yaw / Pitch / Roll
  • UAV
  • Visual-Inertial Odometry

Related projects

  • PCL: Industry-standard C++ library for point cloud processing in robotics perception
  • OpenCat: An open-source Arduino and Raspberry Pi-based quadruped robot pet framework
  • FAST-LIVO2: Fast, direct LiDAR-inertial-visual odometry from HKU-MARS, published in IEEE T-RO
  • GTSAM: Factor-graph optimization library powering many modern SLAM backends
  • OpenVINS: The reference open platform for visual-inertial state estimation research
  • manif: Header-only C++ Lie theory library for robotics state estimation

Videos

  • Drone Control and the Complementary Filter: Brian Douglas
  • Complementary Filter Sensor Fusion between Accelerometer and Gyroscope to estimate Pitch and Roll: OtherYATC

This website requires JavaScript to function properly. Please enable JavaScript to explore our collection of open source robotics projects.