From d87feee5b08a24f75e48fb237c15daad01374dc0 Mon Sep 17 00:00:00 2001 From: bresch Date: Fri, 5 Aug 2022 17:47:32 +0200 Subject: [PATCH] ekf2: move baro control logic to file --- src/modules/ekf2/CMakeLists.txt | 1 + src/modules/ekf2/EKF/CMakeLists.txt | 1 + src/modules/ekf2/EKF/baro_height_control.cpp | 135 +++++++++++++++++++ src/modules/ekf2/EKF/control.cpp | 51 ------- src/modules/ekf2/EKF/ekf_helper.cpp | 45 ------- 5 files changed, 137 insertions(+), 96 deletions(-) create mode 100644 src/modules/ekf2/EKF/baro_height_control.cpp diff --git a/src/modules/ekf2/CMakeLists.txt b/src/modules/ekf2/CMakeLists.txt index aca3f66b32..920c3971c5 100644 --- a/src/modules/ekf2/CMakeLists.txt +++ b/src/modules/ekf2/CMakeLists.txt @@ -48,6 +48,7 @@ px4_add_module( 3600 SRCS EKF/airspeed_fusion.cpp + EKF/baro_height_control.cpp EKF/bias_estimator.cpp EKF/control.cpp EKF/covariance.cpp diff --git a/src/modules/ekf2/EKF/CMakeLists.txt b/src/modules/ekf2/EKF/CMakeLists.txt index 9043be2eb5..9d0ba294be 100644 --- a/src/modules/ekf2/EKF/CMakeLists.txt +++ b/src/modules/ekf2/EKF/CMakeLists.txt @@ -33,6 +33,7 @@ add_library(ecl_EKF airspeed_fusion.cpp + baro_height_control.cpp bias_estimator.cpp control.cpp covariance.cpp diff --git a/src/modules/ekf2/EKF/baro_height_control.cpp b/src/modules/ekf2/EKF/baro_height_control.cpp new file mode 100644 index 0000000000..2a909c9ff7 --- /dev/null +++ b/src/modules/ekf2/EKF/baro_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 baro_height_control.cpp + * Control functions for ekf barometric height fusion + */ + +#include "ekf.h" + +void Ekf::controlBaroHeightFusion() +{ + if (!(_params.baro_ctrl == 1)) { + stopBaroHgtFusion(); + return; + } + + _baro_b_est.predict(_dt_ekf_avg); + + // check for intermittent data + const bool baro_hgt_intermittent = !isRecent(_time_last_baro, 2 * BARO_MAX_INTERVAL); + + if (_baro_data_ready) { + _baro_lpf.update(_baro_sample_delayed.hgt); + updateBaroHgt(_baro_sample_delayed, _aid_src_baro_hgt); + + const bool continuing_conditions_passing = !_baro_hgt_faulty && !baro_hgt_intermittent; + const bool starting_conditions_passing = continuing_conditions_passing; + + if (_control_status.flags.baro_hgt) { + if (continuing_conditions_passing) { + fuseBaroHgt(_aid_src_baro_hgt); + + const bool is_fusion_failing = isTimedOut(_aid_src_baro_hgt.time_last_fuse, _params.hgt_fusion_timeout_max); + + if (isHeightResetRequired()) { + // All height sources are failing + resetHeightToBaro(); + resetVerticalVelocityToZero(); + + } else if (is_fusion_failing) { + // Some other height source is still working + stopBaroHgtFusion(); + _baro_hgt_faulty = true; + } + + } else { + stopBaroHgtFusion(); + } + } else { + if (starting_conditions_passing) { + startBaroHgtFusion(); + } + } + + } else if (_control_status.flags.baro_hgt && baro_hgt_intermittent) { + // No baro data anymore. Stop until it comes back. + stopBaroHgtFusion(); + } +} + +void Ekf::startBaroHgtFusion() +{ + if (!_control_status.flags.baro_hgt) { + if (_params.height_sensor_ref == HeightSensor::BARO) { + _height_sensor_ref = HeightSensor::BARO; + resetHeightToBaro(); + + } else { + _baro_b_est.setBias(_state.pos(2) + _baro_lpf.getState()); + } + + _control_status.flags.baro_hgt = true; + _baro_b_est.setFusionActive(); + ECL_INFO("starting baro height fusion"); + } +} + +void Ekf::resetHeightToBaro() +{ + ECL_INFO("reset height to baro"); + _information_events.flags.reset_hgt_to_baro = true; + + resetVerticalPositionTo(-(_baro_sample_delayed.hgt - _baro_b_est.getBias())); + + // the state variance is the same as the observation + P.uncorrelateCovarianceSetVariance<1>(9, sq(_params.baro_noise)); + + _gps_hgt_b_est.setBias(_gps_hgt_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); +} + +void Ekf::stopBaroHgtFusion() +{ + if (_control_status.flags.baro_hgt) { + if (_height_sensor_ref == HeightSensor::BARO) { + _height_sensor_ref = HeightSensor::UNKNOWN; + } + + _control_status.flags.baro_hgt = false; + _baro_b_est.setFusionInactive(); + ECL_INFO("stopping baro height fusion"); + } +} diff --git a/src/modules/ekf2/EKF/control.cpp b/src/modules/ekf2/EKF/control.cpp index 8c61068b65..1ed7f7cfd5 100644 --- a/src/modules/ekf2/EKF/control.cpp +++ b/src/modules/ekf2/EKF/control.cpp @@ -759,57 +759,6 @@ bool Ekf::isConditionalRangeAidSuitable() return is_range_aid_suitable; } -void Ekf::controlBaroHeightFusion() -{ - if (!(_params.baro_ctrl == 1)) { - stopBaroHgtFusion(); - return; - } - - _baro_b_est.predict(_dt_ekf_avg); - - // check for intermittent data - const bool baro_hgt_intermittent = !isRecent(_time_last_baro, 2 * BARO_MAX_INTERVAL); - - if (_baro_data_ready) { - _baro_lpf.update(_baro_sample_delayed.hgt); - updateBaroHgt(_baro_sample_delayed, _aid_src_baro_hgt); - - const bool continuing_conditions_passing = !_baro_hgt_faulty && !baro_hgt_intermittent; - const bool starting_conditions_passing = continuing_conditions_passing; - - if (_control_status.flags.baro_hgt) { - if (continuing_conditions_passing) { - fuseBaroHgt(_aid_src_baro_hgt); - - const bool is_fusion_failing = isTimedOut(_aid_src_baro_hgt.time_last_fuse, _params.hgt_fusion_timeout_max); - - if (isHeightResetRequired()) { - // All height sources are failing - resetHeightToBaro(); - resetVerticalVelocityToZero(); - - } else if (is_fusion_failing) { - // Some other height source is still working - stopBaroHgtFusion(); - _baro_hgt_faulty = true; - } - - } else { - stopBaroHgtFusion(); - } - } else { - if (starting_conditions_passing) { - startBaroHgtFusion(); - } - } - - } else if (_control_status.flags.baro_hgt && baro_hgt_intermittent) { - // No baro data anymore. Stop until it comes back. - stopBaroHgtFusion(); - } -} - void Ekf::controlGnssHeightFusion() { if (!(_params.gnss_ctrl & GnssCtrl::VPOS)) { diff --git a/src/modules/ekf2/EKF/ekf_helper.cpp b/src/modules/ekf2/EKF/ekf_helper.cpp index 34c0b7fb9a..5457d5e480 100644 --- a/src/modules/ekf2/EKF/ekf_helper.cpp +++ b/src/modules/ekf2/EKF/ekf_helper.cpp @@ -251,21 +251,6 @@ void Ekf::resetVerticalPositionTo(const float new_vert_pos) _time_last_hgt_fuse = _time_last_imu; } -void Ekf::resetHeightToBaro() -{ - ECL_INFO("reset height to baro"); - _information_events.flags.reset_hgt_to_baro = true; - - resetVerticalPositionTo(-(_baro_sample_delayed.hgt - _baro_b_est.getBias())); - - // the state variance is the same as the observation - P.uncorrelateCovarianceSetVariance<1>(9, sq(_params.baro_noise)); - - _gps_hgt_b_est.setBias(_gps_hgt_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); -} - void Ekf::resetHeightToGps() { ECL_INFO("reset height to GPS"); @@ -1303,36 +1288,6 @@ void Ekf::startMag3DFusion() } } -void Ekf::startBaroHgtFusion() -{ - if (!_control_status.flags.baro_hgt) { - if (_params.height_sensor_ref == HeightSensor::BARO) { - _height_sensor_ref = HeightSensor::BARO; - resetHeightToBaro(); - - } else { - _baro_b_est.setBias(_state.pos(2) + _baro_lpf.getState()); - } - - _control_status.flags.baro_hgt = true; - _baro_b_est.setFusionActive(); - ECL_INFO("starting baro height fusion"); - } -} - -void Ekf::stopBaroHgtFusion() -{ - if (_control_status.flags.baro_hgt) { - if (_height_sensor_ref == HeightSensor::BARO) { - _height_sensor_ref = HeightSensor::UNKNOWN; - } - - _control_status.flags.baro_hgt = false; - _baro_b_est.setFusionInactive(); - ECL_INFO("stopping baro height fusion"); - } -} - void Ekf::startGpsHgtFusion() { if (!_control_status.flags.gps_hgt) {