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 3bce65f5fe3..9cd4de2614a 100644 --- a/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp +++ b/src/modules/ekf2/EKF/aid_sources/gnss/gps_control.cpp @@ -43,7 +43,7 @@ void Ekf::controlGpsFusion(const imuSample &imu_delayed) { _fc.gps.available = (_params.ekf2_gps_ctrl != 0); - if (!_gps_buffer || !_fc.gps.intended()) { + if (!_gps_buffer) { stopGnssFusion(); return; } @@ -52,11 +52,15 @@ void Ekf::controlGpsFusion(const imuSample &imu_delayed) _yawEstimator.setGyroBias(getGyroBias(), _control_status.flags.vehicle_at_rest); } - // run EKF-GSF yaw estimator once per imu_delayed update _yawEstimator.predict(imu_delayed.delta_ang, imu_delayed.delta_ang_dt, imu_delayed.delta_vel, imu_delayed.delta_vel_dt, (_control_status.flags.in_air && !_control_status.flags.vehicle_at_rest)); + if (!_fc.gps.intended()) { + stopGnssFusion(); + return; + } + _gps_intermittent = !isNewestSampleRecent(_time_last_gps_buffer_push, 2 * GNSS_MAX_INTERVAL); // check for arrival of new sensor data at the fusion time horizon @@ -387,6 +391,26 @@ void Ekf::controlGnssYawEstimator(estimator_aid_source3d_s &aid_src_vel) _yawEstimator.fuseVelocity(vel_xy, vel_accuracy, _control_status.flags.in_air); + if (_yawEstimator.isActive()) { + if (_time_yaw_estimator_activated_us == 0) { + _time_yaw_estimator_activated_us = _time_delayed_us; + _yaw_estimator_restarted_in_air = _control_status.flags.in_air + && _yaw_estimator_was_active_in_air; + } + + if (_control_status.flags.in_air) { + _yaw_estimator_was_active_in_air = true; + } + + } else { + _time_yaw_estimator_activated_us = 0; + } + + if (!_control_status.flags.in_air) { + _yaw_estimator_was_active_in_air = false; + _yaw_estimator_restarted_in_air = false; + } + // Try to align yaw using estimate if available if (((_params.ekf2_gps_ctrl & static_cast(GnssCtrl::VEL)) || (_params.ekf2_gps_ctrl & static_cast(GnssCtrl::HPOS))) @@ -475,6 +499,7 @@ void Ekf::stopGnssFusion() #endif // CONFIG_EKF2_GNSS_YAW _yawEstimator.reset(); + _time_yaw_estimator_activated_us = 0; } void Ekf::stopGnssVelFusion() @@ -524,6 +549,12 @@ bool Ekf::isYawFailure() const return false; } + if (_yaw_estimator_restarted_in_air + && ((_time_yaw_estimator_activated_us == 0) + || !isTimedOut(_time_yaw_estimator_activated_us, _params.EKFGSF_min_active_time))) { + return false; + } + const float euler_yaw = getEulerYaw(_R_to_earth); const float yaw_error = wrap_pi(euler_yaw - _yawEstimator.getYaw()); diff --git a/src/modules/ekf2/EKF/common.h b/src/modules/ekf2/EKF/common.h index d6c7ecd58b0..9a2e1d42277 100644 --- a/src/modules/ekf2/EKF/common.h +++ b/src/modules/ekf2/EKF/common.h @@ -394,6 +394,7 @@ struct parameters { float ekf2_gsf_tas{15.0f}; ///< default airspeed value assumed during fixed wing flight if no airspeed measurement available (m/s) const unsigned EKFGSF_reset_delay{1000000}; ///< Number of uSec of bad innovations on main filter in immediate post-takeoff phase before yaw is reset to EKF-GSF value const float EKFGSF_yaw_err_max{0.262f}; ///< Composite yaw 1-sigma uncertainty threshold used to check for convergence (rad) + const unsigned EKFGSF_min_active_time{10'000'000}; ///< Minimum period of continuous EKF-GSF velocity fusion after an in-flight restart #endif // CONFIG_EKF2_GNSS diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index 68c65e56d38..591d6358a91 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -969,6 +969,10 @@ private: // yaw estimator instance EKFGSF_yaw _yawEstimator{}; + uint64_t _time_yaw_estimator_activated_us{0}; + bool _yaw_estimator_was_active_in_air{false}; + bool _yaw_estimator_restarted_in_air{false}; + #endif // CONFIG_EKF2_GNSS #if defined(CONFIG_EKF2_RANGING_BEACON)