Tutorials

AI-generated illustration of a compact wheeled robot on a dark map plane, with a teal estimated path, a gold ground-truth path, a translucent uncertainty ellipse, and a scatter of faint GNSS points. Simulation only.
Tutorial

How I built an 8-state EKF that fuses wheel odometry, IMU, and simulated GNSS

A portfolio, simulation-only walkthrough of ekf-gnss-localization: an 8-state EKF that fuses wheel odometry, a z-gyro and forward accelerometer, and simulated WGS84 GNSS, scored against scripted ground truth. How to build the C++17 core, run the tests, and what the WASM browser demo and optional ROS 2 Humble package actually show, including the honest gaps.

Arpan Bom9 min read
ShareXLinkedIn

This is a simulation only

This is a portfolio demo, not a production localization stack and not a test on a real vehicle. The plant is a scripted unicycle with simulated WGS84 GNSS (white noise, Gauss-Markov bias, dropouts, and multipath), a z-gyro and forward accelerometer, and wheel odometry. It has never been run on hardware, it is not RTK, it is not Autoware, and it is not certified. I built an 8-state extended Kalman filter that fuses those simulated sensors against scripted ground truth, with RMSE and NEES checks behind the numbers I quote below. I did not invent the EKF.

Try the live demo

The fastest way to see it working is the browser demo: https://applycoding.github.io/ekf-gnss-localization/

The page title is "EKF GNSS localization (simulation)," and the header subtitle is "Simulation-only portfolio demo." The page loads the same C++ filter through a WASM module, not a TypeScript rewrite of the EKF. You get a scenario menu (matched, urban, and urban_white_model), a seed field, a GNSS-gate checkbox, a Run button, a metrics table for EKF, GNSS-only, dead-reckoning, and gate-off RMSE, and three Canvas plots (map, error, and NEES). TypeScript is UI only.

The GitHub Pages demo of ekf-gnss-localization after a completed matched seed 1 run, with the simulation-only subtitle, RMSE table, map, error, and NEES canvases. Simulation only.

Image: the GitHub Pages demo after a completed matched seed 1 run. Simulation only. applycoding.com.

What the project is

ekf-gnss-localization (repo: applycoding/ekf-gnss-localization, commit 955d67e) is a simulation-only GNSS/INS-style localization demo. The stack is:

Layer Role
ekf_core C++17 8-state EKF, initializer, and WGS84 geodesy. No heap, no ROS, and no JSON in this library, as checked by tools/check_no_heap.py.
ekf_sim Scripted trajectory, sensor models, metrics, and baselines. Links nlohmann/json.
ekf_cli Native CLI used for traces and WASM/native parity.
web/ TypeScript UI and Canvas 2D, running the same C++ through WASM (emsdk 7.0.0).
ros2/ekf_gnss_demo Optional ROS 2 Humble demo package. It is not required for native ctest.

This is an original implementation of well-known methods: an extended Kalman filter with Joseph-form updates, chi-square gating, and WGS84/ENU conversions, inspired by the projects credited in REFERENCES.md and NOTICE (robot_localization, PythonRobotics, TinyEKF, GeographicLib/pymap3d, and filterpy). I did not copy those sources.

The eight states are east and north position, heading, speed, gyro bias, wheel scale, and a 2D GNSS bias. Heading and gyro bias become observable from GNSS while moving; at rest, heading is not observable and is initialized from GNSS displacement. First-order process-noise and Euler predict steps are validated at 100 Hz (dt at most 0.02 s).

Initialization, gates, and reacquisition

Initialization is a small state machine (NoFix to Collecting to Initialized). Straight-segment heading change is tracked as the net integrated gyro over the window, integral of omega_m dt, not the integral of its absolute value. That net-gyro test is why initialization is slower than the plan's 8.6 to 8.7 s window. DEVIATIONS.md reports the measured init time on seed 1 as 9.1 s on the matched scenario and 9.0 s on urban.

GNSS updates can be chi-square gated. After 20 consecutive gated fixes, reacquisition checks that those innovations are clustered, then inflates the position covariance and applies a single correction built from the component-wise average of that 20-sample window (the code stores this as med; PLAN.md calls it a median). That is a heuristic, not a textbook EKF step. The tests require at least one reacquisition after a +20 m east injection at t = 30 s, and zero reacquisitions on unmodified seeds.

Build the C++ core and run the tests

CI uses g++ 13 (the core and web jobs on this SHA logged g++-13 (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0). Locally:

cmake --preset default && cmake --build --preset default && ctest --preset default
python3 tools/check_no_heap.py

The core GitHub Actions job also reruns the mc-labelled Monte Carlo tests, runs the static no-heap checker, and runs tools/o0_o2_parity.sh. It sets EKF_REQUIRE_WASM_PARITY=1, so Determinism.WasmMatchesNativeParity fails rather than skips if the WASM artifact cannot be compared.

Run the browser demo locally

source emsdk/emsdk_env.sh
cmake --preset wasm && cmake --build --preset wasm
cp build/wasm/ekf.js build/wasm/ekf.wasm web/public/
cd web && npm ci && npx tsc --noEmit && npx vitest run && npm run build

Vite base is /ekf-gnss-localization/. The web CI job also runs node tools/wasm_native_parity.mjs after the WASM rebuild, then Playwright.

WASM versus native parity covers matched seeds 1 to 5 plus urban seed 1: RMSE within 1e-6 m, identical GNSS gate accept/reject decisions per emitted epoch, and the full state trace (ee, en, heading, speed, gyro bias, wheel scale) within 1e-6. When EKF_REQUIRE_WASM_PARITY=1, as in CI, a missing or mismatched WASM build fails the test instead of skipping it.

Results from the tests

On commit 955d67e, core GitHub Actions run 43028061864 reported 100% tests passed, 0 tests failed out of 30, then 100% tests passed, 0 tests failed out of 6 for the mc label, then a clean tools/check_no_heap.py and a passing tools/o0_o2_parity.sh. That suite includes frozen predict/update checks, Jacobian finite differences, geodesy anchors, invalid-input rollback, GNSS and wheel gates, matched and urban criteria on seeds 1 to 5, a gyro-bias mutant, reacquisition, Monte Carlo NEES and rejection-rate tests, bit-identical traces for a fixed seed, WASM/native parity, and NoHeapOperator.FilterStepsWithoutAlloc.

The asserted matched bounds in cpp/test/test_scenario.cpp include horizontal RMSE below 1.0 m and below half the GNSS-only baseline, gyro-bias error at 40 s below 0.004 rad/s, dead-reckoning error at 90 s above 20 m, and EKF error at 90 s at most 4.0 m. Urban bounds include horizontal RMSE below 4.0 m and below half the gate-off EKF baseline, dropout max at most 8.0 m, and wheel-scale error at most 0.015 around 1.02. Scenario.MatchedGyroBiasMutantFailsFor01 disables gyro-bias estimation and requires the 0.01 rad/s case to fail that 0.004 rad/s check. MonteCarlo.UrbanWhiteModelFailsNeessN50 requires the white-noise GNSS model, under Gauss-Markov truth, to fail the NEES regression check.

The Monte Carlo NEES bounds are engineering regression checks, not formal chi-squared hypothesis tests. The urban NEES result is diagnostic only. The urban_white_model demo label is diagnostic: model mismatch.

Web GitHub Actions run 43028061455 used the same g++ 13 toolchain for the native CLI, printed wasm_native_parity ok, reported 3 Vitest tests passed, and reported 1 Playwright test passed.

The runtime heap test (NoHeapOperator.FilterStepsWithoutAlloc) overrides operator new and aborts on allocation during a short predict/update sequence. A release-time optimizer can remove some allocation pairs, so that test can miss them. The static checker tools/check_no_heap.py scans ekf_core sources for new, malloc, and heap containers, and it is what covers those cases.

The ROS 2 Humble demo

The optional package ros2/ekf_gnss_demo is gated by ros2-humble.yml. sim_sensor_node streams a precomputed sensor log on /sim/imu, /sim/wheel, /sim/gnss/pose, and /sim/truth. ekf_gnss_demo_node fuses those topics live, but each predict uses a fixed time step ekf::sim::kDtS, not the delta between message timestamps.

The launch test requires at least 4000 /ekf_gnss/odometry messages, all finite, with horizontal RMSE versus /sim/truth at most 1.0 m. On GitHub Actions run 43028061533 that checker printed RMSE horizontal: 0.3484 m over 10901 pairs and the launch test passed. Best-effort IMU delivery means that RMSE can vary slightly between runs, around 0.348 m, still against the 1.0 m bound. There is no robot_localization comparison in this repository, and this tutorial does not claim one.

License and references

Apache-2.0. Copyright 2026 Arpan Bom. See LICENSE and NOTICE.

NOTICE lists the fetched dependencies with versions and licenses: nlohmann/json 3.12.0 (MIT), Emscripten SDK 7.0.0, GoogleTest 1.18.0 (BSD-3-Clause), Node.js 26.13.0 (MIT), TypeScript 7.0.2 (Apache-2.0), Vite 8.3.7 (MIT), Vitest 5.1.0 (MIT), Playwright Test 1.65.0 (Apache-2.0), and @types/node 22.13.4 (MIT), plus the Humble demo packages used in ros2-humble.yml. robot_localization is named as not used. NOTICE points at REFERENCES.md for idea-only references and their licences. No third-party filter source is vendored.

How this was verified

I ran the standard portfolio-tutorial checks against commit 955d67e1f0c50e3cc3ce65f70b7d7e3f4545159b on main.

  • The GitHub repo is not a fork. gh api repos/applycoding/ekf-gnss-localization reports "fork": false.
  • All 3 commits on main have author and committer Arpan Bom (applycoding@gmail.com). git log --format=%B main has no Co-authored-by trailers. The only remote branch at this SHA is main.
  • A search of the tree found no third_party, vendor, or external directories, and no "ported from", "adapted from", or "copied from" markers in the authored source.
  • This is an original implementation of well-known EKF, gating, and geodesy methods, inspired by the projects credited above. I did not invent the EKF.
  • At 955d67e, NOTICE names the direct CMake, npm, and Humble pins with licenses, and it points at REFERENCES.md for idea-only references. The repo license is Apache-2.0. The LICENSE file is byte-identical to the official Apache-2.0 text from apache.org.
  • Commands, paths, APIs, and the numbers in this tutorial were checked against the repo at 955d67e. The 30/30 and 6/6 figures are from core GitHub Actions run 43028061864 on that commit, built with g++ 13. The web figures are from run 43028061455 (WASM/native parity ok, 3 Vitest passed, 1 Playwright passed). Humble run 43028061533 reported 0.3484 m RMSE against the 1.0 m bound. Pages deploy 43028373581 on this SHA returned HTTP 200 for the live demo. All of those runs have actor and triggering_actor applycoding. There are no cursor[bot] workflow runs.
  • Merge-gate checks at this SHA: WASM/native parity with EKF_REQUIRE_WASM_PARITY=1 (Determinism.WasmMatchesNativeParity and tools/wasm_native_parity.mjs); -O0/-O2 parity; authorship as above; and the in-tree tests that are supposed to fail did fail as designed. Scenario.MatchedGyroBiasMutantFailsFor01 disables gyro-bias estimation and requires the 0.01 rad/s case to miss the 0.004 rad/s bound. MonteCarlo.UrbanWhiteModelFailsNeessN50 requires the mismatched white-noise model to fail the NEES regression check. I did not find a recorded set of nine local mutants, or leftover mutant branches, on the repository at this SHA (the only branch is main).
  • This project is simulation only. It has not been tested on a real vehicle.

Limits

This is simulation only, on a scripted unicycle, not a vehicle dynamics model and not a real GNSS receiver. Reacquisition applies a heuristic built from the last 20 gated innovations; it is not a textbook EKF step. Initialization takes about 9.0 to 9.1 s and uses a net-gyro heading test. Monte Carlo NEES bounds are engineering regression checks, not formal chi-squared tests, and the urban NEES is diagnostic only. The ROS 2 Humble demo streams a precomputed sensor log, fuses it live with a fixed time step rather than message timestamps, and has no robot_localization comparison; its RMSE can vary slightly between runs, around 0.348 m against a 1.0 m bound. WASM versus native parity covers matched seeds 1 to 5 plus urban seed 1, including gate decisions and the full state, and it fails rather than skips when EKF_REQUIRE_WASM_PARITY=1. The runtime heap test can miss allocation pairs the optimizer removes; the static checker covers those.

How I built an 8-state EKF that fuses wheel odometry, IMU, and simulated GNSS | Apply Coding