diff --git a/src/modules/ekf2/EKF/ekf_helper.cpp b/src/modules/ekf2/EKF/ekf_helper.cpp index 2ad5d2b9581..7d9a2c75b67 100644 --- a/src/modules/ekf2/EKF/ekf_helper.cpp +++ b/src/modules/ekf2/EKF/ekf_helper.cpp @@ -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 diff --git a/src/modules/ekf2/test/test_EKF_basics.cpp b/src/modules/ekf2/test/test_EKF_basics.cpp index d4b26d58582..89b18d023d4 100644 --- a/src/modules/ekf2/test/test_EKF_basics.cpp +++ b/src/modules/ekf2/test/test_EKF_basics.cpp @@ -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(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(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)