From 3ebc6d6a40c8aa7343adf7008fbf3d0134b9df5a Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Tue, 26 May 2026 15:52:17 -0400 Subject: [PATCH] fix(ekf2): publish HAGL variance in dist_bottom_var (#27349) * fix(ekf2): report HAGL variance for dist_bottom_var dist_bottom_var was published as P(terrain, terrain) only. When the range finder is the EKF height reference (EKF2_HGT_REF=2), the terrain state is reset to 0 and not directly observed, so this variance does not represent the actual uncertainty of dist_bottom (HAGL). Use ComputeHaglInnovVar(P, 0) to report the proper HAGL variance, which accounts for terrain state, vertical position state, and their covariance. Signed-off-by: Jacob Dahl * fix(ekf2): clamp HAGL variance to non-negative H*P*H^T should be >= 0 for a valid covariance, but float drift or mildly non-PSD P can produce a tiny negative result. Clamp at the accessor so consumers of lpos.dist_bottom_var never see a negative variance. Signed-off-by: Jacob Dahl --------- Signed-off-by: Jacob Dahl --- msg/versioned/VehicleLocalPosition.msg | 2 +- src/modules/ekf2/EKF/ekf.h | 13 +++++++++++++ src/modules/ekf2/EKF2.cpp | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/msg/versioned/VehicleLocalPosition.msg b/msg/versioned/VehicleLocalPosition.msg index 3178a00bb3c..f87436cc1e7 100644 --- a/msg/versioned/VehicleLocalPosition.msg +++ b/msg/versioned/VehicleLocalPosition.msg @@ -59,7 +59,7 @@ float32 ref_alt # Reference altitude AMSL, (metres) # Distance to surface bool dist_bottom_valid # true if distance to bottom surface is valid float32 dist_bottom # Distance from from bottom surface to ground, (metres) -float32 dist_bottom_var # terrain estimate variance (m^2) +float32 dist_bottom_var # height above ground estimate variance (m^2) float32 delta_dist_bottom # Amount of vertical shift of dist bottom estimate in latest reset [m] uint8 dist_bottom_reset_counter # Index of latest dist bottom estimate reset diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index 1382a0e4aba..6313f307dc3 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -56,6 +56,10 @@ #include +#if defined(CONFIG_EKF2_TERRAIN) +# include +#endif // CONFIG_EKF2_TERRAIN + #include #include #include @@ -109,6 +113,15 @@ public: // get the terrain variance float getTerrainVariance() const { return P(State::terrain.idx, State::terrain.idx); } + // get the variance of the height above ground (HAGL) estimate; accounts for terrain and + // vertical position uncertainty as well as their covariance + float getHaglVariance() const + { + float hagl_var = 0.f; + sym::ComputeHaglInnovVar(P, 0.f, &hagl_var); + return fmaxf(hagl_var, 0.f); + } + #endif // CONFIG_EKF2_TERRAIN #if defined(CONFIG_EKF2_RANGE_FINDER) diff --git a/src/modules/ekf2/EKF2.cpp b/src/modules/ekf2/EKF2.cpp index 58b505345c8..9cb699cdf5f 100644 --- a/src/modules/ekf2/EKF2.cpp +++ b/src/modules/ekf2/EKF2.cpp @@ -1760,7 +1760,7 @@ void EKF2::PublishLocalPosition(const hrt_abstime ×tamp) // Distance to bottom surface (ground) in meters, must be positive lpos.dist_bottom_valid = _ekf.isTerrainEstimateValid() || (_ekf.getHeightSensorRef() == HeightSensor::RANGE); lpos.dist_bottom = math::max(_ekf.getHagl(), 0.f); - lpos.dist_bottom_var = _ekf.getTerrainVariance(); + lpos.dist_bottom_var = _ekf.getHaglVariance(); _ekf.get_hagl_reset(&lpos.delta_dist_bottom, &lpos.dist_bottom_reset_counter); lpos.dist_bottom_sensor_bitfield = vehicle_local_position_s::DIST_BOTTOM_SENSOR_NONE;