diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index 0343810a6a..41c5ee3df2 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -425,7 +425,6 @@ private: bool _mag_yaw_reset_req{false}; ///< true when a reset of the yaw using the magnetometer data has been requested bool _mag_decl_cov_reset{false}; ///< true after the fuseDeclination() function has been used to modify the earth field covariances after a magnetic field reset event. bool _synthetic_mag_z_active{false}; ///< true if we are generating synthetic magnetometer Z measurements - bool _non_mag_yaw_aiding_running_prev{false}; ///< true when heading is being fused from other sources that are not the magnetometer (for example EV or GPS). bool _is_yaw_fusion_inhibited{false}; ///< true when yaw sensor use is being inhibited SquareMatrix24f P{}; ///< state covariance matrix @@ -842,9 +841,6 @@ private: // control fusion of magnetometer observations void controlMagFusion(); - bool noOtherYawAidingThanMag() const; - bool otherHeadingSourcesHaveStopped(); - void checkHaglYawResetReq(); float getTerrainVPos() const { return isTerrainEstimateValid() ? _terrain_vpos : _last_on_ground_posD; } diff --git a/src/modules/ekf2/EKF/mag_control.cpp b/src/modules/ekf2/EKF/mag_control.cpp index 6b74b940a9..5033020428 100644 --- a/src/modules/ekf2/EKF/mag_control.cpp +++ b/src/modules/ekf2/EKF/mag_control.cpp @@ -88,7 +88,7 @@ void Ekf::controlMagFusion() stopMagFusion(); - if (noOtherYawAidingThanMag()) { + if (!_control_status.flags.ev_yaw && !_control_status.flags.gps_yaw) { // TODO: setting _is_yaw_fusion_inhibited to true is required to tell // fuseHeading to perform a "zero innovation heading fusion" // We should refactor it to avoid using this flag here @@ -100,17 +100,18 @@ void Ekf::controlMagFusion() return; } - _mag_yaw_reset_req |= otherHeadingSourcesHaveStopped(); _mag_yaw_reset_req |= !_control_status.flags.yaw_align; _mag_yaw_reset_req |= _mag_inhibit_yaw_reset_req; - if (noOtherYawAidingThanMag() && mag_data_ready) { + if (mag_data_ready && !_control_status.flags.ev_yaw && !_control_status.flags.gps_yaw) { + + const bool mag_enabled_previously = _control_status_prev.flags.mag_hdg || _control_status_prev.flags.mag_3D; + // Determine if we should use simple magnetic heading fusion which works better when // there are large external disturbances or the more accurate 3-axis fusion switch (_params.mag_fusion_type) { default: - - /* fallthrough */ + // FALLTHROUGH case MAG_FUSE_TYPE_AUTO: selectMagAuto(); break; @@ -127,6 +128,12 @@ void Ekf::controlMagFusion() break; } + const bool mag_enabled = _control_status.flags.mag_hdg || _control_status.flags.mag_3D; + + if (!mag_enabled_previously && mag_enabled) { + _mag_yaw_reset_req = true; + } + if (_control_status.flags.in_air) { checkHaglYawResetReq(); runInAirYawReset(mag_sample.mag); @@ -147,12 +154,6 @@ void Ekf::controlMagFusion() } } -bool Ekf::noOtherYawAidingThanMag() const -{ - // If we are using external vision data or GPS-heading for heading then no magnetometer fusion is used - return !_control_status.flags.ev_yaw && !_control_status.flags.gps_yaw; -} - void Ekf::checkHaglYawResetReq() { // We need to reset the yaw angle after climbing away from the ground to enable @@ -388,13 +389,3 @@ void Ekf::run3DMagAndDeclFusions(const Vector3f &mag) } } } - -bool Ekf::otherHeadingSourcesHaveStopped() -{ - // detect rising edge of noOtherYawAidingThanMag() - bool result = noOtherYawAidingThanMag() && _non_mag_yaw_aiding_running_prev; - - _non_mag_yaw_aiding_running_prev = !noOtherYawAidingThanMag(); - - return result; -}