Overview
Wheel odometry alone drifts due to slippage. IMU (Inertial Measurement Unit) provides complementary data:- Odometry: Good at estimating position, poor at orientation (slips during rotation)
- IMU: Good at orientation (gyroscope), drifts in position (accelerometer double integration)
robot_localization package provides EKF node that fuses odometry, IMU, and other sensors.
Architecture
TF Frames
Before fusion:odom → base_link transform.
Installation
IMU Data Publishing
From ESP32 to ROS2
ESP32 reads ICM-20948, publishes via serial: Protocol: Same as encoder data, extended with IMU:gx, gy, gz: Gyroscope (rad/s)ax, ay, az: Accelerometer (m/s²)
Hardware Interface Update
Extend hardware interface to read IMU:IMU Publisher Node
Create separate node to publishsensor_msgs/Imu:
File: src/imu_publisher.cpp (in mecanum_hardware package)
EKF Configuration
Configuration File
File:config/ekf.yaml
Configuration Explained
Key sections:-
Odometry configuration (
odom0_config):- Use X, Y position ✓
- Use X, Y velocity ✓
- Don’t use yaw ✗ (IMU better for orientation)
-
IMU configuration (
imu0_config):- Use yaw (heading) ✓
- Use yaw velocity (angular velocity) ✓
- Optionally use acceleration (if calibrated)
-
Two_d_mode:
true: Ignores Z, roll, pitch (for ground robots)
-
Process noise:
- Higher values = trust sensors less, smooth more
- Lower values = trust sensors more, follow closely
Launch File
File:launch/localization.launch.py
Complete Launch (Robot + Localization)
File:launch/robot_full.launch.py
Testing Fusion
Check Topics
Echo Filtered Odometry
Visualize in RViz
RViz config:- Fixed Frame:
odom - Add Odometry displays:
- Raw odometry: Topic
/mecanum_drive_controller/odom, Color: Red - Filtered odometry: Topic
/odometry/filtered, Color: Green
- Raw odometry: Topic
- Add IMU display: Topic
/imu/data
- Drive robot
- Green path (filtered) should be smoother
- Rotation should match IMU orientation
Tuning EKF
Sensor Covariance
Increase covariance = trust sensor less: Odometry (if drifts quickly):Process Noise
Inekf.yaml:
- Start with defaults
- If filtered odometry too jerky → Increase process noise
- If filtered odometry lags behind → Decrease process noise
Diagnostic Tools
Check EKF Status
- Sensor update rates
- Covariance values
- Errors/warnings
Plot Odometry Comparison
- Streaming → ROS2 Topics
- Start
- Plot:
/mecanum_drive_controller/odom/pose/pose/position/x(raw)/odometry/filtered/pose/pose/position/x(filtered)
- Observe difference
Benefits of Fusion
Better Orientation
IMU provides accurate heading, eliminating wheel slip errors during rotation.
Smoother Estimates
EKF filters noise from both sensors, producing smooth pose estimate.
Redundancy
If one sensor fails (encoder wire loose), other compensates temporarily.
Optimal Fusion
EKF weighs sensors by uncertainty (covariance), trusting more reliable source.
Limitations
Troubleshooting
EKF not publishing /odometry/filtered
EKF not publishing /odometry/filtered
Debug:
-
Check EKF node running:
-
Check input topics:
-
Check EKF logs:
- “Sensor timeout” → No data from sensor
- “Frame mismatch” → Frame IDs inconsistent
Filtered odometry same as raw
Filtered odometry same as raw
Cause: IMU not contributing (check
imu0_config)Solution:- Verify IMU data publishing:
ros2 topic echo /imu/data - Check
imu0_configenables yaw and yaw velocity - Verify
imu_remove_gravitational_acceleration: true
Filtered odometry diverges
Filtered odometry diverges
Cause: Sensor covariances misconfiguredSolutions:
- Increase odometry covariance if it drifts
- Increase IMU covariance if it’s noisy
- Check IMU calibration (gyro bias)
Next Steps
EKF Deep Dive
Understand Extended Kalman Filter theory and advanced tuning
Localization Testing
Comprehensive testing procedures for localization
SLAM Mapping
Use SLAM to correct long-term drift
IMU Calibration
Calibrate IMU for better accuracy