From b272af040671e318598dbda8cbdec17d7b6a4e40 Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Thu, 1 Oct 2026 12:22:41 -0600 Subject: [PATCH] fix(gnss): gate home, geofence and precision takeoff on usable samples HomePosition, the geofence on the GNSS source and the precision takeoff home check each tested the fix type on their own and took samples the estimator rejects, such as spoofed ones or those before the receiver passed its checks. They now take only samples marked usable. Home keeps its own accuracy thresholds on top. The others keep their gates: Remote ID and Open Drone ID broadcast the receiver's own solution with its accuracy whenever it has one, and the calibrations, the attitude estimator and the telemetry drivers need only a rough or reported position. The SIH home yaw test reached its condition, home set from GNSS while the local position is invalid, with 5 satellites: enough for the old fix type test, too few for EKF2. Such samples are never usable now, so home was never set. The test now gives the receiver a full constellation and turns GNSS fusion off in EKF2 (EKF2_SENS_EN). Home again comes from the receiver while the local position stays invalid, the branch of HomePosition::setHomePosition() that 282c969687 fixed to take the ground heading, and nothing later refreshes home.yaw. Assisted-by: Claude:claude-opus-5-5 --- src/modules/commander/HomePosition.cpp | 4 ++-- src/modules/commander/HomePosition.hpp | 1 - src/modules/navigator/navigator_main.cpp | 5 ++--- .../tasks/PrecTakeoffTask.cpp | 2 +- .../test/TEST_VTE_VisionTargetEst.cpp | 14 +++++++------- test/mavsdk_tests/autopilot_tester.py | 3 +++ test/mavsdk_tests/test_multicopter_home.py | 10 +++++++--- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/modules/commander/HomePosition.cpp b/src/modules/commander/HomePosition.cpp index 3d01eadaa66..c55720031d4 100644 --- a/src/modules/commander/HomePosition.cpp +++ b/src/modules/commander/HomePosition.cpp @@ -397,12 +397,12 @@ void HomePosition::update(bool set_automatically, bool check_if_changed) const hrt_abstime now = hrt_absolute_time(); const bool time_valid = now < (vehicle_gnss.timestamp + 1_s); - const bool fix_valid = vehicle_gnss.receiver.fix_type >= kHomePositionGPSRequiredFixType; const bool eph_valid = vehicle_gnss.receiver.eph < kHomePositionGPSRequiredEPH; const bool epv_valid = vehicle_gnss.receiver.epv < kHomePositionGPSRequiredEPV; const bool evh_valid = vehicle_gnss.receiver.speed_accuracy < kHomePositionGPSRequiredEVH; - _gps_position_for_home_valid = time_valid && fix_valid && eph_valid && epv_valid && evh_valid + // Home needs a sample the estimator would fuse, and on top of that the accuracy above + _gps_position_for_home_valid = time_valid && vehicle_gnss.usable && eph_valid && epv_valid && evh_valid && isGpsPositionFusionEnabled(); if (_param_com_home_en.get() && _gps_position_for_home_valid && _last_gps_timestamp != 0 && _last_baro_timestamp != 0 diff --git a/src/modules/commander/HomePosition.hpp b/src/modules/commander/HomePosition.hpp index b1dfc4d7a33..0fe75847c91 100644 --- a/src/modules/commander/HomePosition.hpp +++ b/src/modules/commander/HomePosition.hpp @@ -48,7 +48,6 @@ using namespace time_literals; -static constexpr int kHomePositionGPSRequiredFixType = 2; static constexpr float kHomePositionGPSRequiredEPH = 5.f; static constexpr float kHomePositionGPSRequiredEPV = 10.f; static constexpr float kHomePositionGPSRequiredEVH = 1.f; diff --git a/src/modules/navigator/navigator_main.cpp b/src/modules/navigator/navigator_main.cpp index e5844f239c1..1f2bfb016a0 100644 --- a/src/modules/navigator/navigator_main.cpp +++ b/src/modules/navigator/navigator_main.cpp @@ -1243,9 +1243,8 @@ void Navigator::geofence_breach_check() && hrt_elapsed_time(&_global_pos.timestamp) < 1_s; bool have_valid_position_for_breach_check = global_position_valid; - // relying on raw gps is questionable already, but at least check the basics - const bool raw_gps_valid = - hrt_elapsed_time(&_vehicle_gnss.timestamp) < 2_s && _vehicle_gnss.receiver.fix_type >= 2; + // relying on raw gps is questionable already, but at least take only samples the estimator would fuse + const bool raw_gps_valid = hrt_elapsed_time(&_vehicle_gnss.timestamp) < 2_s && _vehicle_gnss.usable; if (_geofence.getSource() == Geofence::GF_SOURCE_GPS) { current_latitude = _vehicle_gnss.receiver.latitude; diff --git a/src/modules/vision_target_estimator/tasks/PrecTakeoffTask.cpp b/src/modules/vision_target_estimator/tasks/PrecTakeoffTask.cpp index 1d65066235d..e67f84172fc 100644 --- a/src/modules/vision_target_estimator/tasks/PrecTakeoffTask.cpp +++ b/src/modules/vision_target_estimator/tasks/PrecTakeoffTask.cpp @@ -123,7 +123,7 @@ bool PrecTakeoffTask::updateHomeReference(const bool report_distance_warning) vehicle_gnss_s gnss; - if (!_vehicle_gnss_sub.copy(&gnss) || gnss.receiver.fix_type < sensor_gnss_s::FIX_TYPE_3D + if (!_vehicle_gnss_sub.copy(&gnss) || !gnss.usable || gnss.timestamp_sample == 0 || gnss.timestamp_sample > now || now - gnss.timestamp_sample >= kGnssDataTimeoutUs || !PX4_ISFINITE(gnss.receiver.latitude) || !PX4_ISFINITE(gnss.receiver.longitude)) { diff --git a/src/modules/vision_target_estimator/test/TEST_VTE_VisionTargetEst.cpp b/src/modules/vision_target_estimator/test/TEST_VTE_VisionTargetEst.cpp index 4573dfde9f6..debcc426ef2 100644 --- a/src/modules/vision_target_estimator/test/TEST_VTE_VisionTargetEst.cpp +++ b/src/modules/vision_target_estimator/test/TEST_VTE_VisionTargetEst.cpp @@ -345,8 +345,7 @@ protected: ASSERT_TRUE(_home_position_pub->publish(msg)); } - void publishUavGnssAt(double lat, double lon, float alt_amsl, hrt_abstime timestamp, - uint8_t fix_type = sensor_gnss_s::FIX_TYPE_3D) + void publishUavGnssAt(double lat, double lon, float alt_amsl, hrt_abstime timestamp, bool usable = true) { vehicle_gnss_s msg{}; msg.timestamp = timestamp; @@ -354,7 +353,8 @@ protected: msg.receiver.latitude = lat; msg.receiver.longitude = lon; msg.receiver.altitude_msl = static_cast(alt_amsl); - msg.receiver.fix_type = fix_type; + msg.receiver.fix_type = usable ? sensor_gnss_s::FIX_TYPE_3D : sensor_gnss_s::FIX_TYPE_NONE; + msg.usable = usable; ASSERT_TRUE(_uav_gnss_pub->publish(msg)); } @@ -982,7 +982,7 @@ TEST_F(VisionTargetEstTest, NewTaskClearsPreviousGnssBiasReference) } // WHY: Stale position data or a distant home could identify the wrong pad. -// WHAT: Accept home only while landed and within 5 m of a recent 3D GNSS fix. +// WHAT: Accept home only while landed and within 5 m of a recent usable GNSS sample. TEST_F(VisionTargetEstTest, PrecisionTakeoffHomeReferenceRequiresLandedAndNearby) { const double lat = 47.0; @@ -998,12 +998,12 @@ TEST_F(VisionTargetEstTest, PrecisionTakeoffHomeReferenceRequiresLandedAndNearby EXPECT_FALSE(_vte->updatePrecTakeoffHomeReference()); EXPECT_FALSE(_vte->precTakeoffHomeReferenceValid()); - // GIVEN: Landed with a nearby home, but an invalid GNSS fix. + // GIVEN: Landed with a nearby home, but a GNSS sample that is not usable. publishLandDetected(true, vte_test::advanceMicroseconds(kStepUs)); publishHomePosition(lat, lon, alt, vte_test::advanceMicroseconds(kStepUs)); - publishUavGnssAt(lat, lon, alt, vte_test::advanceMicroseconds(kStepUs), sensor_gnss_s::FIX_TYPE_NONE); + publishUavGnssAt(lat, lon, alt, vte_test::advanceMicroseconds(kStepUs), false); - // THEN: The invalid fix cannot validate the home-to-vehicle distance. + // THEN: The sample cannot validate the home-to-vehicle distance. EXPECT_FALSE(_vte->updatePrecTakeoffHomeReference()); EXPECT_FALSE(_vte->precTakeoffHomeReferenceValid()); diff --git a/test/mavsdk_tests/autopilot_tester.py b/test/mavsdk_tests/autopilot_tester.py index 82a7ad893b3..8efa43212fd 100644 --- a/test/mavsdk_tests/autopilot_tester.py +++ b/test/mavsdk_tests/autopilot_tester.py @@ -328,6 +328,9 @@ class AutopilotTester: def set_param_int(self, name: str, value: int) -> None: self._param.set_param_int(name, value) + def get_param_int(self, name: str) -> int: + return int(self._param.get_param_int(name)) + def set_param_float(self, name: str, value: float) -> None: self._param.set_param_float(name, value) diff --git a/test/mavsdk_tests/test_multicopter_home.py b/test/mavsdk_tests/test_multicopter_home.py index 809302e0976..ff08240360f 100644 --- a/test/mavsdk_tests/test_multicopter_home.py +++ b/test/mavsdk_tests/test_multicopter_home.py @@ -19,11 +19,15 @@ def test_home_yaw_captured_when_local_position_invalid(tester: AutopilotTester) tester.sleep_for(10) ground_yaw_deg = float(tester.get_attitude_euler().yaw_deg) - # Degraded GPS: good enough for the home position, too few satellites for EKF fusion, so the + # A GPS fix the estimator doesn't use: home is set from the receiver's usable samples while the # local position stays invalid. This is the exact condition under which the buggy code left # home yaw at 0. - tester.set_param_int("SIM_GPS_USED", 5) - tester.sleep_for(6) + sens_en_gnss0 = 1 + tester.set_param_int("EKF2_SENS_EN", tester.get_param_int("EKF2_SENS_EN") & ~sens_en_gnss0) + tester.set_param_int("SIM_GPS_USED", 10) + + # The receiver's samples are usable once its checks passed for GNSS_REQ_TIME (10 s by default) + tester.sleep_for(15) q = tester.get_home_position()["q"] home_yaw_deg = math.degrees(yaw_from_quaternion(q[0], q[1], q[2], q[3]))