mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 10:07:39 +08:00
324 lines
10 KiB
C++
324 lines
10 KiB
C++
/****************************************************************************
|
|
*
|
|
* Copyright (c) 2015 Estimation and Control Library (ECL). 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 ECL 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 ekf.cpp
|
|
* Core functions for ekf attitude and position estimator.
|
|
*
|
|
* @author Roman Bast <bapstroman@gmail.com>
|
|
* @author Paul Riseborough <p_riseborough@live.com.au>
|
|
*/
|
|
|
|
#include "ekf.h"
|
|
|
|
#include <mathlib/mathlib.h>
|
|
|
|
bool Ekf::init(uint64_t timestamp)
|
|
{
|
|
if (!_initialised) {
|
|
_initialised = initialise_interface(timestamp);
|
|
reset();
|
|
}
|
|
|
|
return _initialised;
|
|
}
|
|
|
|
void Ekf::reset()
|
|
{
|
|
ECL_INFO("reset");
|
|
|
|
_state.vel.setZero();
|
|
_state.pos.setZero();
|
|
_state.gyro_bias.setZero();
|
|
_state.accel_bias.setZero();
|
|
_state.mag_I.setZero();
|
|
_state.mag_B.setZero();
|
|
_state.wind_vel.setZero();
|
|
_state.quat_nominal.setIdentity();
|
|
|
|
#if defined(CONFIG_EKF2_RANGE_FINDER)
|
|
_range_sensor.setPitchOffset(_params.rng_sens_pitch);
|
|
_range_sensor.setCosMaxTilt(_params.range_cos_max_tilt);
|
|
_range_sensor.setQualityHysteresis(_params.range_valid_quality_s);
|
|
#endif // CONFIG_EKF2_RANGE_FINDER
|
|
|
|
_control_status.value = 0;
|
|
_control_status_prev.value = 0;
|
|
|
|
_control_status.flags.in_air = true;
|
|
_control_status_prev.flags.in_air = true;
|
|
|
|
_ang_rate_delayed_raw.zero();
|
|
|
|
_fault_status.value = 0;
|
|
_innov_check_fail_status.value = 0;
|
|
|
|
_prev_gyro_bias_var.zero();
|
|
_prev_accel_bias_var.zero();
|
|
|
|
resetGpsDriftCheckFilters();
|
|
|
|
_output_predictor.reset();
|
|
|
|
// Ekf private fields
|
|
_time_last_horizontal_aiding = 0;
|
|
_time_last_v_pos_aiding = 0;
|
|
_time_last_v_vel_aiding = 0;
|
|
|
|
_time_last_hor_pos_fuse = 0;
|
|
_time_last_hgt_fuse = 0;
|
|
_time_last_hor_vel_fuse = 0;
|
|
_time_last_ver_vel_fuse = 0;
|
|
_time_last_heading_fuse = 0;
|
|
_time_last_zero_velocity_fuse = 0;
|
|
|
|
_last_known_pos.setZero();
|
|
|
|
_time_acc_bias_check = 0;
|
|
|
|
_gps_checks_passed = false;
|
|
_gps_alt_ref = NAN;
|
|
|
|
_baro_counter = 0;
|
|
|
|
#if defined(CONFIG_EKF2_MAGNETOMETER)
|
|
_mag_counter = 0;
|
|
#endif // CONFIG_EKF2_MAGNETOMETER
|
|
|
|
_time_bad_vert_accel = 0;
|
|
_time_good_vert_accel = 0;
|
|
_clip_counter = 0;
|
|
|
|
resetEstimatorAidStatus(_aid_src_baro_hgt);
|
|
#if defined(CONFIG_EKF2_AIRSPEED)
|
|
resetEstimatorAidStatus(_aid_src_airspeed);
|
|
#endif // CONFIG_EKF2_AIRSPEED
|
|
#if defined(CONFIG_EKF2_SIDESLIP)
|
|
resetEstimatorAidStatus(_aid_src_sideslip);
|
|
#endif // CONFIG_EKF2_SIDESLIP
|
|
|
|
resetEstimatorAidStatus(_aid_src_fake_pos);
|
|
resetEstimatorAidStatus(_aid_src_fake_hgt);
|
|
|
|
#if defined(CONFIG_EKF2_EXTERNAL_VISION)
|
|
resetEstimatorAidStatus(_aid_src_ev_hgt);
|
|
resetEstimatorAidStatus(_aid_src_ev_pos);
|
|
resetEstimatorAidStatus(_aid_src_ev_vel);
|
|
resetEstimatorAidStatus(_aid_src_ev_yaw);
|
|
#endif // CONFIG_EKF2_EXTERNAL_VISION
|
|
|
|
resetEstimatorAidStatus(_aid_src_gnss_hgt);
|
|
resetEstimatorAidStatus(_aid_src_gnss_pos);
|
|
resetEstimatorAidStatus(_aid_src_gnss_vel);
|
|
|
|
#if defined(CONFIG_EKF2_GNSS_YAW)
|
|
resetEstimatorAidStatus(_aid_src_gnss_yaw);
|
|
#endif // CONFIG_EKF2_GNSS_YAW
|
|
|
|
#if defined(CONFIG_EKF2_MAGNETOMETER)
|
|
resetEstimatorAidStatus(_aid_src_mag_heading);
|
|
resetEstimatorAidStatus(_aid_src_mag);
|
|
#endif // CONFIG_EKF2_MAGNETOMETER
|
|
|
|
#if defined(CONFIG_EKF2_AUXVEL)
|
|
resetEstimatorAidStatus(_aid_src_aux_vel);
|
|
#endif // CONFIG_EKF2_AUXVEL
|
|
|
|
#if defined(CONFIG_EKF2_OPTICAL_FLOW)
|
|
resetEstimatorAidStatus(_aid_src_optical_flow);
|
|
resetEstimatorAidStatus(_aid_src_terrain_optical_flow);
|
|
#endif // CONFIG_EKF2_OPTICAL_FLOW
|
|
|
|
#if defined(CONFIG_EKF2_RANGE_FINDER)
|
|
resetEstimatorAidStatus(_aid_src_rng_hgt);
|
|
#endif // CONFIG_EKF2_RANGE_FINDER
|
|
}
|
|
|
|
bool Ekf::update()
|
|
{
|
|
if (!_filter_initialised) {
|
|
_filter_initialised = initialiseFilter();
|
|
|
|
if (!_filter_initialised) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Only run the filter if IMU data in the buffer has been updated
|
|
if (_imu_updated) {
|
|
_imu_updated = false;
|
|
|
|
// get the oldest IMU data from the buffer
|
|
// TODO: explicitly pop at desired time horizon
|
|
const imuSample imu_sample_delayed = _imu_buffer.get_oldest();
|
|
|
|
// perform state and covariance prediction for the main filter
|
|
predictCovariance(imu_sample_delayed);
|
|
predictState(imu_sample_delayed);
|
|
|
|
// control fusion of observation data
|
|
controlFusionModes(imu_sample_delayed);
|
|
|
|
#if defined(CONFIG_EKF2_TERRAIN)
|
|
// run a separate filter for terrain estimation
|
|
runTerrainEstimator(imu_sample_delayed);
|
|
#endif // CONFIG_EKF2_TERRAIN
|
|
|
|
_output_predictor.correctOutputStates(imu_sample_delayed.time_us, _state.quat_nominal, _state.vel, _state.pos, _state.gyro_bias, _state.accel_bias);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Ekf::initialiseFilter()
|
|
{
|
|
// Filter accel for tilt initialization
|
|
const imuSample &imu_init = _imu_buffer.get_newest();
|
|
|
|
// protect against zero data
|
|
if (imu_init.delta_vel_dt < 1e-4f || imu_init.delta_ang_dt < 1e-4f) {
|
|
return false;
|
|
}
|
|
|
|
if (_is_first_imu_sample) {
|
|
_accel_lpf.reset(imu_init.delta_vel / imu_init.delta_vel_dt);
|
|
_gyro_lpf.reset(imu_init.delta_ang / imu_init.delta_ang_dt);
|
|
_is_first_imu_sample = false;
|
|
|
|
} else {
|
|
_accel_lpf.update(imu_init.delta_vel / imu_init.delta_vel_dt);
|
|
_gyro_lpf.update(imu_init.delta_ang / imu_init.delta_ang_dt);
|
|
}
|
|
|
|
if (!initialiseTilt()) {
|
|
return false;
|
|
}
|
|
|
|
// initialise the state covariance matrix now we have starting values for all the states
|
|
initialiseCovariance();
|
|
|
|
#if defined(CONFIG_EKF2_TERRAIN)
|
|
// Initialise the terrain estimator
|
|
initHagl();
|
|
#endif // CONFIG_EKF2_TERRAIN
|
|
|
|
// reset the output predictor state history to match the EKF initial values
|
|
_output_predictor.alignOutputFilter(_state.quat_nominal, _state.vel, _state.pos);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Ekf::initialiseTilt()
|
|
{
|
|
const float accel_norm = _accel_lpf.getState().norm();
|
|
const float gyro_norm = _gyro_lpf.getState().norm();
|
|
|
|
if (accel_norm < 0.8f * CONSTANTS_ONE_G ||
|
|
accel_norm > 1.2f * CONSTANTS_ONE_G ||
|
|
gyro_norm > math::radians(15.0f)) {
|
|
return false;
|
|
}
|
|
|
|
// get initial tilt estimate from delta velocity vector, assuming vehicle is static
|
|
_state.quat_nominal = Quatf(_accel_lpf.getState(), Vector3f(0.f, 0.f, -1.f));
|
|
_R_to_earth = Dcmf(_state.quat_nominal);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Ekf::predictState(const imuSample &imu_delayed)
|
|
{
|
|
// apply imu bias corrections
|
|
const Vector3f delta_ang_bias_scaled = getGyroBias() * imu_delayed.delta_ang_dt;
|
|
Vector3f corrected_delta_ang = imu_delayed.delta_ang - delta_ang_bias_scaled;
|
|
|
|
// subtract component of angular rate due to earth rotation
|
|
corrected_delta_ang -= _R_to_earth.transpose() * _earth_rate_NED * imu_delayed.delta_ang_dt;
|
|
|
|
const Quatf dq(AxisAnglef{corrected_delta_ang});
|
|
|
|
// rotate the previous quaternion by the delta quaternion using a quaternion multiplication
|
|
_state.quat_nominal = (_state.quat_nominal * dq).normalized();
|
|
_R_to_earth = Dcmf(_state.quat_nominal);
|
|
|
|
// Calculate an earth frame delta velocity
|
|
const Vector3f delta_vel_bias_scaled = getAccelBias() * imu_delayed.delta_vel_dt;
|
|
const Vector3f corrected_delta_vel = imu_delayed.delta_vel - delta_vel_bias_scaled;
|
|
const Vector3f corrected_delta_vel_ef = _R_to_earth * corrected_delta_vel;
|
|
|
|
// save the previous value of velocity so we can use trapzoidal integration
|
|
const Vector3f vel_last = _state.vel;
|
|
|
|
// calculate the increment in velocity using the current orientation
|
|
_state.vel += corrected_delta_vel_ef;
|
|
|
|
// compensate for acceleration due to gravity
|
|
_state.vel(2) += CONSTANTS_ONE_G * imu_delayed.delta_vel_dt;
|
|
|
|
// predict position states via trapezoidal integration of velocity
|
|
_state.pos += (vel_last + _state.vel) * imu_delayed.delta_vel_dt * 0.5f;
|
|
|
|
constrainStates();
|
|
|
|
// calculate an average filter update time
|
|
float input = 0.5f * (imu_delayed.delta_vel_dt + imu_delayed.delta_ang_dt);
|
|
|
|
// filter and limit input between -50% and +100% of nominal value
|
|
const float filter_update_s = 1e-6f * _params.filter_update_interval_us;
|
|
input = math::constrain(input, 0.5f * filter_update_s, 2.f * filter_update_s);
|
|
_dt_ekf_avg = 0.99f * _dt_ekf_avg + 0.01f * input;
|
|
|
|
// some calculations elsewhere in code require a raw angular rate vector so calculate here to avoid duplication
|
|
// protect against possible small timesteps resulting from timing slip on previous frame that can drive spikes into the rate
|
|
// due to insufficient averaging
|
|
if (imu_delayed.delta_ang_dt > 0.25f * _dt_ekf_avg) {
|
|
_ang_rate_delayed_raw = imu_delayed.delta_ang / imu_delayed.delta_ang_dt;
|
|
}
|
|
|
|
|
|
// calculate a filtered horizontal acceleration with a 1 sec time constant
|
|
// this are used for manoeuvre detection elsewhere
|
|
const float alpha = 1.0f - imu_delayed.delta_vel_dt;
|
|
_accel_lpf_NE = _accel_lpf_NE * alpha + corrected_delta_vel_ef.xy();
|
|
|
|
// calculate a yaw change about the earth frame vertical
|
|
const float spin_del_ang_D = corrected_delta_ang.dot(Vector3f(_R_to_earth.row(2)));
|
|
_yaw_delta_ef += spin_del_ang_D;
|
|
|
|
// Calculate filtered yaw rate to be used by the magnetometer fusion type selection logic
|
|
// Note fixed coefficients are used to save operations. The exact time constant is not important.
|
|
_yaw_rate_lpf_ef = 0.95f * _yaw_rate_lpf_ef + 0.05f * spin_del_ang_D / imu_delayed.delta_ang_dt;
|
|
}
|