fix(ekf2): place the global position at a manually set first origin

Before horizontal aiding is valid, setLatLonOrigin initialised the origin
but left _gpos at lat/lon (0, 0). The Earth rate, gravity and magnetic
field model are evaluated from _gpos once the origin is valid, so an
origin set on the ground before a GNSS fix or before flow/EV aiding ran
them at the equator until a later global position reset, and for a
flow-only flight never recovered. With aiding already valid, the
reprojection went through resetHorizontalPositionTo, whose delta spans
the frame switch and reported a position reset of thousands of km
although the local position did not move.

Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
Jacob Dahl authored and Mathieu Bresciani committed 2026-10-28 11:08:39 +01:00
1 parent 02d6a0619d
commit fd9fe58df8
2 files changed
+35 -3

No files matched your search

+10 -3
View File
@@ -104,14 +104,21 @@ bool Ekf::setLatLonOrigin(const double latitude, const double longitude, const f
return false;
}
if (!_local_origin_lat_lon.isInitialized() && isLocalHorizontalPositionValid()) {
// Already navigating in a local frame, use the origin to initialize global position
if (!_local_origin_lat_lon.isInitialized()) {
// Until the first origin the local frame is referenced to lat/lon (0, 0). Keep the local position
// and place the global position where the new origin puts it: the latitude feeds the Earth rate,
// gravity and magnetic field model before any position aiding, and no reset needs reporting.
const Vector2f pos_prev = getLocalHorizontalPosition();
_local_origin_lat_lon.initReference(latitude, longitude, _time_delayed_us);
double new_latitude;
double new_longitude;
_local_origin_lat_lon.reproject(pos_prev(0), pos_prev(1), new_latitude, new_longitude);
resetHorizontalPositionTo(new_latitude, new_longitude, hpos_var);
_gpos.setLatLonDeg(new_latitude, new_longitude);
_output_predictor.resetLatLonTo(new_latitude, new_longitude);
if (PX4_ISFINITE(hpos_var)) {
P.uncorrelateCovarianceSetVariance<2>(State::pos.idx, math::max(sq(0.01f), hpos_var));
}
} else {
// Simply move the origin and compute the change in local position
+25
View File
@@ -309,6 +309,8 @@ TEST_F(EkfBasicsTest, reset_ekf_global_origin_gps_uninitialized)
EXPECT_FALSE(_ekf->global_origin_valid());
const uint8_t pos_reset_count_prev = _ekf->get_posNE_reset_count();
_latitude_new = 45.0000005;
_longitude_new = 111.0000005;
_altitude_new = 1500.0;
@@ -325,8 +327,21 @@ TEST_F(EkfBasicsTest, reset_ekf_global_origin_gps_uninitialized)
EXPECT_TRUE(_ekf->global_origin_valid());
EXPECT_FALSE(_ekf->isGlobalHorizontalPositionValid());
// The global position is placed at the origin without reporting a local position reset
EXPECT_EQ(_ekf->get_posNE_reset_count(), pos_reset_count_prev);
EXPECT_NEAR(_ekf->getLatLonAlt().latitude_deg(), _latitude_new, 1e-5);
EXPECT_NEAR(_ekf->getLatLonAlt().longitude_deg(), _longitude_new, 1e-5);
_sensor_simulator.runSeconds(1);
// Earth rate and gravity are evaluated at the origin latitude, not at lat/lon (0, 0)
const double lat_rad = math::radians(_latitude_new);
EXPECT_NEAR(_ekf->getGravityMss(), LatLonAlt::Wgs84::gravity(lat_rad), 1e-4f);
const Vector3f earth_rate = _ekf->getEarthRateNED();
EXPECT_NEAR(earth_rate(0), static_cast<double>(CONSTANTS_EARTH_SPIN_RATE) * cos(lat_rad), 1e-9);
EXPECT_NEAR(earth_rate(1), 0.f, 1e-9);
EXPECT_NEAR(earth_rate(2), -static_cast<double>(CONSTANTS_EARTH_SPIN_RATE) * sin(lat_rad), 1e-9);
// After the change of origin, the pos and vel innovations should stay small
EXPECT_NEAR(_ekf->aid_src_gnss_pos().test_ratio[0], 0.f, 0.05f);
EXPECT_NEAR(_ekf->aid_src_gnss_pos().test_ratio[1], 0.f, 0.05f);
@@ -355,6 +370,9 @@ TEST_F(EkfBasicsTest, global_position_from_local_ev)
_longitude_new = 111.0000005;
_altitude_new = 1500.0;
const Vector3f lpos_prev = _ekf->getPosition();
const uint8_t pos_reset_count_prev = _ekf->get_posNE_reset_count();
// BUT WHEN: the global origin is set (manually)
_ekf->setEkfGlobalOrigin(_latitude_new, _longitude_new, _altitude_new);
@@ -362,6 +380,13 @@ TEST_F(EkfBasicsTest, global_position_from_local_ev)
EXPECT_TRUE(_ekf->global_origin_valid());
EXPECT_TRUE(_ekf->isGlobalHorizontalPositionValid());
EXPECT_TRUE(_ekf->isLocalHorizontalPositionValid());
// AND: the local position is unchanged, no reset is reported and the global position is at the origin
EXPECT_NEAR(_ekf->getPosition()(0), lpos_prev(0), 1e-3f);
EXPECT_NEAR(_ekf->getPosition()(1), lpos_prev(1), 1e-3f);
EXPECT_EQ(_ekf->get_posNE_reset_count(), pos_reset_count_prev);
EXPECT_NEAR(_ekf->getLatLonAlt().latitude_deg(), _latitude_new, 1e-5);
EXPECT_NEAR(_ekf->getLatLonAlt().longitude_deg(), _longitude_new, 1e-5);
}
TEST_F(EkfBasicsTest, global_position_from_opt_flow)