Fusing IMU, Wheel Odometry, and GPS with ROS 2 robot_localization: A Dual EKF Guide

ContentsIntroduction
- Introduction
- The Dual-EKF Architecture: Local (odom) vs. Global (map) Frames
- EKF State Vector Formulation
- Mathematical Update Cycle
- Step 1: Configuring navsat_transform_node
- Critical Prerequisite:
- Step 2: Example ROS 2 Dual-EKF YAML Configuration
- Step 3: Launch and verify TF ownership
- Further reading and technical reference
Introduction
Autonomous outdoor mobile robots face severe state estimation challenges. Relying solely on wheel encoders leads to catastrophic position drift over time due to wheel slip, mechanical backlash, and varying surface friction. Conversely, relying purely on GPS data exposes the navigation stack to signal occlusions, multipath interference, and low sampling frequencies (typically 1 Hz−10 Hz), yielding abrupt position jumps that ruin local path execution.
To achieve more robust, drift-managed localization required for autonomous navigation (such as Nav2), mobile robots utilize Multi-Sensor Data Fusion.
The industry standard in ROS 2 is the robot_localization package, which implements an Extended Kalman Filter (EKF) to fuse arbitrary numbers of sensor streams into a continuous, statistically optimal 3D pose and velocity estimate.
This guide details the mathematical foundations of EKF sensor fusion, explains the commonly used dual-EKF architecture, and provides a complete ROS 2 configuration fusing a 9-DOF IMU, optical wheel odometry, and RTK-GPS.
The Dual-EKF Architecture: Local (odom) vs. Global (map) Frames
A fundamental mistake in outdoor robot setup is directly feeding discontinuous GPS updates into the local control loop. When a GPS receiver fixes its position or receives an RTK differential correction, the position output can jump by several centimeters or meters instantly. If the local velocity controller (e.g., DWB or MPPI in Nav2) sees this coordinate jump, it perceives infinite instantaneous acceleration, triggering violent motor commands or control loop crashes.
To prevent this, ROS REP-105 distinguishes continuous odom and globally corrected map frames; a dual-EKF setup is one way to implement that distinction:
DUAL-EKF ARCHITECTURE +-----------------------+ +------------------------+ | Wheel Encoders (20Hz)| | 9-DOF IMU (100Hz-1kHz)| +-----------------------+ +------------------------+ | | +------------------+-------------------+ | v +-------------------------------+ | ekf_node_local (Continuous) | | World Frame: odom |---> TF: odom -> base_link | Publishes: /odometry/local | (Smooth for Local Control) +-------------------------------+ | +--------------------+--------------------+ | | v v +--------------------+ +---------------------+ | navsat_transform |<-- Raw GPS Fix | ekf_node_global | | Transforms WGS84 | (/gps/fix) | World Frame: map |--> TF: map -> odom | to UTM Cartesian |------------------->| Publishes: /odom/gps| (Global Drift-Corrected) +--------------------+ +---------------------+- Local EKF (ekf_node_local): Fuses high-rate, continuous sensor inputs (wheel odometry + IMU). Its output frame is set to odom. It aims to provide continuous pose estimates when inputs and resets are managed correctly, satisfying local velocity controllers.
- Global EKF (ekf_node_global): Fuses continuous sensor inputs plus transformed global GPS coordinates. Its output frame is set to map. It calculates the offset between the absolute global coordinate system and the drifting local system, publishing the dynamic map -> odom transform tree.
EKF State Vector Formulation
The robot_localization package tracks a 15-dimensional state vector xk at time step k:
xk=[xyzϕθψx˙y˙z˙ϕ˙θ˙ψ˙x¨y¨z¨]T
Where:
- x,y,z: 3D Position
- ϕ,θ,ψ: Euler Angles (Roll, Pitch, Yaw)
- x˙,y˙,z˙: Linear Velocities
- ϕ˙,θ˙,ψ˙: Angular Velocities
- x¨,y¨,z¨: Linear Accelerations
Mathematical Update Cycle
At each time step, the EKF executes two distinct phases:
- Prediction Step (3D Kinematic Model): Uses the dynamic state model to project the state x^k− and error covariance matrix Pk− forward in time: x^k−=f(x^k−1,uk) Pk−=FkPk−1FkT+Qk
- Measurement Correction Step: When a sensor reading zk arrives, the filter calculates the Kalman Gain Kk using the sensor's Measurement Covariance Matrix Rk: Kk=Pk−HkT(HkPk−HkT+Rk)−1 x^k=x^k−+Kk(zk−h(x^k−)) Pk=(I−KkHk)Pk−
Key Takeaway: The lower a sensor's covariance values in Rk, the higher the Kalman Gain Kk, forcing the state vector to trust that sensor more heavily over the internal motion model.
Step 1: Configuring navsat_transform_node
The navsat_transform_node transforms global WGS84 GPS latitude/longitude coordinates (sensor_msgs/msg/NavSatFix) into 3D Cartesian coordinates (nav_msgs/msg/Odometry) in UTM (Universal Transverse Mercator) projections.
Critical Prerequisite:
navsat_transform_node needs a trustworthy earth-referenced heading source. A calibrated IMU is one option; a properly configured GNSS heading or absolute odometry source can also be suitable. A magnetometer is not universally required. Follow the documented east-zero yaw convention.
Step 2: Example ROS 2 Dual-EKF YAML Configuration
Below is an example dual_ekf_navsat.yaml configuration for ROS 2 Humble/Jazzy:
# ===================================================================# 1. LOCAL EKF NODE (Outputs odom -> base_link)# ===================================================================ekf_filter_node_local: ros__parameters: frequency: 30.0 two_d_mode: true # Set true for ground mobile platforms publish_tf: true map_frame: map odom_frame: odom base_link_frame: base_link world_frame: odom # Local frame execution # --- Sensor 1: Wheel Odometry --- odom0: /wheel/odometry # Config Matrix: [x, y, z, roll, pitch, yaw, vx, vy, vz, vroll, vpitch, vyaw, ax, ay, az] odom0_config: [false, false, false, false, false, false, true, true, false, false, false, true, false, false, false] odom0_differential: false odom0_queue_size: 10 # --- Sensor 2: 9-DOF BNO085 IMU --- imu0: /imu/data imu0_config: [false, false, false, false, false, true, # Yaw absolute reading false, false, false, false, false, true, # Angular velocity z true, false, false] # Linear acceleration x imu0_differential: false imu0_relative: false imu0_queue_size: 10 imu0_remove_gravitational_acceleration: true # Only if your IMU output includes gravity # ===================================================================# 2. GLOBAL EKF NODE (Outputs map -> odom)# ===================================================================ekf_filter_node_global: ros__parameters: frequency: 30.0 two_d_mode: true publish_tf: true map_frame: map odom_frame: odom base_link_frame: base_link world_frame: map # Global frame execution # Fuse Local Inputs odom0: /wheel/odometry odom0_config: [false, false, false, false, false, false, true, true, false, false, false, true, false, false, false] imu0: /imu/data imu0_config: [false, false, false, false, false, true, false, false, false, false, false, true, true, false, false] # --- Sensor 3: Transformed GPS Odometry --- odom1: /odometry/gps odom1_config: [true, true, false, # Fuses global absolute X and Y position false, false, false, false, false, false, false, false, false, false, false, false] odom1_differential: false odom1_queue_size: 10 # ===================================================================# 3. NAVSAT TRANSFORM NODE (GPS to UTM Conversion)# ===================================================================navsat_transform: ros__parameters: frequency: 10.0 delay: 3.0 magnetic_declination_radians: 0.0 # Replace with the site-specific declination if needed yaw_offset: 0.0 # Set from sensor convention: 0 if yaw is already zero when facing East zero_altitude: true broadcast_cartesian_transform: true publish_filtered_gps: true use_odometry_yaw: falseStep 3: Launch and verify TF ownership
Create a launch file whose node names match the YAML keys, with distinct output remappings for the local and global filters. Remap navsat_transform_node's filtered odometry input to the global filter output and its GPS and IMU inputs to the actual driver topics. Package example launch files may use different topics; do not assume a name alone supplies these mappings.
ros2 run tf2_tools view_framesros2 topic echo /odometry/filtered/local --field pose.covarianceros2 topic echo /odometry/filtered/global --field pose.covarianceEnsure only the local filter publishes odom → base_link and only the global filter publishes map → odom. Disable duplicate wheel-driver TF if needed. The commands above assume your launch remaps the outputs to those exact topic names.
navsat_transform expects yaw zero at east under its documented convention. Apply yaw_offset and magnetic declination based on measured sensor conventions, not a copied 90-degree offset. Set covariance from driver data and characterization; do not fuse derived wheel pose and velocity as independent evidence when they share the same measurements. Test GNSS loss, reacquisition, magnetic disturbance, and wheel slip before autonomous operation.
Further reading and technical reference
For the prerequisites, read our ROS 2 topics, coordinate frames, and node setup. For ROS 2 GPS integration and map/odom frame guidance, consult the maintained upstream robot_localization GPS integration guide. The upstream page is for ROS 2 Rolling, not a Humble/Jazzy-specific reference; this example has not been tested on either distribution. Verify parameter availability and behavior against the robot_localization version installed on your system.
