Pure C altitude estimation component for embedded targets such as STM32 and ESP32.
The original Arduino/C++ library layout has been removed. The repository now exposes
a portable struct + function API with no Arduino.h dependency, no dynamic memory,
and no C++ runtime requirement.
The algorithm still uses two stages:
- Kalman filter: estimate vertical acceleration from gyro and accelerometer data
- Complementary filter: fuse barometer altitude with vertical acceleration to estimate altitude and vertical velocity
components/
altitude_estimation/
CMakeLists.txt
include/
altitude_estimation.h
src/
altitude_estimation.c
altitude_estimation_filters.c
altitude_estimation_math.c
examples/
portable/
altitude_estimation_example.c
accel_g[3]: accelerometer data inggyro_rad_s[3]: gyroscope data inrad/sbaro_altitude_m: barometer altitude inmtimestamp_us: sample timestamp inus
Sensor drivers and pressure-to-altitude conversion stay outside this component.
Copy components/altitude_estimation into your ESP-IDF
project components/ directory. The component has no external dependencies.
Example:
#include "altitude_estimation.h"
static altitude_estimation_t estimator;
void app_main(void)
{
altitude_estimation_config_t config = ALTITUDE_ESTIMATION_DEFAULT_CONFIG;
altitude_estimation_init(&estimator, &config);
altitude_estimation_reset(&estimator, 0.0f);
}Add these files to your STM32 project:
- altitude_estimation.h
- altitude_estimation.c
- altitude_estimation_filters.c
- altitude_estimation_math.c
Also make sure to:
- add
components/altitude_estimation/includeto your include path - build as C99 or newer
- link
mfor GCC/Clang toolchains
Initialization:
altitude_estimation_t estimator;
altitude_estimation_config_t config = ALTITUDE_ESTIMATION_DEFAULT_CONFIG;
altitude_estimation_init(&estimator, &config);
altitude_estimation_reset(&estimator, 0.0f);Runtime update:
altitude_estimation_status_t status = altitude_estimation_update(
&estimator,
accel_g,
gyro_rad_s,
baro_altitude_m,
timestamp_us);Notes:
- the first
altitude_estimation_update()call only seeds history and returnsALTITUDE_ESTIMATION_STATUS_NEEDS_MORE_DATA - later successful calls return
ALTITUDE_ESTIMATION_STATUS_OK - equal timestamps return
ALTITUDE_ESTIMATION_STATUS_INVALID_DELTA_TIME uint32_tmicrosecond timestamps can wrap naturally as long as the gap between consecutive samples is far below 71 minutes
Read results:
float altitude_m = altitude_estimation_get_altitude(&estimator);
float velocity_mps = altitude_estimation_get_vertical_velocity(&estimator);
float accel_mps2 = altitude_estimation_get_vertical_acceleration(&estimator);See altitude_estimation_example.c for a minimal portable example that does not depend on any board SDK.