Thread Rating:
Real-Time Sensor Fusion With Kalman Filters (Complete Guide + Example Code)
#1
Thread 11 — Real-Time Sensor Fusion With Kalman Filters 
How Modern Robots, Drones & Spacecraft Combine Noisy Sensors Into One Perfect Measurement

Kalman filters run in: 
• satellites 
• missiles 
• drones 
• autonomous vehicles 
• VR headsets 
• robotics 
• aircraft navigation 
• weather models 
• astrophysics data reduction 

If you want to understand REAL engineering… this is where it begins.

This thread teaches you:
• why sensors are noisy 
• why two sensors disagree 
• how the Kalman filter fixes it 
• how predictions & corrections combine 
• how to fuse accelerometer + gyroscope 
• how to implement your own filter in Python 
• how to run it on microcontrollers 



1. Why Sensor Fusion Matters

Sensors lie.

A temperature sensor drifts. 
A gyroscope drifts. 
An accelerometer is noisy. 
A GPS jumps around. 
A magnetometer swings when metal is nearby. 

But when you COMBINE sensors properly:

• noise cancels 
• drift disappears 
• accuracy improves 10× 
• stability becomes rock solid 

That is the magic of a Kalman filter.



2. The Core Idea — Predict + Correct

Every step the Kalman filter does two things:

1) Predict 
It uses physics to estimate the next state. 
Example: “Based on velocity, your position should be here.”

2) Correct 
It checks the prediction against the actual sensor reading.

Then it blends both using **weights** determined by uncertainty.

If a sensor is noisy → trust prediction more. 
If prediction is weak → trust measurement more.

Mathematically, this creates a perfect blend.



3. The Famous Kalman Equations (Explained Simply)

Prediction step: 
• xₚ = A·x + B·u 
• Pₚ = A·P·Aᵀ + Q 

Correction step: 
• K = Pₚ·Hᵀ · (H·Pₚ·Hᵀ + R)⁻¹ 
• xₙ = xₚ + K·(z − H·xₚ) 
• Pₙ = (I − K·H)·Pₚ 

Where: 
x = state (position, velocity, angle) 
z = sensor measurement 
A = motion model 
H = measurement model 
P = uncertainty 
Q = model noise 
R = sensor noise 
K = Kalman gain 

You don’t need to memorise these — 
the code does it for you.



4. Real Application Example — Fusing Accelerometer + Gyroscope

Accelerometer: 
• stable long-term angle 
• VERY noisy short-term 

Gyroscope: 
• smooth short-term angle 
• drifts long-term 

Combined with Kalman: 
→ smooth & stable 
→ no noise 
→ no drift 
→ perfect angle tracking 

This is how drones keep upright.



5. Python Example — Fully Working Kalman Filter

This example fuses accel + gyro into a clean angle estimate.

Code:
import numpy as np

# Time step
dt = 0.01 

# Initial state: [angle, gyro_bias]
x = np.array([[0.0],
              [0.0]])

# State covariance
P = np.eye(2)

# Process noise
Q = np.array([[0.001, 0],
              [0, 0.003]])

# Measurement noise (accelerometer)
R = np.array([[0.03]])

# State transition matrix
A = np.array([[1, -dt],
              [0, 1]])

# Control matrix
B = np.array([[dt],
              [0]])

# Measurement matrix
H = np.array([[1, 0]])

angle_kalman = []

for t in range(1000):
    gyro = 0.02        # example input
    accel_angle = 1.2  # noisy angle reading from accelerometer

    # ==== Prediction ====
    x = A @ x + B * gyro
    P = A @ P @ A.T + Q

    # ==== Correction ====
    z = np.array([[accel_angle]])

    K = P @ H.T @ np.linalg.inv(H @ P @ H.T + R)
    x = x + K @ (z - H @ x)
    P = (np.eye(2) - K @ H) @ P

    angle_kalman.append(float(x[0]))
   
print("Final filtered angle:", angle_kalman[-1])

Paste this into Python — 
you’ll see how insanely stable the filtered angle becomes.



6. Microcontroller Version (Pseudocode)

Use this on:
• Arduino 
• ESP32 
• STM32 

Code:
loop():
    gyro = readGyro()
    accel = readAccelerometerAngle()

    # Prediction
    x = A * x + B * gyro
    P = A * P * A^T + Q

    # Correction
    K = P * H^T * inv(H * P * H^T + R)
    x = x + K * (accel - H * x)
    P = (I - K * H) * P

    filtered_angle = x[0]

This is exactly what real drones run internally.



7. When to Use Kalman Filters

Use them when you have:
• noisy sensors 
• drifting sensors 
• two sensors disagreeing 
• predictions that need refinement 

Perfect for:
• robotics 
• self-driving cars 
• tracking objects 
• signals buried in noise 
• stabilising cameras 
• navigation & aerospace 
• weather forecasting 



8. When NOT to Use Them

Kalman filters are NOT ideal when:
• sensors behave non-linearly 
• errors are extremely large 
• the system cannot be modelled mathematically 

In those cases → Extended Kalman Filter (EKF) 
or → Particle Filter.

(We’ll write threads for those too.)



9. Example: How NASA Uses This

• Apollo spacecraft guidance 
• Mars rovers positioning 
• ISS attitude control 
• Satellite orbit determination 
• Real-time trajectory prediction 

It is one of the most important algorithms in aerospace history.



10. What You Learned

By completing this thread you now understand:

• the reason sensors disagree 
• why predictions need correction 
• the math behind Kalman filters 
• how real-time sensor fusion works 
• how to implement a working filter 
• how drones and spacecraft stabilise themselves 
• how FFT, DSP & fusion tie into real engineering 

This thread alone puts users years ahead of typical beginners.



Written by LeeJohnston — The Lumin Archive Research Division
Reply
« Next Oldest | Next Newest »


Forum Jump:


Users browsing this thread: 1 Guest(s)