A Python implementation of Feature-based Extended Kalman Filter (FEKF) localization and SLAM for a simulated 3-DOF differential-drive robot. It is built on the PRPY probabilistic robot localization library (see prpyprobabilisticrobotlocalizationpythonlibrary.pdf). The lab assignment is described in PR-LAB4, FEKFMBL.pdf.
The repository covers three steps, each building on the previous one:
- EKF localization with dead reckoning + compass. Wheel-encoder odometry is the input of the motion model, and compass (yaw) readings update the filter.
- Feature EKF Map-Based Localization (FEKFMBL). The robot knows a map of 2D point features. It observes them, matches them to the map with ICNN data association, and uses them to correct its pose.
- Feature EKF SLAM (FEKFSLAM). The map starts empty. Unmatched observations are added to the state vector as new features, so the robot builds the map and localizes itself at the same time.
Live run of MBL_3DOFDDInputDisplacementMM_2DCartesianFeatureOM.py (full video:
MP4).
Blue dots: estimated trajectory. Blue ellipses and dashed lines: feature observations.
Black ellipses: expected observations of the map features.
| Item | Value |
|---|---|
| Robot | Differential drive, wheel base 0.5 m, sampled at dt = 0.1 s
|
| Simulated state |
|
| Filter state (MBL) | |
| Filter state (SLAM) | |
| Input | Commanded velocity |
| Motion model | Displacement from the wheel encoders: |
| Sensors | Encoders (every step), compass |
| Map | 6 point features at (-40,5), (-5,40), (-5,25), (-3,50), (-20,3), (40,-40) |
On each step Localize() runs:
-
Prediction.
$\bar x_k = f(x_{k-1}, u_k)$ and$\bar P_k = A_k P_{k-1} A_k^T + W_k Q_k W_k^T$ , using the JacobiansJfxandJfwof the compounding operator$\oplus$ . -
Measurements. It reads the compass and the feature observations
${}^Bz_{F_i}$ , given in the robot frame. -
Data association (ICNN). For each map feature
$j$ , it computes the expected observation$h_{F_j} = \ominus x_B \boxplus x_{F_j}$ and its covariance. Each observation$z_{F_i}$ is paired with the nearest feature by squared Mahalanobis distance$D^2_{ij} = (z_i - h_j)^T (P_{h_j} + R_i)^{-1} (z_i - h_j)$ . A pairing is accepted only if$D^2_{ij} < \chi^2_{d,\alpha}$ (individual compatibility,$\alpha = 0.95$ for MBL and$0.99$ for SLAM). - Update. Paired features are stacked with the compass reading, and the standard EKF update is applied. The Joseph form is also implemented in EKF.py.
-
State augmentation (SLAM only). Each unpaired observation becomes a new feature,
$x_{F_{new}} = x_B \boxplus z_{F_i}$ . The covariance grows through the Jacobians$J_{1\boxplus}$ and$J_{2\boxplus}$ (FEKFSLAM.AddNewFeatures). The cross-correlations between the robot and the features are kept.
| File | Role |
|---|---|
| SimulatedRobot.py, DifferentialDriveSimulatedRobot.py | Ground-truth simulator: kinematics, encoders, compass and feature sensors |
| Pose.py, Feature.py, conversions.py |
Pose3D (⊕, ⊖ and their Jacobians), CartesianFeature / PolarFeature (⊞ and Jacobians) |
| GaussianFilter.py, KF.py, EKF.py | Generic filter classes: prediction and update |
| Localization.py, GFLocalization.py | Localization loop, logging, live plotting and result plots |
| DR_3DOFDifferentialDrive.py | Dead reckoning from the wheel encoders |
| EKF_3DOFDifferentialDriveInputDisplacement.py | EKF with displacement motion model + compass update (Part I) |
| EKF_3DOFDifferentialDriveCtVelocity.py | Constant-velocity motion model variant (optional part) |
| MapFeature.py | Feature observation model |
| FEKFMBL.py | Map-based localization: Mahalanobis distance, ICNN, stacking, plotting |
| FEKFSLAM.py, FEKFSLAMFeature.py | SLAM: state augmentation, prediction on the augmented state, mapped-feature plotting |
MBL_3DOFDD*.py |
Runnable MBL configurations (Cartesian or polar features, displacement or constant-velocity motion models) |
| FEKFSLAM_3DOFDD_InputVelocityMM_2DCartesianFeatureOM.py | Runnable SLAM configuration |
| record_video.py | Records the live animation of any demo to MP4, and saves the final figures |
| docs/, pyreverse_output/ | Sphinx documentation and UML class diagrams |
The concrete classes are put together with multiple inheritance. For example:
class MBL_3DOFDDInputDisplacementMM_2DCartesianFeatureOM(
Cartesian2DMapFeature, # observation model
FEKFMBL, # map-based localization
EKF_3DOFDifferentialDriveInputDisplacement): # motion model + compassDependencies: numpy, scipy, matplotlib, roboticstoolbox-python (used for the robot animation icon).
# Part I – EKF with dead reckoning + compass
python EKF_3DOFDifferentialDriveInputDisplacement.py
# Part II – Feature EKF map-based localization
python MBL_3DOFDDInputDisplacementMM_2DCartesianFeatureOM.py
# Part III – Feature EKF SLAM
python FEKFSLAM_3DOFDD_InputVelocityMM_2DCartesianFeatureOM.pyTo try other experiments, change the sensor rates in DifferentialDriveSimulatedRobot.__init__.
yaw_reading_frequency and xy_feature_reading_frequency are the number of simulation steps between two
readings.
python record_video.py MBL_3DOFDDInputDisplacementMM_2DCartesianFeatureOM.py --seed 1 --fps 25 --every 1
# -> media/<script>.mp4 + media/<script>_fig{1,2,3}.pngThe script renders off-screen (Agg) and grabs a frame at every animation refresh. It encodes the frames
with OpenCV. If OpenCV is not installed in the same environment as roboticstoolbox, the frames are kept
in media/<script>_frames/. Encode them afterwards from any Python that has OpenCV:
python3 record_video.py --encode media/MBL_3DOFDDInputDisplacementMM_2DCartesianFeatureOM_frames --fps 25In each figure set below, the first panel is the live map view at the end of the run, the second is the
XY trajectory (ground truth vs. estimate), and the third is the state estimate. The state plots show each
state component
5000 steps, feature observations every 50 steps, compass every 5 steps.
| XY trajectory | State, error and ±3σ |
|---|---|
![]() |
![]() |
The estimate stays on top of the ground truth for the whole run. Position errors stay within about ±0.3 m and inside the 3σ bounds, with a mean close to 0. The bounds stay bounded and shrink each time features are observed, instead of growing without limit as in dead reckoning.
Part I: EKF with compass updates (results_lab_4/)
Compass rate: none, 1 Hz (every 10 steps) and 10 Hz (every step).
| No compass | 1 Hz | 10 Hz | |
|---|---|---|---|
| Live map | ![]() |
![]() |
![]() |
| XY | ![]() |
![]() |
![]() |
| States | ![]() |
![]() |
![]() |
- No compass. This is pure dead reckoning. Heading error builds up, so the estimated circle drifts away from the true one. The position uncertainty grows without bound, to errors of tens of metres.
- 1 Hz. Heading is corrected, which strongly limits the drift. Position is still unobservable, though, so its σ keeps growing slowly.
- 10 Hz. Heading stays locked (|e_ψ| < 0.1 rad), and the trajectory almost matches the ground truth. Even so, the x/y covariance still grows, because the compass does not observe position.
Part II: map-based localization, with and without feature updates (results_lab_5/)
| No compass, no feature updates | Feature updates every 50 steps | |
|---|---|---|
| Live map | ![]() |
![]() |
| XY | ![]() |
![]() |
| States | ![]() |
![]() |
Without updates, the robot ellipse and the expected-feature ellipses (black) swell over time. Once feature updates are enabled, the full pose becomes observable, including x and y. The uncertainty then stays bounded and periodic, and it is smallest when the robot is close to the feature cluster on the left side of the map.
Part III: Feature EKF SLAM (Result_lab_5/)
The map starts empty. The rate of feature observations is varied: none, every 20 steps and every 50 steps.
| No feature observations | Every 20 steps | Every 50 steps | |
|---|---|---|---|
| Live map | ![]() |
![]() |
![]() |
| XY | ![]() |
![]() |
![]() |
| States | ![]() |
![]() |
![]() |
- No observations. The filter falls back to dead reckoning. The estimate drifts, and the uncertainty (grey ellipses) grows to tens of metres.
- With observations. Features within the 50 m sensor range are initialized and then re-observed on every lap. The robot–feature correlations let each re-observation correct the robot pose and the map together. The estimated trajectory overlaps the ground truth, and the mapped features (red ellipses) converge on the true landmarks. The feature at (40,-40) is never within range, so it is never mapped.
- Every 20 steps vs. every 50 steps. Observing more often gives tighter ±3σ bounds on the robot (about ±1 m vs. ±0.5 m on x/y) and smaller feature ellipses. Both rates stay consistent: the errors remain inside the 3σ bounds.
- J. Solà, Simultaneous localization and mapping with the extended Kalman filter, 2014.
- R. Smith, M. Self, P. Cheeseman, Estimating uncertain spatial relationships in robotics, 1990.
- J. Neira, J. D. Tardós, Data association in stochastic mapping using the joint compatibility test, IEEE T-RA, 2001.
- P. Ridao, PRPY: Probabilistic Robot Localization Python Library (included PDF).


























