From f5f31006a0a54fa4a68526121e780e07ce45569d Mon Sep 17 00:00:00 2001 From: bresch Date: Tue, 26 Jul 2022 22:12:03 +0200 Subject: [PATCH] Ekf2: create HeightBiasEstimator class Contains some logic common to all height bias estimators --- src/modules/ekf2/EKF/bias_estimator.hpp | 6 +- .../ekf2/EKF/height_bias_estimator.hpp | 74 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/modules/ekf2/EKF/height_bias_estimator.hpp diff --git a/src/modules/ekf2/EKF/bias_estimator.hpp b/src/modules/ekf2/EKF/bias_estimator.hpp index faf8ae720d..5f70ec5204 100644 --- a/src/modules/ekf2/EKF/bias_estimator.hpp +++ b/src/modules/ekf2/EKF/bias_estimator.hpp @@ -65,10 +65,10 @@ public: }; BiasEstimator() = default; - ~BiasEstimator() = default; + virtual ~BiasEstimator() = default; - void predict(float dt); - void fuseBias(float measurement, float measurement_var); + virtual void predict(float dt); + virtual void fuseBias(float measurement, float measurement_var); void setBias(float bias) { _state = bias; } void setProcessNoiseStdDev(float process_noise) diff --git a/src/modules/ekf2/EKF/height_bias_estimator.hpp b/src/modules/ekf2/EKF/height_bias_estimator.hpp new file mode 100644 index 0000000000..9082842b9e --- /dev/null +++ b/src/modules/ekf2/EKF/height_bias_estimator.hpp @@ -0,0 +1,74 @@ +/**************************************************************************** + * + * 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 height_bias_estimator.hpp + */ + +#pragma once + +#include "bias_estimator.hpp" + +class HeightBiasEstimator: public BiasEstimator +{ +public: + HeightBiasEstimator(uint8_t sensor, const uint8_t &sensor_ref): + BiasEstimator(0.f, 0.f), + _sensor(sensor), + _sensor_ref(sensor_ref) + {} + virtual ~HeightBiasEstimator() = default; + + void setFusionActive() { _is_sensor_fusion_active = true; } + void setFusionInactive() { _is_sensor_fusion_active = false; } + + virtual void predict(float dt) override + { + if ((_sensor_ref != _sensor) && _is_sensor_fusion_active) { + BiasEstimator::predict(dt); + } + } + + virtual void fuseBias(float bias, float bias_var) override + { + if ((_sensor_ref != _sensor) && _is_sensor_fusion_active) { + BiasEstimator::fuseBias(bias, bias_var); + } + } + +private: + const uint8_t _sensor; + const uint8_t &_sensor_ref; + + bool _is_sensor_fusion_active{false}; // TODO: replace by const ref and remove setter when migrating _control_status.flags from union to bool +};