Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

EKF-SLAM

using the Incremental Fixed-Lag Smoother

Open In Colab

This notebook demonstrates 2D Simultaneous Localization and Mapping (SLAM) using an EKF, although it is implemented using GTSAM’s IncrementalFixedLagSmoother, just using a lag of 1.

Scenario: A robot moves in a circular path, receiving noisy odometry and bearing-range measurements to landmarks.

Approach: We use a fixed-lag smoother which maintains and optimizes only a recent window of variables (defined by the SMOOTHER_LAG). Variables older than the lag are marginalized out, keeping the computational cost bounded, making it suitable for online applications. By default we set the lag to 1 here, which makes this an extended Kalman filter. But feel free to change the lag and see the fixed-lag smoother results.

1. Setup and Imports

2. Simulation and Smoother Parameters

Define parameters for the simulation environment, robot motion, noise models, and the fixed-lag smoother.

3. Generate Ground Truth Data

Create the true environment, robot path, and simulate noisy sensor readings using the simulation module.

Simulation Generated: 15 landmarks.
Simulation Generated: 51 ground truth poses and 50 odometry measurements.
Simulation Generated: 210 bearing-range measurements.

4. Fixed-Lag Smoother SLAM Implementation

Initialize Smoother and Helper Functions

We create the IncrementalFixedLagSmoother with the specified lag. We also initialize the first state (pose X(0) at time 0.0) and add it to the smoother using its update method. The update method requires factors, initial values (theta), and timestamps for the new variables being added.

Initializing IncrementalFixedLagSmoother with lag = 0.99 seconds...
Performing initial smoother update...
Initial update complete.

Main Iterative Loop

At each step k, we process the odometry measurement from X(k) to X(k+1) and all landmark measurements taken at pose X(k+1).

  1. Prepare Data: Collect new factors (NonlinearFactorGraph), initial estimates for new variables (Values), and timestamps for new variables (KeyTimestampMap) for the current step.

  2. Predict: Calculate an initial estimate for the new pose X(k+1) based on the previous estimate X(k) (retrieved from the smoother) and the odometry measurement.

  3. Initialize Landmarks: If a landmark is observed for the first time, calculate an initial estimate based on the predicted pose and the measurement, and add it to the new_values and new_timestamps.

  4. Update Smoother: Call smoother.update() with the collected factors, values, and timestamps. This incorporates the new information, performs optimization (iSAM2), and marginalizes old variables.

  5. Store Results: Retrieve the current estimate and factor graph from the smoother for visualization.

Running Incremental Fixed-Lag Smoother SLAM loop (50 steps)...
Loading...

Incremental Fixed-Lag Smoother SLAM finished.
Final number of poses in smoother state: 1
Final number of landmarks in smoother state: 5

5. Create Plotly Animation

Visualize the results using the gtsam_plotly module. The animation shows the evolution of the robot’s path estimate and mapped landmarks within the smoother’s active window. The full ground truth path is shown for reference. If plot_full_estimated_trajectory=True, the entire estimated trajectory (including marginalized poses) is shown faintly.

Generating Plotly animation...
Loading...
Plotly animation generated.
Displaying animation...
Loading...

6. Discussion

  • Fixed-Lag Smoothing: The IncrementalFixedLagSmoother successfully performed online SLAM, maintaining a bounded computational load by marginalizing variables older than the specified SMOOTHER_LAG.

  • smoother.update(): This core method efficiently integrated new measurements and optimized the active variable window using iSAM2.

  • Lag Parameter (SMOOTHER_LAG): This parameter controls the trade-off between computational cost and accuracy. A smaller lag (e.g., 1.0 * DT) acts like a filter, while a larger lag allows for more smoothing over recent history.

  • Visualization: The animation displays the estimates for variables currently within the smoother’s lag. The faint grey line (if enabled) shows the complete history of estimated poses, including those that have been marginalized out.