embedded-kf Library

A lightweight, statically-allocated C library for implementing Kalman filters on resource-constrained embedded systems. It features asynchronous measurement updates and automatic code generation from JSON configurations, facilitating sensor fusion for robotics and IoT devices without dynamic memory allocation.

embedded-kf Library

Implementing Kalman filters on microcontrollers often presents a significant challenge for embedded developers. The complexity of matrix mathematics, the risks of dynamic memory allocation, and the need to synchronize data from sensors with varying sample rates can quickly complicate a project. The embedded-kf library is designed to solve these problems by providing a lightweight, C99-compliant framework specifically optimized for real-time operation in resource-constrained environments.

A Modern Approach to Filter Design

At the heart of the library is a focus on efficiency and developer experience. Unlike traditional libraries that require manual implementation of matrix operations for every new project, this library utilizes a code generation workflow. Developers define their filter parameters—such as state vectors, measurement models, and noise matrices—in a standard JSON format. A Python-based generator then processes these definitions to produce optimized .c and .h files. This ensures that the resulting code is perfectly tailored to the specific dimensions of the application, reducing overhead and minimizing the potential for manual coding errors.

Key Features for Embedded Systems

One of the most critical aspects of this library is its commitment to static allocation. In many embedded environments, especially those involving safety-critical applications or RTOS-less designs, dynamic memory allocation is avoided to prevent heap fragmentation and ensure deterministic behavior. By ensuring that all memory requirements are determined at compile-time, the library provides a reliable foundation for state estimation in drones, robotics, and navigation systems.

Real-world sensor fusion often involves dealing with asynchronous data. For instance, an Inertial Measurement Unit (IMU) might provide high-frequency acceleration data, while a GPS module provides position updates at a much slower rate. The library natively supports asynchronous measurement updates, allowing the filter to perform prediction steps and update steps independently as sensor data becomes available. This flexibility is essential for creating accurate state estimates in complex multi-sensor systems.

Technical Integration

The library is built with portability in mind, adhering to the C99 standard and utilizing a CMake-based build system. It integrates a local matrix utility library to handle the underlying linear algebra, ensuring that the core Kalman filter logic remains clean and readable.

For developers looking to integrate this into their workflow, the process is straightforward:

  1. Define: Create a JSON file describing the filter’s state and transition models.
  2. Generate: Use the provided Python script to generate the C source code.
  3. Build: Link the generated files into the application using the automatically generated CMakeLists.txt.
  4. Execute: Call the user-friendly API to feed sensor data into the filter and retrieve state estimates.

Typical Applications

The library is particularly well-suited for projects requiring real-time signal processing and state estimation. Common use cases include:

  • Robotics and Drones: Fusing IMU, magnetometer, and GPS data for orientation and position tracking.
  • IoT Devices: Filtering noisy sensor data for environmental monitoring or industrial automation.
  • Autonomous Vehicles: Implementing high-speed state estimation for control loops in constrained hardware environments.

By combining the flexibility of JSON configuration with the performance of optimized C code, this library offers a robust solution for developers needing reliable Kalman filtering without the typical implementation overhead.