From 28b4cd0579885f94f2a6c44b40fcf1006649053c Mon Sep 17 00:00:00 2001 From: bresch Date: Fri, 5 Aug 2022 18:02:34 +0200 Subject: [PATCH] ekf2: move GNSS hgt control to file --- src/modules/ekf2/CMakeLists.txt | 1 + src/modules/ekf2/EKF/CMakeLists.txt | 1 + src/modules/ekf2/EKF/control.cpp | 43 ------ src/modules/ekf2/EKF/ekf_helper.cpp | 53 -------- src/modules/ekf2/EKF/gnss_height_control.cpp | 135 +++++++++++++++++++ 5 files changed, 137 insertions(+), 96 deletions(-) create mode 100644 src/modules/ekf2/EKF/gnss_height_control.cpp diff --git a/src/modules/ekf2/CMakeLists.txt b/src/modules/ekf2/CMakeLists.txt index 95dbd5b9dd..5d85e494c0 100644 --- a/src/modules/ekf2/CMakeLists.txt +++ b/src/modules/ekf2/CMakeLists.txt @@ -59,6 +59,7 @@ px4_add_module( EKF/estimator_interface.cpp EKF/ev_height_control.cpp EKF/fake_pos_control.cpp + EKF/gnss_height_control.cpp EKF/gps_checks.cpp EKF/gps_control.cpp EKF/gps_fusion.cpp diff --git a/src/modules/ekf2/EKF/CMakeLists.txt b/src/modules/ekf2/EKF/CMakeLists.txt index aeb87d2c46..e7bc1d669c 100644 --- a/src/modules/ekf2/EKF/CMakeLists.txt +++ b/src/modules/ekf2/EKF/CMakeLists.txt @@ -44,6 +44,7 @@ add_library(ecl_EKF estimator_interface.cpp ev_height_control.cpp fake_pos_control.cpp + gnss_height_control.cpp gps_checks.cpp gps_control.cpp gps_fusion.cpp diff --git a/src/modules/ekf2/EKF/control.cpp b/src/modules/ekf2/EKF/control.cpp index a9cb737415..5d07241ce2 100644 --- a/src/modules/ekf2/EKF/control.cpp +++ b/src/modules/ekf2/EKF/control.cpp @@ -685,49 +685,6 @@ bool Ekf::isConditionalRangeAidSuitable() return is_range_aid_suitable; } -void Ekf::controlGnssHeightFusion() -{ - if (!(_params.gnss_ctrl & GnssCtrl::VPOS)) { - stopGpsHgtFusion(); - return; - } - - _gps_hgt_b_est.predict(_dt_ekf_avg); - - if (_gps_data_ready) { - const bool continuing_conditions_passing = !_gps_intermittent && _gps_checks_passed && _NED_origin_initialised; - const bool starting_conditions_passing = continuing_conditions_passing; - - if (_control_status.flags.gps_hgt) { - if (continuing_conditions_passing) { - /* fuseGpsHgt(); */ // Done in fuseGpsPos - - const bool is_fusion_failing = isTimedOut(_aid_src_gnss_pos.time_last_fuse[2], _params.hgt_fusion_timeout_max); - - if (isHeightResetRequired()) { - // All height sources are failing - resetHeightToGps(); - resetVerticalVelocityToGps(_gps_sample_delayed); - - } else if (is_fusion_failing) { - // Some other height source is still working - stopGpsHgtFusion(); - } - - } else { - stopGpsHgtFusion(); - } - } else { - if (starting_conditions_passing) { - startGpsHgtFusion(); - } - } - - } else if (_control_status.flags.gps_hgt && _gps_intermittent) { - stopGpsHgtFusion(); - } -} - void Ekf::controlRangeHeightFusion() { if (!((_params.rng_ctrl == RngCtrl::CONDITIONAL) || (_params.rng_ctrl == RngCtrl::ENABLED))) { diff --git a/src/modules/ekf2/EKF/ekf_helper.cpp b/src/modules/ekf2/EKF/ekf_helper.cpp index 1e6399179e..79a3c978f5 100644 --- a/src/modules/ekf2/EKF/ekf_helper.cpp +++ b/src/modules/ekf2/EKF/ekf_helper.cpp @@ -251,23 +251,6 @@ void Ekf::resetVerticalPositionTo(const float new_vert_pos) _time_last_hgt_fuse = _time_last_imu; } -void Ekf::resetHeightToGps() -{ - ECL_INFO("reset height to GPS"); - _information_events.flags.reset_hgt_to_gps = true; - - resetVerticalPositionTo(-(_gps_sample_delayed.hgt - getEkfGlobalOriginAltitude() - _gps_hgt_b_est.getBias())); - - // the state variance is the same as the observation - P.uncorrelateCovarianceSetVariance<1>(9, getGpsHeightVariance()); - - _baro_b_est.setBias(_baro_b_est.getBias() + _state_reset_status.posD_change); - _rng_hgt_b_est.setBias(_rng_hgt_b_est.getBias() + _state_reset_status.posD_change); - _ev_hgt_b_est.setBias(_ev_hgt_b_est.getBias() - _state_reset_status.posD_change); - - _aid_src_gnss_pos.time_last_fuse[2] = _time_last_imu; -} - void Ekf::resetHeightToRng() { ECL_INFO("reset height to RNG"); @@ -1273,42 +1256,6 @@ void Ekf::startMag3DFusion() } } -void Ekf::startGpsHgtFusion() -{ - if (!_control_status.flags.gps_hgt) { - - if (_params.height_sensor_ref == HeightSensor::GNSS) { - _gps_hgt_b_est.reset(); - _height_sensor_ref = HeightSensor::GNSS; - resetHeightToGps(); - - } else { - _gps_hgt_b_est.setBias(_state.pos(2) + (_gps_sample_delayed.hgt - getEkfGlobalOriginAltitude())); - - // Reset the timeout value here because the fusion isn't done at the same place and would immediately trigger a timeout - _aid_src_gnss_pos.time_last_fuse[2] = _time_last_imu; - } - - _control_status.flags.gps_hgt = true; - _gps_hgt_b_est.setFusionActive(); - ECL_INFO("starting GPS height fusion"); - } -} - -void Ekf::stopGpsHgtFusion() -{ - if (_control_status.flags.gps_hgt) { - - if (_height_sensor_ref == HeightSensor::GNSS) { - _height_sensor_ref = HeightSensor::UNKNOWN; - } - - _control_status.flags.gps_hgt = false; - _gps_hgt_b_est.setFusionInactive(); - ECL_INFO("stopping GPS height fusion"); - } -} - void Ekf::startRngHgtFusion() { if (!_control_status.flags.rng_hgt) { diff --git a/src/modules/ekf2/EKF/gnss_height_control.cpp b/src/modules/ekf2/EKF/gnss_height_control.cpp new file mode 100644 index 0000000000..5f30e60669 --- /dev/null +++ b/src/modules/ekf2/EKF/gnss_height_control.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** + * + * Copyright (c) 2022 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file gnss_height_control.cpp + * Control functions for ekf GNSS height fusion + */ + +#include "ekf.h" + +void Ekf::controlGnssHeightFusion() +{ + if (!(_params.gnss_ctrl & GnssCtrl::VPOS)) { + stopGpsHgtFusion(); + return; + } + + _gps_hgt_b_est.predict(_dt_ekf_avg); + + if (_gps_data_ready) { + const bool continuing_conditions_passing = !_gps_intermittent && _gps_checks_passed && _NED_origin_initialised; + const bool starting_conditions_passing = continuing_conditions_passing; + + if (_control_status.flags.gps_hgt) { + if (continuing_conditions_passing) { + /* fuseGpsHgt(); */ // Done in fuseGpsPos + + const bool is_fusion_failing = isTimedOut(_aid_src_gnss_pos.time_last_fuse[2], _params.hgt_fusion_timeout_max); + + if (isHeightResetRequired()) { + // All height sources are failing + resetHeightToGps(); + resetVerticalVelocityToGps(_gps_sample_delayed); + + } else if (is_fusion_failing) { + // Some other height source is still working + stopGpsHgtFusion(); + } + + } else { + stopGpsHgtFusion(); + } + } else { + if (starting_conditions_passing) { + startGpsHgtFusion(); + } + } + + } else if (_control_status.flags.gps_hgt && _gps_intermittent) { + stopGpsHgtFusion(); + } +} + +void Ekf::startGpsHgtFusion() +{ + if (!_control_status.flags.gps_hgt) { + + if (_params.height_sensor_ref == HeightSensor::GNSS) { + _gps_hgt_b_est.reset(); + _height_sensor_ref = HeightSensor::GNSS; + resetHeightToGps(); + + } else { + _gps_hgt_b_est.setBias(_state.pos(2) + (_gps_sample_delayed.hgt - getEkfGlobalOriginAltitude())); + + // Reset the timeout value here because the fusion isn't done at the same place and would immediately trigger a timeout + _aid_src_gnss_pos.time_last_fuse[2] = _time_last_imu; + } + + _control_status.flags.gps_hgt = true; + _gps_hgt_b_est.setFusionActive(); + ECL_INFO("starting GPS height fusion"); + } +} + +void Ekf::resetHeightToGps() +{ + ECL_INFO("reset height to GPS"); + _information_events.flags.reset_hgt_to_gps = true; + + resetVerticalPositionTo(-(_gps_sample_delayed.hgt - getEkfGlobalOriginAltitude() - _gps_hgt_b_est.getBias())); + + // the state variance is the same as the observation + P.uncorrelateCovarianceSetVariance<1>(9, getGpsHeightVariance()); + + _baro_b_est.setBias(_baro_b_est.getBias() + _state_reset_status.posD_change); + _rng_hgt_b_est.setBias(_rng_hgt_b_est.getBias() + _state_reset_status.posD_change); + _ev_hgt_b_est.setBias(_ev_hgt_b_est.getBias() - _state_reset_status.posD_change); + + _aid_src_gnss_pos.time_last_fuse[2] = _time_last_imu; +} + +void Ekf::stopGpsHgtFusion() +{ + if (_control_status.flags.gps_hgt) { + + if (_height_sensor_ref == HeightSensor::GNSS) { + _height_sensor_ref = HeightSensor::UNKNOWN; + } + + _control_status.flags.gps_hgt = false; + _gps_hgt_b_est.setFusionInactive(); + ECL_INFO("stopping GPS height fusion"); + } +}