sfwa/ukf
Header-only C++17 Unscented Kalman Filter library for state and parameter estimation
What it solves
When a system's behavior is nonlinear (for example, a moving vehicle or a robot's orientation), the classic Kalman filter — which assumes linear dynamics — gives poor estimates. The Unscented Kalman Filter (UKF) handles nonlinearity by propagating a small set of carefully chosen sample points through the system model, then recovering the mean and covariance from those transformed points. This library provides several UKF variants that are fast, deterministic, and small enough to run on embedded hardware, while still supporting advanced features like quaternion-based attitude (orientation) estimation.
How it works
The library is built around C++ class templates that you specialise for your own problem. You define a state vector (the things you want to track, like position, velocity, attitude) and a measurement vector (the sensor readings you get, like accelerometer, gyroscope, pressure). Each field has a key and a type, and the library stores all fields together in one contiguous Eigen column vector — key lookups happen at compile time, so there's no runtime overhead.
You also provide a process model (how the state evolves over time) and a measurement model (how the state maps to expected sensor readings). The library offers several filter cores: a standard UKF, a square-root UKF (more numerically stable), and a square-root form optimised for parameter estimation. It uses the Eigen library for linear algebra and makes heavy use of C++11/14 features to avoid dynamic memory allocations, which makes it predictable and fast on microcontrollers.
You can choose single or double precision by defining a preprocessor macro. The filter can be driven as a single step() call, or split into a prediction, innovation, and correction phase if you need to inspect internal state. Every step returns true on success and false on failure (e.g., if a covariance becomes indefinite), leaving the filter unchanged so you can recover gracefully.
Who it's for
This library is aimed at developers working on embedded systems or real-time applications that need nonlinear state estimation — for example, drone attitude estimation, robotics, or sensor fusion. It's also suitable for anyone who wants a modern, dependency-light UKF implementation with clear template-based customisation.
Highlights
- Multiple UKF variants: standard, square-root, and square-root optimised for parameter estimation.
- Transparent handling of quaternions in the state vector, crucial for attitude estimation.
- No dynamic memory allocation (when using the provided templates) — ideal for microcontrollers.
- Compile-time field lookup and a fully type-safe API.
- Single or double precision selectable at compile time.
- Graceful error handling: steps return
falseon failure and leave state untouched. - Includes example attitude estimators and benchmarks (e.g., on STM32H7 and ESP32) demonstrating real-world performance.
Related
- Project
- Project
- Project
- Project