From 0ebe5ad7e45044303eebf98c7201a444620333a0 Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Thu, 1 Oct 2026 12:22:02 -0600 Subject: [PATCH] feat(ekf2): report why the latest GNSS sample was not fused Commander had to infer a GNSS loss reason from the receiver's check flags and the fault flags, and could not tell a silent receiver, a rejected sample or a disabled fusion apart. EKF2 knows which of these applies, so it publishes it as estimator_status_flags.gnss_fusion_state, and publishes the flags on every change so the reason is current when the position goes. Assisted-by: Claude:claude-opus-5-5 --- msg/EstimatorStatusFlags.msg | 9 ++ plan.md | 25 ---- .../ekf2/EKF/aid_sources/gnss/gps_control.cpp | 50 +++++++- src/modules/ekf2/EKF/common.h | 10 ++ src/modules/ekf2/EKF/control.cpp | 4 + src/modules/ekf2/EKF/ekf.cpp | 2 + src/modules/ekf2/EKF/ekf.h | 9 ++ src/modules/ekf2/EKF2.cpp | 21 +++ src/modules/ekf2/EKF2.hpp | 1 + src/modules/ekf2/test/test_EKF_gps.cpp | 121 ++++++++++++++++++ 10 files changed, 226 insertions(+), 26 deletions(-) delete mode 100644 plan.md diff --git a/msg/EstimatorStatusFlags.msg b/msg/EstimatorStatusFlags.msg index 632ed2315b8..f2b350acafe 100644 --- a/msg/EstimatorStatusFlags.msg +++ b/msg/EstimatorStatusFlags.msg @@ -68,3 +68,12 @@ bool fs_bad_optflow_x # 7 - true if fusion of the optical flow X axis h bool fs_bad_optflow_y # 8 - true if fusion of the optical flow Y axis has encountered a numerical error bool fs_bad_acc_vertical # 10 - true if bad vertical accelerometer data has been detected bool fs_bad_acc_clipping # 11 - true if delta velocity data contains clipping (asymmetric railing) + +# GNSS fusion +uint8 gnss_fusion_state # [@enum GNSS_FUSION] Why the latest GNSS sample was or was not fused. Horizontal position and velocity count, height only when it is the only GNSS observation in use +uint8 GNSS_FUSION_FUSED = 0 # Fused +uint8 GNSS_FUSION_NO_DATA = 1 # No recent sample +uint8 GNSS_FUSION_UNUSABLE = 2 # The sample is marked not usable +uint8 GNSS_FUSION_REJECTED = 3 # Innovation outside the gate +uint8 GNSS_FUSION_VEL_LIMIT = 4 # Velocity above the limit of the velocity states +uint8 GNSS_FUSION_INACTIVE = 5 # Not in use: disabled, not aligned, waiting to restart, or GNSS declared faulty diff --git a/plan.md b/plan.md deleted file mode 100644 index 63ec82b07d8..00000000000 --- a/plan.md +++ /dev/null @@ -1,25 +0,0 @@ -# GNSS reporting (step 6 of #28813) - -Draft placeholder: this file is the plan and is deleted when the work lands. Stacked on #28798. Design: [RFC #28813](https://github.com/PX4/PX4-Autopilot/issues/28813), [pipeline_migration.md](https://github.com/dakejahl/botenook/blob/main/gnss/pipeline_migration.md) §2 (decisions 6, 8–10) and §4. - -## Goal - -Commander and the user learn from the new topics why GNSS is not fused and which receiver failed. The estimator-side GNSS reporting that predates those topics goes. - -## Scope - -1. **`gnss_fusion_state`**: EKF2 publishes on `estimator_status_flags` why the latest GNSS sample was or was not fused: fused, no data, unusable (`vehicle_gnss.usable` false), rejected by the innovation gate, above `EKF2_VEL_LIM`, or inactive (`EKF2_GPS_CTRL`, alignment, a declared GNSS fault). -2. **Commander reads the new topics**: pre-arm and in-flight GNSS messages come from `vehicle_gnss` (`usable`, `failed_checks`) and `sensors_status_gnss` instead of `estimator_status.gps_check_fail_flags`. The loss-reason event from #28873 takes its reason from `gnss_fusion_state` and names the receiver. -3. **Switch event**: one event per `vehicle_gnss.selection_count` change, with `selection_reason` and both receivers. -4. **Divergence in the hub**: `sensors_status_gnss.inconsistency` (horizontal distance to the selected receiver after lever arms) and `priority` (the preferred receiver). Commander's `gnss_lost` divergence test reads `inconsistency` instead of computing its own. -5. **Consumers gate on `usable`** instead of their own fix-type tests. HomePosition must; geofence, RemoteID, open_drone_id and the mag and baro calibrations are reviewed one by one. -6. **Removed**: `estimator_status.gps_check_fail_flags` and its `GPS_CHECK_FAIL_*` constants, once commander and HIGH_LATENCY2 read the new topics. - -## Out of scope - -- Per-receiver MAVLink GNSS messages, which wait for mavlink/mavlink#2146. `GPS_RAW_INT` and `GPS2_RAW` stay on fixed receivers. -- SIH tests that assert the switch event and `gnss_fusion_state`: step 7. - -## Open - -- Items 3 and 4 were planned for the selection but left out of #28798. They can move to their own PR if this one gets large. diff --git a/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp b/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp index 57b4b29cd78..248d77232cc 100644 --- a/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp +++ b/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp @@ -86,12 +86,15 @@ void Ekf::controlGpsFusion(const imuSample &imu_delayed) const bool vel_within_limit = gnss_sample.vel.isAllFinite() && (gnss_sample.vel.abs().max() <= _params.ekf2_vel_lim); - if (gnss_sample.usable && vel_within_limit) { + _gnss_sample_accepted = gnss_sample.usable && vel_within_limit; + + if (_gnss_sample_accepted) { _time_last_gnss_sample_accepted_us = _time_delayed_us; } else { // Skip this sample _gps_data_ready = false; + _gnss_fusion_state = gnss_sample.usable ? GnssFusionState::VelLimit : GnssFusionState::Unusable; const bool using_gnss = _control_status.flags.gnss_vel || _control_status.flags.gnss_pos || _control_status.flags.gps_hgt; @@ -334,6 +337,51 @@ bool Ekf::isGnssPosResetAllowed() const return allowed; } +void Ekf::updateGnssFusionState() +{ + const bool sample_accepted = _gnss_sample_accepted; + _gnss_sample_accepted = false; + + if (!_fc.gps.intended()) { + _gnss_fusion_state = GnssFusionState::Inactive; + + } else if (!isNewestSampleRecent(_time_last_gps_buffer_push, 2 * GNSS_MAX_INTERVAL)) { + _gnss_fusion_state = GnssFusionState::NoData; + + } else if (sample_accepted) { + _gnss_fusion_state = acceptedGnssSampleFusionState(); + } + + // Otherwise no sample reached the fusion time horizon, or controlGpsFusion() skipped it and set the state +} + +GnssFusionState Ekf::acceptedGnssSampleFusionState() const +{ + // Navigation needs the horizontal observations, so the height counts only when nothing else is used + const bool hpos_enabled = _params.ekf2_gps_ctrl & static_cast(GnssCtrl::HPOS); + const bool vel_enabled = _params.ekf2_gps_ctrl & static_cast(GnssCtrl::VEL); + const bool horizontal = hpos_enabled || vel_enabled; + + const bool fused = horizontal ? (_aid_src_gnss_pos.fused || _aid_src_gnss_vel.fused) : _aid_src_gnss_hgt.fused; + + if (fused) { + return GnssFusionState::Fused; + } + + const bool aligned = !horizontal || (_control_status.flags.tilt_align && _control_status.flags.yaw_align); + + if (!aligned || _control_status.flags.gnss_fault || _control_status.flags.gnss_hgt_fault) { + return GnssFusionState::Inactive; + } + + const bool rejected = horizontal + ? ((hpos_enabled && _aid_src_gnss_pos.innovation_rejected) || (vel_enabled && _aid_src_gnss_vel.innovation_rejected)) + : _aid_src_gnss_hgt.innovation_rejected; + + // Not rejected: the aid source can't start + return rejected ? GnssFusionState::Rejected : GnssFusionState::Inactive; +} + void Ekf::updateGnssVel(const imuSample &imu_sample, const gnssSample &gnss_sample, estimator_aid_source3d_s &aid_src) { // correct velocity for offset relative to IMU diff --git a/src/modules/ekf2/EKF/common.h b/src/modules/ekf2/EKF/common.h index d864f7a8ffe..009cad78462 100644 --- a/src/modules/ekf2/EKF/common.h +++ b/src/modules/ekf2/EKF/common.h @@ -155,6 +155,16 @@ enum class GnssMode : uint8_t { kDeadReckoning = 1 ///< Reset on fusion timeout if no source of velocity is availabl }; +// Why the latest GNSS sample was or was not fused, with the values of estimator_status_flags GNSS_FUSION_* +enum class GnssFusionState : uint8_t { + Fused = 0, + NoData = 1, ///< no sample within the data timeout + Unusable = 2, ///< the sensors module marked the sample not usable + Rejected = 3, ///< innovation outside the gate + VelLimit = 4, ///< velocity above EKF2_VEL_LIM + Inactive = 5 ///< not intended, not aligned, can't start, or a GNSS fault is declared +}; + enum class RngCtrl : uint8_t { DISABLED = 0, CONDITIONAL = 1, diff --git a/src/modules/ekf2/EKF/control.cpp b/src/modules/ekf2/EKF/control.cpp index 3fee37ca5bd..136bb4df0af 100644 --- a/src/modules/ekf2/EKF/control.cpp +++ b/src/modules/ekf2/EKF/control.cpp @@ -140,6 +140,10 @@ void Ekf::controlFusionModes(const imuSample &imu_delayed) controlHeightFusion(imu_delayed); +#if defined(CONFIG_EKF2_GNSS) + updateGnssFusionState(); +#endif // CONFIG_EKF2_GNSS + #if defined(CONFIG_EKF2_GRAVITY_FUSION) controlGravityFusion(imu_delayed); #endif // CONFIG_EKF2_GRAVITY_FUSION diff --git a/src/modules/ekf2/EKF/ekf.cpp b/src/modules/ekf2/EKF/ekf.cpp index 245d7647d03..dea0acb86b6 100644 --- a/src/modules/ekf2/EKF/ekf.cpp +++ b/src/modules/ekf2/EKF/ekf.cpp @@ -96,6 +96,8 @@ void Ekf::reset() _gnss_usable = false; _gnss_checks_passed_reported = false; _time_last_gnss_sample_accepted_us = 0; + _gnss_fusion_state = GnssFusionState::NoData; + _gnss_sample_accepted = false; #endif // CONFIG_EKF2_GNSS _local_origin_alt = NAN; diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index d2e7f7fd976..e4681dd0b23 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -419,6 +419,8 @@ public: bool gps_checks_passed() const { return _gnss_usable; }; + GnssFusionState getGnssFusionState() const { return _gnss_fusion_state; } + const BiasEstimator::status &getGpsHgtBiasEstimatorStatus() const { return _gps_hgt_b_est.getStatus(); } const auto &aid_src_gnss_hgt() const { return _aid_src_gnss_hgt; } @@ -616,6 +618,9 @@ private: bool _gnss_usable{false}; ///< the latest GNSS sample at the fusion time horizon was usable bool _gnss_checks_passed_reported{false}; ///< gps_checks_passed was reported since the last reset + GnssFusionState _gnss_fusion_state{GnssFusionState::NoData}; + bool _gnss_sample_accepted{false}; ///< a sample reached the fusion time horizon in this update and was not skipped + HeightBiasEstimator _gps_hgt_b_est{HeightSensor::GNSS, _height_sensor_ref}; estimator_aid_source1d_s _aid_src_gnss_hgt{}; @@ -925,6 +930,10 @@ private: bool isGnssVelResetAllowed() const; bool isGnssPosResetAllowed() const; + // After every GNSS observation of the update had its turn, height included + void updateGnssFusionState(); + GnssFusionState acceptedGnssSampleFusionState() const; + void controlGnssYawEstimator(estimator_aid_source3d_s &aid_src_vel); bool tryYawEmergencyReset(); void resetVelocityToGnss(estimator_aid_source3d_s &aid_src); diff --git a/src/modules/ekf2/EKF2.cpp b/src/modules/ekf2/EKF2.cpp index 13be194b10e..c7f02ad9a82 100644 --- a/src/modules/ekf2/EKF2.cpp +++ b/src/modules/ekf2/EKF2.cpp @@ -81,6 +81,13 @@ static uint16_t toEstimatorStatusCheckFlags(uint16_t failed_checks) return flags; } + +static_assert(static_cast(GnssFusionState::Fused) == estimator_status_flags_s::GNSS_FUSION_FUSED); +static_assert(static_cast(GnssFusionState::NoData) == estimator_status_flags_s::GNSS_FUSION_NO_DATA); +static_assert(static_cast(GnssFusionState::Unusable) == estimator_status_flags_s::GNSS_FUSION_UNUSABLE); +static_assert(static_cast(GnssFusionState::Rejected) == estimator_status_flags_s::GNSS_FUSION_REJECTED); +static_assert(static_cast(GnssFusionState::VelLimit) == estimator_status_flags_s::GNSS_FUSION_VEL_LIMIT); +static_assert(static_cast(GnssFusionState::Inactive) == estimator_status_flags_s::GNSS_FUSION_INACTIVE); #endif // CONFIG_EKF2_GNSS EKF2::EKF2(bool multi_mode, const px4::wq_config_t &config, bool replay_mode): @@ -2056,6 +2063,18 @@ void EKF2::PublishStatusFlags(const hrt_abstime ×tamp) _filter_fault_status_changes++; } +#if defined(CONFIG_EKF2_GNSS) + const uint8_t gnss_fusion_state = static_cast(_ekf.getGnssFusionState()); +#else + const uint8_t gnss_fusion_state = estimator_status_flags_s::GNSS_FUSION_INACTIVE; +#endif // CONFIG_EKF2_GNSS + + // Commander names the reason when the position goes, so a change can't wait for the next periodic update + if (gnss_fusion_state != _gnss_fusion_state) { + update = true; + _gnss_fusion_state = gnss_fusion_state; + } + if (update) { estimator_status_flags_s status_flags{}; status_flags.timestamp_sample = _ekf.time_delayed_us(); @@ -2125,6 +2144,8 @@ void EKF2::PublishStatusFlags(const hrt_abstime ×tamp) status_flags.fs_bad_acc_vertical = _ekf.fault_status_flags().bad_acc_vertical; status_flags.fs_bad_acc_clipping = _ekf.fault_status_flags().bad_acc_clipping; + status_flags.gnss_fusion_state = gnss_fusion_state; + status_flags.timestamp = _replay_mode ? timestamp : hrt_absolute_time(); _estimator_status_flags_pub.publish(status_flags); diff --git a/src/modules/ekf2/EKF2.hpp b/src/modules/ekf2/EKF2.hpp index 0f9b932ab41..724a4acbdc7 100644 --- a/src/modules/ekf2/EKF2.hpp +++ b/src/modules/ekf2/EKF2.hpp @@ -431,6 +431,7 @@ private: uint64_t _filter_control_status{0}; uint32_t _filter_fault_status{0}; + uint8_t _gnss_fusion_state{estimator_status_flags_s::GNSS_FUSION_INACTIVE}; uint32_t _filter_control_status_changes{0}; uint32_t _filter_fault_status_changes{0}; diff --git a/src/modules/ekf2/test/test_EKF_gps.cpp b/src/modules/ekf2/test/test_EKF_gps.cpp index 6b4a0ed40b0..b4e9f629254 100644 --- a/src/modules/ekf2/test/test_EKF_gps.cpp +++ b/src/modules/ekf2/test/test_EKF_gps.cpp @@ -530,3 +530,124 @@ TEST_F(EkfGpsTest, velocityAtLimitIsNotSkipped) EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion()); EXPECT_GT(_ekf->aid_src_gnss_vel().time_last_fuse, time_last_vel_fuse); } + +TEST_F(EkfGpsTest, fusionStateWhileFused) +{ + // GIVEN: an EKF that fuses GNSS + EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion()); + + // THEN: every sample is fused + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); +} + +TEST_F(EkfGpsTest, fusionStateNamesUnusableSamples) +{ + // WHEN: the sensors module marks the samples not usable + _sensor_simulator._gps.setFixType(0); + _sensor_simulator.runSeconds(1); + + // THEN: the samples are skipped as unusable, also once the fusion stopped + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Unusable); + _sensor_simulator.runSeconds(8); + EXPECT_FALSE(_ekf_wrapper.isIntendingGpsFusion()); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Unusable); + + // AND WHEN: the samples are usable again, after the strict checks' health time on the ground + _sensor_simulator._gps.setFixType(3); + _sensor_simulator.runSeconds(12); + + // THEN: they are fused again + EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion()); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); +} + +TEST_F(EkfGpsTest, fusionStateNamesMissingData) +{ + // WHEN: the receiver stops + _sensor_simulator.stopGps(); + _sensor_simulator.runSeconds(0.5); + + // THEN: the samples still in the buffer were fused + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); + + // AND: there is no data once the newest sample is a second old + _sensor_simulator.runSeconds(1); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::NoData); + + // AND WHEN: the receiver comes back before the fusion stopped + _sensor_simulator.startGps(); + _sensor_simulator.runSeconds(1); + + // THEN: its samples are fused again + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); +} + +TEST_F(EkfGpsTest, fusionStateNamesTheVelocityLimit) +{ + // GIVEN: an airborne EKF that fuses GPS with the optional quality checks disabled + _ekf->set_in_air_status(true); + _ekf->set_vehicle_at_rest(false); + _sensor_simulator._gps.setCheckMask(0); + + // WHEN: the receiver reports a velocity above EKF2_VEL_LIM + _sensor_simulator._gps.setVelocity(Vector3f(150.f, 0.f, 0.f)); + _sensor_simulator.runSeconds(1); + + // THEN: the samples are skipped for their velocity + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::VelLimit); +} + +TEST_F(EkfGpsTest, fusionStateNamesRejectedSamples) +{ + // GIVEN: an airborne EKF that fuses GNSS + _ekf->set_in_air_status(true); + _ekf->set_vehicle_at_rest(false); + _sensor_simulator.runSeconds(1); + ASSERT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); + + // WHEN: the receiver's position and velocity both jump away from the estimate + _sensor_simulator._gps.stepHorizontalPositionByMeters(Vector2f(500.f, 0.f)); + _sensor_simulator._gps.setVelocity(Vector3f(20.f, 0.f, 0.f)); + _sensor_simulator.runSeconds(1); + + // THEN: the samples are rejected by the innovation gates + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Rejected); + + // AND: once the fusion timed out, the estimate is reset to them and they are fused again + _sensor_simulator.runSeconds(8); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Fused); +} + +TEST_F(EkfGpsTest, fusionStateNamesADeclaredFault) +{ + // GIVEN: an EKF in dead reckoning mode that fuses GNSS and airspeed in flight + _ekf_wrapper.setGnssDeadReckonMode(); + _sensor_simulator._gps.setVelocity(Vector3f(1.f, 0.5f, 0.f)); + _ekf->set_in_air_status(true); + _ekf->set_vehicle_at_rest(false); + _ekf->set_is_fixed_wing(true); + _sensor_simulator.startAirspeedSensor(); + _sensor_simulator._airspeed.setData(15.f, 15.f); + _sensor_simulator.runSeconds(1); + + // WHEN: the receiver's position jumps, which the airspeed lets the EKF reject + _sensor_simulator._gps.stepHorizontalPositionByMeters(Vector2f(500.f, -1.f)); + _sensor_simulator.runSeconds(15); + + // THEN: GNSS is declared faulty and its samples are no longer used + EXPECT_TRUE(_ekf_wrapper.isGnssFaultDetected()); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Inactive); +} + +TEST_F(EkfGpsTest, fusionStateWhileDisabled) +{ + // WHEN: the GNSS fusion is disabled + _ekf_wrapper.setGpsEnabled(false); + _sensor_simulator.runSeconds(0.2); + + // THEN: GNSS is inactive, whatever the samples are + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Inactive); + _sensor_simulator._gps.setFixType(0); + _sensor_simulator.runSeconds(1); + EXPECT_EQ(_ekf->getGnssFusionState(), GnssFusionState::Inactive); +}