Overview
The Extended Kalman Filter (EKF) is the algorithm behind sensor fusion inrobot_localization. It optimally combines noisy sensor measurements to estimate robot state (pose and velocity).
Why EKF? It handles nonlinear motion models (like Mecanum kinematics) and provides uncertainty estimates (covariance).
Kalman Filter Basics
What is a Kalman Filter?
Kalman Filter = Recursive algorithm that:- Predicts next state based on motion model
- Updates prediction using sensor measurements
- Weights prediction vs measurement based on uncertainty
EKF vs Linear KF
Linear Kalman Filter:- Motion model is linear:
x_new = A * x_old + B * u - Works for simple systems (e.g., moving in straight line)
- Motion model is nonlinear:
x_new = f(x_old, u) - Uses linearization (Jacobian matrices)
- Works for complex robots (Mecanum, Ackermann, etc.)
State Vector
EKF estimates 15-dimensional state vector:- Only
x,y,yaw,vx,vy,vyawused - Others set to zero
EKF Algorithm (Simplified)
Prediction Step
Every time step (50Hz):-
Predict state:
-
Predict uncertainty:
- Uncertainty grows with time (process noise)
- Covariance matrix P increases
Update Step
When sensor data arrives:-
Compute innovation (error):
-
Compute Kalman gain:
P: Prediction covarianceH: Measurement model (which states sensor measures)R: Sensor noise covariance
-
Update state:
-
Update uncertainty:
- High sensor noise (large R) → Small K → Trust prediction more
- Low sensor noise (small R) → Large K → Trust measurement more
Configuration Deep Dive
Sensor Configuration
Format: 15-element boolean array- ✓ Position (X, Y): Odometry good at position
- ✓ Velocity (X, Y): Odometry provides velocity
- ✗ Yaw: Wheel slip makes yaw unreliable (use IMU instead)
- ✓ Yaw: Gyroscope accurate for orientation
- ✓ Yaw velocity: Angular velocity from gyroscope
- ? Acceleration: Can use if calibrated, but often noisy
Covariance Matrices
Sensor Covariance (R)
From sensor messages:- Odometry:
pose.covarianceandtwist.covariance - IMU:
angular_velocity_covariance,linear_acceleration_covariance
- Small covariance → High confidence → EKF trusts sensor more
- Large covariance → Low confidence → EKF trusts sensor less
Process Noise Covariance (Q)
Inekf.yaml:
| Index | Variable | Typical Value | Meaning |
|---|---|---|---|
| 0,0 | X position | 0.05 | Position drift rate |
| 1,1 | Y position | 0.05 | Position drift rate |
| 5,5 | Yaw | 0.06 | Orientation drift rate |
| 6,6 | X velocity | 0.025 | Velocity change rate |
| 7,7 | Y velocity | 0.025 | Velocity change rate |
| 11,11 | Yaw velocity | 0.06 | Angular accel. noise |
- Increase → EKF assumes state changes quickly (trust sensors more)
- Decrease → EKF assumes state changes slowly (smooth more)
Advanced Parameters
Tuning Workflow
Step 1: Baseline
Start with default configuration:frequency: 50.0two_d_mode: true- Process noise: 0.05 (position), 0.025 (velocity)
- Sensor covariances from sensor messages
Step 2: Test and Observe
Drive robot, monitor filtered odometry:| Symptom | Cause | Solution |
|---|---|---|
| Filtered odometry lags behind | Process noise too small | Increase process noise |
| Filtered odometry too jerky | Process noise too large | Decrease process noise |
| Orientation drifts | IMU not fused | Check imu0_config[5] = true (yaw) |
| Position jumps | Sensor covariance wrong | Increase sensor covariance |
Step 3: Validation
Quantitative tests: Test 1: Straight line- Drive 2m straight
- Compare filtered vs ground truth
- Error should be < 5cm
- Rotate 360°
- Check final yaw close to 2π rad
- Error should be < 5°
- Drive 1m square
- Return to start
- Closure error should be < 10cm
Multiple EKF Instances
For some applications, run two EKF nodes: EKF 1 (Local odometry):- Fuses: Odometry + IMU
- Publishes:
odom → base_link - Use for: Smooth local motion
- Fuses: Odometry + IMU + GPS (or SLAM corrections)
- Publishes:
map → odom - Use for: Absolute position in world
Diagnostic Tools
RViz Visualization
Add Odometry displays:- Raw odometry:
/mecanum_drive_controller/odom(Red) - Filtered odometry:
/odometry/filtered(Green)
- Green should be smoother than red
- During rotation, green should be more stable
rqt_multiplot
Plot multiple topics simultaneously:- X position: Raw vs Filtered
- Yaw: Raw vs Filtered vs IMU
Covariance Ellipses
In RViz (Odometry display):- Enable “Covariance” → “Position”
- Shows uncertainty ellipse
- Small ellipse → High confidence
- Large ellipse → Low confidence
- Ellipse should be small and stable
EKF State Transition
Visualization of EKF state machine:Mathematical Details (Advanced)
Prediction Equations
State prediction:F_k: Jacobian of motion model (linearization)Q_k: Process noise covariance
Update Equations
Kalman gain:H_k: Jacobian of measurement modelR_k: Sensor noise covariancez_k: Measurementh(x): Measurement model
You don’t need to implement this!
robot_localization does it for you. Understanding helps with tuning.Troubleshooting
EKF diverges (explodes)
EKF diverges (explodes)
Symptom: Filtered odometry grows unboundedCauses:
- Sensor covariances too small (EKF over-trusts sensors)
- Process noise too large
- Sensor data has outliers
- Increase sensor covariances
- Enable rejection threshold:
- Check sensor data for spikes
Filtered odometry lags
Filtered odometry lags
Symptom: Filtered pose trails behind robotCause: Process noise too smallSolution:
- Increase process noise covariance (especially position)
Orientation doesn't improve
Orientation doesn't improve
Symptom: Filtered yaw same as raw odometryCause: IMU not contributing to yaw estimateSolutions:
- Check IMU data publishing:
ros2 topic echo /imu/data - Verify
imu0_config[5]= true (yaw) - Ensure
imu0_config[11]= true (yaw velocity) - Check IMU covariance is reasonable (not too high)
Covariance grows unbounded
Covariance grows unbounded
Symptom: Uncertainty ellipse keeps growingCause: No sensor updates, only predictionSolutions:
- Check sensors publishing data
- Verify sensor_timeout not too small
- Ensure sensor config enables some measurements
Best Practices
Start Simple
Begin with odometry only, then add IMU. Easier to debug.
Calibrate Sensors
Accurate sensor covariances critical for good fusion. Measure empirically if needed.
Validate Frequently
Test after every configuration change. Small systematic changes better than random tweaking.
Record Data
Use
ros2 bag record to replay and tune offline.Next Steps
Localization Testing
Comprehensive testing procedures and validation
SLAM Mapping
Use SLAM to correct global position drift
Navigation
Use localization for autonomous navigation
Advanced Tuning
Official robot_localization tuning guide