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 <dahl.jakejacob@gmail.com>

* 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 <dahl.jakejacob@gmail.com>

---------

Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
Jacob Dahl authored and GitHub committed 2026-05-26 13:52:17 -06:00
1 parent ad26499a36
commit 3ebc6d6a40
3 files changed
+15 -2

No files matched your search

+1 -1
View File
@@ -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
+13
View File
@@ -56,6 +56,10 @@
#include <ekf_derivation/generated/state.h>
#if defined(CONFIG_EKF2_TERRAIN)
# include <ekf_derivation/generated/compute_hagl_innov_var.h>
#endif // CONFIG_EKF2_TERRAIN
#include <uORB/topics/estimator_aid_source1d.h>
#include <uORB/topics/estimator_aid_source2d.h>
#include <uORB/topics/estimator_aid_source3d.h>
@@ -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)
+1 -1
View File
@@ -1760,7 +1760,7 @@ void EKF2::PublishLocalPosition(const hrt_abstime &timestamp)
// 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;