1268 Complementary Filters and IMU Fusion
Learning Objectives
After completing this chapter, you will be able to:
- Understand complementary filter principles
- Implement IMU sensor fusion for 3D orientation
- Apply Madgwick filter for quaternion-based orientation
- Choose between Kalman and complementary filters
- Design practical IMU fusion for drones and wearables
1268.1 Complementary Filter Principle
Complementary filters combine high-frequency data from one sensor with low-frequency data from another. They are commonly used for IMU fusion (accelerometer + gyroscope).
1268.1.1 The Core Idea
- Accelerometer: Accurate for orientation (long-term) but noisy (short-term)
- Gyroscope: Accurate for rotation rate (short-term) but drifts (long-term)
- Fusion: Use gyroscope for high-frequency updates, correct with accelerometer for low-frequency
Complementary filter equation:
\[\theta_{\text{fused}} = \alpha \cdot (\theta_{\text{prev}} + \omega \cdot dt) + (1-\alpha) \cdot \theta_{\text{accel}}\]
Where:
- \(\alpha\) = 0.98 (typical, “trust” parameter)
- \(\omega\): Gyroscope angular velocity
- \(dt\): Time step
- \(\theta_{\text{accel}}\): Orientation from accelerometer
Core Concept: Sensors are complementary when their error characteristics occupy different frequency bands. Gyroscopes are accurate for fast changes but drift slowly; accelerometers are noisy at high frequencies but stable over time. Fusion exploits this by high-pass filtering gyroscope data and low-pass filtering accelerometer data.
Why It Matters: The accelerometer measures gravity (constant, absolute reference) plus linear acceleration (noise during motion). It provides accurate orientation when stationary but becomes unreliable during movement. The gyroscope measures rotation rate, which must be integrated to get orientation. Integration accumulates small errors (bias, noise) into unbounded drift.
Key Takeaway: Before designing a fusion system, analyze each sensor’s error characteristics across frequency. Ask: “What time scales does each sensor excel at?”
1268.2 IMU Sensor Fusion for 3D Orientation
Inertial Measurement Units (IMUs) are ubiquitous in IoT applications - from drones and wearables to robotics and AR/VR systems.
1268.2.1 The Fundamental Problem
Neither accelerometer nor gyroscope alone provides accurate orientation tracking:
| Sensor | Strength | Weakness | Time Domain |
|---|---|---|---|
| Gyroscope | Smooth, fast response | Drifts over time (50+ deg in 30 sec) | Short-term OK, Long-term bad |
| Accelerometer | No drift, absolute gravity reference | Noisy, affected by motion | Short-term bad, Long-term OK |
1268.2.2 Why This Fusion Works
The key insight is that these sensors have complementary error characteristics:
- Gyroscope errors accumulate (integration drift) but are smooth
- Accelerometer errors fluctuate (noise) but don’t accumulate
By applying a high-pass filter to gyroscope data (keeps fast changes) and a low-pass filter to accelerometer data (keeps slow trends), we get the best of both worlds.
Core Concept: Sensor drift is the gradual change in a sensor’s output over time even when the measured quantity remains constant - caused by component aging, temperature variations, mechanical stress, and environmental contamination.
Why It Matters: An accelerometer bias of 0.01 m/s2 accumulates to 18 meters position error after just one minute of dead reckoning. Drift is invisible in short tests but catastrophic in deployed systems.
Key Takeaway: Assume all sensors drift. Implement periodic calibration against known references. Use complementary sensors where possible - GPS corrects IMU drift, while IMU fills gaps between GPS updates.
1268.2.3 Gyroscope Drift Explained
Gyroscopes measure angular velocity (degrees per second), not orientation directly. To get orientation, we must integrate over time:
\[\theta(t) = \theta_0 + \int_0^t \omega(\tau) \, d\tau\]
The Problem: Even tiny sensor bias accumulates relentlessly:
- Gyroscope bias: 0.01 deg/s (typical for MEMS gyros)
- After 30 seconds: 0.01 x 30 = 0.3 deg minimum drift
- Real-world noise: Often 50+ deg drift in 30 seconds
1268.2.4 Accelerometer Angles
Accelerometers measure the gravity vector (when at rest), providing an absolute reference for roll and pitch (but not yaw):
\[\text{Roll} = \arctan2(A_y, A_z)\]
\[\text{Pitch} = \arctan2(-A_x, \sqrt{A_y^2 + A_z^2})\]
Advantages:
- No drift: Gravity direction is constant
- Absolute reference: Not relative measurements
Limitations:
- Noise: Vibrations cause high-frequency fluctuations
- Motion interference: Linear acceleration adds to gravity vector
- No yaw information: Gravity is vertical - can’t determine heading
1268.2.5 The Elegant Solution
The complementary filter combines gyroscope and accelerometer data:
\[\theta_{\text{fused}} = \alpha \cdot (\theta_{\text{prev}} + \omega \cdot \Delta t) + (1-\alpha) \cdot \theta_{\text{accel}}\]
How it works:
- 98% weight to gyroscope (alpha = 0.98): Preserves fast dynamics
- 2% weight to accelerometer (1-alpha = 0.02): Slowly corrects drift
- Drift correction time constant: tau = dt/(1-alpha) = 0.5 seconds (for alpha=0.98, dt=0.01)
Intuition: Trust the gyroscope for rapid changes (it’s responsive), but continuously “nudge” it back toward the accelerometer’s gravity reference.
1268.3 Filter Parameter Tuning
| Alpha Value | Behavior | Use Case |
|---|---|---|
| 0.95 | Fast drift correction, more noise | High-vibration environments |
| 0.98 | Balanced (most common) | General-purpose IMU orientation |
| 0.99 | Slow drift correction, very smooth | Low-vibration, slow motion |
Time constant formula: \(\tau = \frac{\Delta t}{1-\alpha}\)
Example: alpha = 0.98, dt = 10ms gives tau = 0.5s (drift corrects in half a second)
Option A: Kalman filter (optimal state estimation with covariance tracking)
Option B: Complementary filter (simple weighted combination with tuned alpha)
Decision Factors: Kalman filters are mathematically optimal for linear systems with Gaussian noise, automatically adapt gain based on measurement uncertainty, and track confidence bounds - but require correct noise models (Q, R matrices), more computation (~3x), and careful initialization.
Complementary filters are simple to implement (5 lines of code), computationally cheap, and robust with a single tuning parameter (alpha) - but don’t adapt to changing conditions and provide no uncertainty estimate.
Choose Kalman for GPS/IMU fusion where accuracy matters and you have accurate noise models.
Choose complementary filters for IMU orientation on resource-constrained MCUs where simplicity and reliability beat optimality.
1268.4 3D Orientation Visualization
Euler Angles:
- Roll (phi): Rotation about X-axis (tilting left/right)
- Pitch (theta): Rotation about Y-axis (nose up/down)
- Yaw (psi): Rotation about Z-axis (compass heading)
Important: Complementary filters using only accelerometer + gyroscope can estimate roll and pitch accurately, but not yaw. For full 3D orientation including heading, you need a magnetometer (9-DOF IMU).
1268.5 Real-World Implementation
Drone IMU Fusion Pipeline (100 Hz update rate):
1. Read sensors (every 10ms):
- Gyroscope: wx, wy, wz (degrees/second)
- Accelerometer: Ax, Ay, Az (g's)
2. Calculate accelerometer angles:
roll_accel = atan2(Ay, Az) * 180/pi
pitch_accel = atan2(-Ax, sqrt(Ay^2 + Az^2)) * 180/pi
3. Integrate gyroscope (trapezoidal):
roll_gyro = roll_prev + (wx_prev + wx)/2 * 0.01
pitch_gyro = pitch_prev + (wy_prev + wy)/2 * 0.01
4. Complementary fusion (alpha=0.98):
roll_fused = 0.98 * roll_gyro + 0.02 * roll_accel
pitch_fused = 0.98 * pitch_gyro + 0.02 * pitch_accel
5. Send to PID controller for motor corrections
Result: +/-1 deg orientation accuracy even with:
- Propeller vibrations (200 Hz accelerometer noise)
- Wind gusts (rapid gyroscope changes)
- 30+ minutes of flight (no gyroscope drift)
Performance metrics:
- Without fusion: +/-10 deg oscillations (accel noise) OR 5 deg/minute drift (gyro only)
- With fusion: +/-1 deg stable orientation, no drift, smooth response
1268.6 Why This Matters for IoT
IMU sensor fusion is critical for numerous IoT domains:
| Application | Role of IMU Fusion | Required Accuracy |
|---|---|---|
| Drone Stabilization | Maintains level flight | +/-1 deg, 10 Hz |
| Wearable Posture | Detects slouching, falls | +/-5 deg, 50 Hz |
| AR/VR Head Tracking | Natural viewport control | +/-0.5 deg, 200+ Hz |
| Robotics Navigation | Self-balancing robots | +/-2 deg, 100 Hz |
| Gesture Recognition | Smart home controllers | +/-10 deg, 30 Hz |
Computational Cost: Complementary filters are extremely lightweight:
- Operations per update: ~10 floating-point operations
- Memory: 12 bytes (3 previous angles)
- MCU suitability: Runs on 8-bit AVR microcontrollers
Comparison to Kalman Filter:
- Kalman filter: ~500 FLOPS, 200+ bytes RAM
- Complementary filter: ~10 FLOPS, 12 bytes RAM
- Accuracy difference: Complementary filter is ~90% as accurate
When to upgrade: Use Extended Kalman Filter or Madgwick filter when you add a magnetometer (9-DOF) or need optimal sensor fusion with different update rates.
1268.7 Madgwick Filter (Quaternion-Based IMU Fusion)
The Madgwick filter is an efficient orientation filter for 9-DOF IMU data using quaternions to avoid gimbal lock.
Advantages over Euler angles:
- No gimbal lock: Euler angles fail at pitch=+/-90 deg, quaternions work for any orientation
- Computational efficiency: 16 multiplies vs 27 for rotation matrices
- Smooth interpolation: Natural rotation paths with SLERP
- Compact: 4 values [w,x,y,z] vs 9 (rotation matrix)
Trade-off: Less intuitive for humans - convert to Euler angles for display.
Beta parameter: Typical values 0.01-0.1, controls accelerometer correction strength.
1268.7.1 9-DOF IMU: Adding the Magnetometer
6-DOF (accel+gyro) can estimate roll/pitch from gravity but yaw (compass heading) is unobservable. Gyroscope integrates yaw but drifts without reference.
Magnetometer measures Earth’s magnetic field (~0.5 gauss, pointing north with dip angle). It provides absolute yaw reference like accelerometer provides pitch/roll reference.
Fusion strategy:
- Accel corrects pitch/roll drift
- Magnetometer corrects yaw drift
- Gyro provides high-frequency updates
Challenges: Magnetic interference from electronics, metal structures requires calibration.
1268.8 Summary
Complementary filters and IMU fusion provide efficient orientation estimation:
- Complementary filters: Simple, efficient fusion of complementary sensor characteristics
- Alpha parameter: Balances noise rejection vs drift correction speed
- IMU fusion: Gyroscope for fast response, accelerometer for drift correction
- Madgwick filter: Quaternion-based for 9-DOF IMU, avoids gimbal lock
- Application-dependent: Choose based on accuracy needs vs computational constraints
1268.9 What’s Next
- Particle Filters - Non-linear, non-Gaussian systems
- Fusion Architectures - System design patterns
- Best Practices - Common pitfalls to avoid
- Exercises - Practice implementing IMU fusion