2015-11-19 13:08:04 -04:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
2015-12-06 07:57:43 -04:00
|
|
|
* Copyright (c) 2015 Estimation and Control Library (ECL). All rights reserved.
|
2015-11-19 13:08:04 -04:00
|
|
|
*
|
|
|
|
* 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
|
2015-12-06 07:57:43 -04:00
|
|
|
* Core functions for ekf attitude and position estimator.
|
2015-11-19 13:08:04 -04:00
|
|
|
*
|
2015-12-06 07:57:43 -04:00
|
|
|
* @author Roman Bast <bapstroman@gmail.com>
|
2016-03-07 05:31:19 -04:00
|
|
|
* @author Paul Riseborough <p_riseborough@live.com.au>
|
2015-11-19 13:08:04 -04:00
|
|
|
*/
|
|
|
|
|
2016-06-07 03:54:12 -03:00
|
|
|
#include "../ecl.h"
|
2015-11-19 13:08:04 -04:00
|
|
|
#include "ekf.h"
|
2016-03-13 05:43:20 -03:00
|
|
|
#include "mathlib.h"
|
2015-11-19 13:08:04 -04:00
|
|
|
|
2016-04-28 11:09:12 -03:00
|
|
|
#ifndef __PX4_QURT
|
2016-04-28 11:30:49 -03:00
|
|
|
#if defined(__cplusplus) && !defined(__PX4_NUTTX)
|
2016-04-28 11:09:12 -03:00
|
|
|
#include <cmath>
|
|
|
|
#define ISFINITE(x) std::isfinite(x)
|
|
|
|
#else
|
|
|
|
#define ISFINITE(x) isfinite(x)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__PX4_QURT)
|
|
|
|
// Missing math.h defines
|
|
|
|
#define ISFINITE(x) __builtin_isfinite(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-05-19 12:58:18 -03:00
|
|
|
const float Ekf::_k_earth_rate = 0.000072921f;
|
|
|
|
const float Ekf::_gravity_mss = 9.80665f;
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
Ekf::Ekf():
|
2016-02-18 06:28:40 -04:00
|
|
|
_filter_initialised(false),
|
|
|
|
_earth_rate_initialised(false),
|
|
|
|
_fuse_height(false),
|
|
|
|
_fuse_pos(false),
|
|
|
|
_fuse_hor_vel(false),
|
|
|
|
_fuse_vert_vel(false),
|
2016-06-07 03:54:12 -03:00
|
|
|
_gps_data_ready(false),
|
|
|
|
_mag_data_ready(false),
|
|
|
|
_baro_data_ready(false),
|
|
|
|
_range_data_ready(false),
|
|
|
|
_flow_data_ready(false),
|
|
|
|
_ev_data_ready(false),
|
|
|
|
_tas_data_ready(false),
|
2016-02-18 06:28:40 -04:00
|
|
|
_time_last_fake_gps(0),
|
|
|
|
_time_last_pos_fuse(0),
|
|
|
|
_time_last_vel_fuse(0),
|
|
|
|
_time_last_hgt_fuse(0),
|
|
|
|
_time_last_of_fuse(0),
|
2016-04-05 10:14:04 -03:00
|
|
|
_time_last_arsp_fuse(0),
|
2016-02-18 06:28:40 -04:00
|
|
|
_last_disarmed_posD(0.0f),
|
2016-05-05 06:49:28 -03:00
|
|
|
_last_dt_overrun(0.0f),
|
2016-03-13 09:41:23 -03:00
|
|
|
_airspeed_innov(0.0f),
|
|
|
|
_airspeed_innov_var(0.0f),
|
2016-02-18 06:21:04 -04:00
|
|
|
_heading_innov(0.0f),
|
|
|
|
_heading_innov_var(0.0f),
|
2016-03-07 05:31:19 -04:00
|
|
|
_delta_time_of(0.0f),
|
2016-02-18 06:28:40 -04:00
|
|
|
_mag_declination(0.0f),
|
2016-02-18 06:21:04 -04:00
|
|
|
_gpsDriftVelN(0.0f),
|
|
|
|
_gpsDriftVelE(0.0f),
|
|
|
|
_gps_drift_velD(0.0f),
|
|
|
|
_gps_velD_diff_filt(0.0f),
|
|
|
|
_gps_velN_filt(0.0f),
|
|
|
|
_gps_velE_filt(0.0f),
|
|
|
|
_last_gps_fail_us(0),
|
|
|
|
_last_gps_origin_time_us(0),
|
|
|
|
_gps_alt_ref(0.0f),
|
2016-03-07 05:31:19 -04:00
|
|
|
_hgt_counter(0),
|
2016-04-21 19:38:41 -03:00
|
|
|
_rng_filt_state(0.0f),
|
2016-02-18 06:21:04 -04:00
|
|
|
_mag_counter(0),
|
2016-05-18 06:18:07 -03:00
|
|
|
_ev_counter(0),
|
2016-03-13 04:44:34 -03:00
|
|
|
_time_last_mag(0),
|
2016-03-15 03:07:33 -03:00
|
|
|
_hgt_sensor_offset(0.0f),
|
2016-06-06 08:59:46 -03:00
|
|
|
_baro_hgt_offset(0.0f),
|
|
|
|
_last_on_ground_posD(0.0f),
|
2016-03-07 05:31:19 -04:00
|
|
|
_terrain_vpos(0.0f),
|
2016-05-02 12:35:42 -03:00
|
|
|
_terrain_var(1.e4f),
|
2016-03-07 05:31:19 -04:00
|
|
|
_hagl_innov(0.0f),
|
|
|
|
_hagl_innov_var(0.0f),
|
2016-03-13 05:43:20 -03:00
|
|
|
_time_last_hagl_fuse(0),
|
2016-03-15 03:07:33 -03:00
|
|
|
_baro_hgt_faulty(false),
|
|
|
|
_gps_hgt_faulty(false),
|
|
|
|
_rng_hgt_faulty(false),
|
2016-04-21 20:24:04 -03:00
|
|
|
_time_bad_vert_accel(0)
|
2015-11-19 13:08:04 -04:00
|
|
|
{
|
2016-02-18 06:28:40 -04:00
|
|
|
_state = {};
|
|
|
|
_last_known_posNE.setZero();
|
|
|
|
_earth_rate_NED.setZero();
|
2016-04-11 17:34:50 -03:00
|
|
|
_R_to_earth = matrix::Dcm<float>();
|
2016-02-23 18:52:02 -04:00
|
|
|
memset(_vel_pos_innov, 0, sizeof(_vel_pos_innov));
|
|
|
|
memset(_mag_innov, 0, sizeof(_mag_innov));
|
2016-03-07 22:28:45 -04:00
|
|
|
memset(_flow_innov, 0, sizeof(_flow_innov));
|
2016-02-23 18:52:02 -04:00
|
|
|
memset(_vel_pos_innov_var, 0, sizeof(_vel_pos_innov_var));
|
|
|
|
memset(_mag_innov_var, 0, sizeof(_mag_innov_var));
|
2016-03-07 22:28:45 -04:00
|
|
|
memset(_flow_innov_var, 0, sizeof(_flow_innov_var));
|
2015-12-10 11:36:10 -04:00
|
|
|
_delta_angle_corr.setZero();
|
2016-02-23 22:53:55 -04:00
|
|
|
_last_known_posNE.setZero();
|
2016-02-18 06:28:40 -04:00
|
|
|
_imu_down_sampled = {};
|
|
|
|
_q_down_sampled.setZero();
|
2016-03-15 03:07:33 -03:00
|
|
|
_mag_filt_state = {};
|
2016-02-18 06:28:40 -04:00
|
|
|
_delVel_sum = {};
|
2016-03-07 05:31:19 -04:00
|
|
|
_flow_gyro_bias = {};
|
2016-03-13 05:43:20 -03:00
|
|
|
_imu_del_ang_of = {};
|
2016-04-20 08:53:15 -03:00
|
|
|
_gps_check_fail_status.value = 0;
|
2016-05-23 06:56:25 -03:00
|
|
|
_state_reset_status = {};
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ekf::~Ekf()
|
|
|
|
{
|
2016-01-30 16:42:24 -04:00
|
|
|
}
|
2016-02-18 06:21:04 -04:00
|
|
|
|
2016-01-30 16:42:24 -04:00
|
|
|
bool Ekf::init(uint64_t timestamp)
|
|
|
|
{
|
|
|
|
bool ret = initialise_interface(timestamp);
|
|
|
|
_state.vel.setZero();
|
|
|
|
_state.pos.setZero();
|
|
|
|
_state.gyro_bias.setZero();
|
2016-04-27 23:05:54 -03:00
|
|
|
_state.accel_bias.setZero();
|
2016-01-30 16:42:24 -04:00
|
|
|
_state.mag_I.setZero();
|
|
|
|
_state.mag_B.setZero();
|
|
|
|
_state.wind_vel.setZero();
|
|
|
|
_state.quat_nominal.setZero();
|
|
|
|
_state.quat_nominal(0) = 1.0f;
|
|
|
|
|
|
|
|
_output_new.vel.setZero();
|
|
|
|
_output_new.pos.setZero();
|
|
|
|
_output_new.quat_nominal = matrix::Quaternion<float>();
|
2016-03-01 00:35:45 -04:00
|
|
|
|
2016-02-26 01:51:14 -04:00
|
|
|
_delta_angle_corr.setZero();
|
2016-01-30 16:42:24 -04:00
|
|
|
_imu_down_sampled.delta_ang.setZero();
|
|
|
|
_imu_down_sampled.delta_vel.setZero();
|
|
|
|
_imu_down_sampled.delta_ang_dt = 0.0f;
|
|
|
|
_imu_down_sampled.delta_vel_dt = 0.0f;
|
|
|
|
_imu_down_sampled.time_us = timestamp;
|
|
|
|
|
|
|
|
_q_down_sampled(0) = 1.0f;
|
|
|
|
_q_down_sampled(1) = 0.0f;
|
|
|
|
_q_down_sampled(2) = 0.0f;
|
|
|
|
_q_down_sampled(3) = 0.0f;
|
|
|
|
|
|
|
|
_imu_updated = false;
|
|
|
|
_NED_origin_initialised = false;
|
|
|
|
_gps_speed_valid = false;
|
2016-02-26 01:51:14 -04:00
|
|
|
|
|
|
|
_filter_initialised = false;
|
2016-03-07 05:31:19 -04:00
|
|
|
_terrain_initialised = false;
|
2016-02-26 01:51:14 -04:00
|
|
|
|
2016-03-16 03:22:27 -03:00
|
|
|
_control_status.value = 0;
|
|
|
|
_control_status_prev.value = 0;
|
|
|
|
|
2016-05-22 12:40:43 -03:00
|
|
|
_dt_ekf_avg = 0.001f * (float)(FILTER_UPDATE_PERIOD_MS);
|
2016-04-27 23:05:54 -03:00
|
|
|
|
2016-05-23 01:14:30 -03:00
|
|
|
_fault_status.value = 0;
|
2016-05-23 04:33:48 -03:00
|
|
|
_innov_check_fail_status.value = 0;
|
2016-05-23 01:08:03 -03:00
|
|
|
|
2016-01-30 16:42:24 -04:00
|
|
|
return ret;
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
bool Ekf::update()
|
2015-11-19 13:08:04 -04:00
|
|
|
{
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2016-03-15 03:07:33 -03:00
|
|
|
if (!_filter_initialised) {
|
|
|
|
_filter_initialised = initialiseFilter();
|
2016-03-07 05:31:19 -04:00
|
|
|
|
2016-03-15 03:07:33 -03:00
|
|
|
if (!_filter_initialised) {
|
|
|
|
return false;
|
2015-12-10 11:36:10 -04:00
|
|
|
}
|
2016-03-15 03:07:33 -03:00
|
|
|
}
|
2016-01-31 04:01:44 -04:00
|
|
|
|
2016-03-13 06:35:51 -03:00
|
|
|
// Only run the filter if IMU data in the buffer has been updated
|
|
|
|
if (_imu_updated) {
|
|
|
|
|
2016-03-07 05:31:19 -04:00
|
|
|
// perform state and covariance prediction for the main filter
|
2015-11-19 13:08:04 -04:00
|
|
|
predictState();
|
|
|
|
predictCovariance();
|
|
|
|
|
2016-03-07 05:31:19 -04:00
|
|
|
// perform state and variance prediction for the terrain estimator
|
|
|
|
if (!_terrain_initialised) {
|
|
|
|
_terrain_initialised = initHagl();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
predictHagl();
|
|
|
|
}
|
2015-12-07 04:26:30 -04:00
|
|
|
|
2016-06-07 03:54:12 -03:00
|
|
|
// control fusion of observation data
|
2016-03-07 05:31:19 -04:00
|
|
|
controlFusionModes();
|
2016-01-28 06:52:39 -04:00
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
2016-03-07 05:31:19 -04:00
|
|
|
// the output observer always runs
|
2015-12-10 11:36:10 -04:00
|
|
|
calculateOutputStates();
|
2015-12-07 04:26:30 -04:00
|
|
|
|
2016-04-27 07:58:22 -03:00
|
|
|
// check for NaN or inf on attitude states
|
2016-04-28 11:09:12 -03:00
|
|
|
if (!ISFINITE(_state.quat_nominal(0)) || !ISFINITE(_output_new.quat_nominal(0))) {
|
2016-03-15 23:43:21 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-07 05:31:19 -04:00
|
|
|
// We don't have valid data to output until tilt and yaw alignment is complete
|
|
|
|
if (_control_status.flags.tilt_align && _control_status.flags.yaw_align) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Ekf::initialiseFilter(void)
|
|
|
|
{
|
2016-05-18 06:18:07 -03:00
|
|
|
// Keep accumulating measurements until we have a minimum of 10 samples for the required sensors
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2016-02-09 19:02:30 -04:00
|
|
|
// Sum the IMU delta angle measurements
|
2016-03-07 05:31:19 -04:00
|
|
|
imuSample imu_init = _imu_buffer.get_newest();
|
|
|
|
_delVel_sum += imu_init.delta_vel;
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2016-02-09 19:02:30 -04:00
|
|
|
// Sum the magnetometer measurements
|
2016-03-15 03:07:33 -03:00
|
|
|
if (_mag_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_mag_sample_delayed)) {
|
2016-06-07 22:56:07 -03:00
|
|
|
if ((_mag_counter == 0) && (_mag_sample_delayed.time_us != 0)) {
|
2016-05-27 20:04:26 -03:00
|
|
|
// initialise the counter when we start getting data from the buffer
|
2016-04-03 23:39:19 -03:00
|
|
|
_mag_counter = 1;
|
2016-06-07 22:56:07 -03:00
|
|
|
} else if ((_mag_counter != 0) && (_mag_sample_delayed.time_us != 0)) {
|
2016-04-03 23:39:19 -03:00
|
|
|
// increment the sample count and apply a LPF to the measurement
|
|
|
|
_mag_counter ++;
|
2016-05-27 19:28:57 -03:00
|
|
|
// don't start using data until we can be certain all bad initial data has been flushed
|
2016-05-27 20:04:26 -03:00
|
|
|
if (_mag_counter == OBS_BUFFER_LENGTH+1) {
|
|
|
|
// initialise filter states
|
|
|
|
_mag_filt_state = _mag_sample_delayed.mag;
|
|
|
|
} else if (_mag_counter > OBS_BUFFER_LENGTH+1) {
|
|
|
|
// noise filter the data
|
2016-05-27 19:28:57 -03:00
|
|
|
_mag_filt_state = _mag_filt_state * 0.9f + _mag_sample_delayed.mag * 0.1f;
|
|
|
|
}
|
2016-03-15 03:07:33 -03:00
|
|
|
}
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
2016-05-18 06:18:07 -03:00
|
|
|
// Count the number of external vision measurements received
|
|
|
|
if (_ext_vision_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_ev_sample_delayed)) {
|
2016-06-07 22:56:07 -03:00
|
|
|
if ((_ev_counter == 0) && (_ev_sample_delayed.time_us != 0)) {
|
2016-05-18 06:18:07 -03:00
|
|
|
// initialise the counter
|
|
|
|
_ev_counter = 1;
|
2016-05-27 00:59:14 -03:00
|
|
|
// set the height fusion mode to use external vision data when we start getting valid data from the buffer
|
|
|
|
if (_primary_hgt_source == VDIST_SENSOR_EV) {
|
|
|
|
_control_status.flags.baro_hgt = false;
|
|
|
|
_control_status.flags.gps_hgt = false;
|
|
|
|
_control_status.flags.rng_hgt = false;
|
|
|
|
_control_status.flags.ev_hgt = true;
|
|
|
|
}
|
2016-06-07 22:56:07 -03:00
|
|
|
} else if ((_ev_counter != 0) && (_ev_sample_delayed.time_us != 0)) {
|
2016-05-18 06:18:07 -03:00
|
|
|
// increment the sample count
|
|
|
|
_ev_counter ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 03:07:33 -03:00
|
|
|
// set the default height source from the adjustable parameter
|
|
|
|
if (_hgt_counter == 0) {
|
2016-05-27 00:14:52 -03:00
|
|
|
_primary_hgt_source = _params.vdist_sensor_type;
|
2016-03-15 03:07:33 -03:00
|
|
|
}
|
|
|
|
|
2016-05-27 00:59:14 -03:00
|
|
|
// accumulate enough height measurements to be confident in the qulaity of the data
|
2016-03-15 03:07:33 -03:00
|
|
|
if (_primary_hgt_source == VDIST_SENSOR_RANGE) {
|
|
|
|
if (_range_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_range_sample_delayed)) {
|
2016-06-07 22:56:07 -03:00
|
|
|
if ((_hgt_counter == 0) && (_range_sample_delayed.time_us != 0)) {
|
2016-05-27 20:04:26 -03:00
|
|
|
// initialise the counter height fusion method when we start getting data from the buffer
|
2016-03-15 03:07:33 -03:00
|
|
|
_control_status.flags.baro_hgt = false;
|
|
|
|
_control_status.flags.gps_hgt = false;
|
|
|
|
_control_status.flags.rng_hgt = true;
|
2016-05-27 00:59:14 -03:00
|
|
|
_control_status.flags.ev_hgt = false;
|
2016-04-03 23:39:19 -03:00
|
|
|
_hgt_counter = 1;
|
2016-06-07 22:56:07 -03:00
|
|
|
} else if ((_hgt_counter != 0) && (_range_sample_delayed.time_us != 0)) {
|
2016-04-03 23:39:19 -03:00
|
|
|
// increment the sample count and apply a LPF to the measurement
|
|
|
|
_hgt_counter ++;
|
2016-05-27 19:28:57 -03:00
|
|
|
// don't start using data until we can be certain all bad initial data has been flushed
|
2016-05-27 20:04:26 -03:00
|
|
|
if (_hgt_counter == OBS_BUFFER_LENGTH+1) {
|
|
|
|
// initialise filter states
|
|
|
|
_rng_filt_state = _range_sample_delayed.rng;
|
|
|
|
} else if (_hgt_counter > OBS_BUFFER_LENGTH+1) {
|
|
|
|
// noise filter the data
|
2016-05-27 19:28:57 -03:00
|
|
|
_rng_filt_state = 0.9f * _rng_filt_state + 0.1f * _range_sample_delayed.rng;
|
|
|
|
}
|
2016-03-15 03:07:33 -03:00
|
|
|
}
|
2016-03-07 05:31:19 -04:00
|
|
|
}
|
|
|
|
|
2016-05-27 00:59:14 -03:00
|
|
|
} else if (_primary_hgt_source == VDIST_SENSOR_BARO || _primary_hgt_source == VDIST_SENSOR_GPS) {
|
|
|
|
// if the user parameter specifies use of GPS for height we use baro height initially and switch to GPS
|
2016-04-03 23:39:19 -03:00
|
|
|
// later when it passes checks.
|
2016-03-15 03:07:33 -03:00
|
|
|
if (_baro_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_baro_sample_delayed)) {
|
2016-06-07 22:56:07 -03:00
|
|
|
if ((_hgt_counter == 0) && (_baro_sample_delayed.time_us != 0)) {
|
2016-05-27 20:04:26 -03:00
|
|
|
// initialise the counter and height fusion method when we start getting data from the buffer
|
2016-03-15 03:07:33 -03:00
|
|
|
_control_status.flags.baro_hgt = true;
|
|
|
|
_control_status.flags.gps_hgt = false;
|
|
|
|
_control_status.flags.rng_hgt = false;
|
2016-04-03 23:39:19 -03:00
|
|
|
_hgt_counter = 1;
|
2016-06-07 22:56:07 -03:00
|
|
|
} else if ((_hgt_counter != 0) && (_baro_sample_delayed.time_us != 0)) {
|
2016-04-03 23:39:19 -03:00
|
|
|
// increment the sample count and apply a LPF to the measurement
|
|
|
|
_hgt_counter ++;
|
2016-05-27 19:28:57 -03:00
|
|
|
// don't start using data until we can be certain all bad initial data has been flushed
|
2016-05-27 20:04:26 -03:00
|
|
|
if (_hgt_counter == OBS_BUFFER_LENGTH+1) {
|
|
|
|
// initialise filter states
|
|
|
|
_baro_hgt_offset = _baro_sample_delayed.hgt;
|
|
|
|
} else if (_hgt_counter > OBS_BUFFER_LENGTH+1) {
|
|
|
|
// noise filter the data
|
2016-05-27 19:28:57 -03:00
|
|
|
_baro_hgt_offset = 0.9f * _baro_hgt_offset + 0.1f * _baro_sample_delayed.hgt;
|
|
|
|
}
|
2016-03-15 03:07:33 -03:00
|
|
|
}
|
2016-03-07 05:31:19 -04:00
|
|
|
}
|
2016-03-13 04:44:34 -03:00
|
|
|
|
2016-05-27 00:59:14 -03:00
|
|
|
} else if (_primary_hgt_source == VDIST_SENSOR_EV) {
|
|
|
|
// do nothing becasue vision data is checked elsewhere
|
2016-03-13 04:44:34 -03:00
|
|
|
} else {
|
|
|
|
return false;
|
2016-02-09 19:02:30 -04:00
|
|
|
}
|
2016-01-02 09:03:03 -04:00
|
|
|
|
2016-03-13 04:44:34 -03:00
|
|
|
// check to see if we have enough measurements and return false if not
|
2016-05-27 19:28:57 -03:00
|
|
|
bool hgt_count_fail = _hgt_counter <= 2*OBS_BUFFER_LENGTH;
|
|
|
|
bool mag_count_fail = _mag_counter <= 2*OBS_BUFFER_LENGTH;
|
|
|
|
bool ev_count_fail = ((_params.fusion_mode & MASK_USE_EVPOS) || (_params.fusion_mode & MASK_USE_EVYAW)) && (_ev_counter <= 2*OBS_BUFFER_LENGTH);
|
2016-05-18 06:18:07 -03:00
|
|
|
if (hgt_count_fail || mag_count_fail || ev_count_fail) {
|
2015-12-10 11:36:10 -04:00
|
|
|
return false;
|
|
|
|
|
2016-02-09 19:02:30 -04:00
|
|
|
} else {
|
2016-03-15 03:07:33 -03:00
|
|
|
// reset variables that are shared with post alignment GPS checks
|
|
|
|
_gps_drift_velD = 0.0f;
|
|
|
|
_gps_alt_ref = 0.0f;
|
|
|
|
|
2016-02-09 19:02:30 -04:00
|
|
|
// Zero all of the states
|
|
|
|
_state.vel.setZero();
|
|
|
|
_state.pos.setZero();
|
|
|
|
_state.gyro_bias.setZero();
|
2016-04-27 23:05:54 -03:00
|
|
|
_state.accel_bias.setZero();
|
2016-02-09 19:02:30 -04:00
|
|
|
_state.mag_I.setZero();
|
|
|
|
_state.mag_B.setZero();
|
|
|
|
_state.wind_vel.setZero();
|
|
|
|
|
|
|
|
// get initial roll and pitch estimate from delta velocity vector, assuming vehicle is static
|
|
|
|
float pitch = 0.0f;
|
|
|
|
float roll = 0.0f;
|
|
|
|
|
|
|
|
if (_delVel_sum.norm() > 0.001f) {
|
|
|
|
_delVel_sum.normalize();
|
|
|
|
pitch = asinf(_delVel_sum(0));
|
2016-02-23 20:56:12 -04:00
|
|
|
roll = atan2f(-_delVel_sum(1), -_delVel_sum(2));
|
2016-02-09 19:02:30 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-22 06:22:17 -04:00
|
|
|
|
2016-02-11 23:23:44 -04:00
|
|
|
// calculate initial tilt alignment
|
2016-02-09 19:02:30 -04:00
|
|
|
matrix::Euler<float> euler_init(roll, pitch, 0.0f);
|
|
|
|
_state.quat_nominal = Quaternion(euler_init);
|
|
|
|
_output_new.quat_nominal = _state.quat_nominal;
|
2016-03-07 05:31:19 -04:00
|
|
|
|
2016-04-11 17:34:50 -03:00
|
|
|
// update transformation matrix from body to world frame
|
|
|
|
_R_to_earth = quat_to_invrotmat(_state.quat_nominal);
|
|
|
|
|
2016-02-11 23:23:44 -04:00
|
|
|
// calculate the averaged magnetometer reading
|
2016-03-15 03:07:33 -03:00
|
|
|
Vector3f mag_init = _mag_filt_state;
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2016-02-11 23:23:44 -04:00
|
|
|
// calculate the initial magnetic field and yaw alignment
|
2016-05-18 06:23:44 -03:00
|
|
|
_control_status.flags.yaw_align = resetMagHeading(mag_init);
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2016-04-21 19:38:41 -03:00
|
|
|
if (_control_status.flags.rng_hgt) {
|
2016-05-27 00:59:14 -03:00
|
|
|
// if we are using the range finder as the primary source, then calculate the baro height at origin so we can use baro as a backup
|
|
|
|
// so it can be used as a backup ad set the initial height using the range finder
|
2016-03-14 01:51:15 -03:00
|
|
|
baroSample baro_newest = _baro_buffer.get_newest();
|
2016-04-21 19:38:41 -03:00
|
|
|
_baro_hgt_offset = baro_newest.hgt;
|
|
|
|
_state.pos(2) = -math::max(_rng_filt_state * _R_to_earth(2, 2),_params.rng_gnd_clearance);
|
2016-06-07 03:54:12 -03:00
|
|
|
ECL_INFO("EKF using range finder height - commencing alignment");
|
2016-05-27 00:59:14 -03:00
|
|
|
|
|
|
|
} else if (_control_status.flags.ev_hgt) {
|
|
|
|
// if we are using external vision data for height, then the vertical position state needs to be reset
|
|
|
|
// because the initialisation position is not the zero datum
|
|
|
|
resetHeight();
|
2016-06-07 03:54:12 -03:00
|
|
|
ECL_INFO("EKF using vision height - commencing alignment");
|
|
|
|
|
|
|
|
} else if (_control_status.flags.baro_hgt){
|
|
|
|
ECL_INFO("EKF using pressure height - commencing alignment");
|
2016-05-27 00:59:14 -03:00
|
|
|
|
2016-03-14 01:51:15 -03:00
|
|
|
}
|
2016-03-13 05:43:20 -03:00
|
|
|
|
2016-02-09 21:53:24 -04:00
|
|
|
// initialise the state covariance matrix
|
2016-02-09 19:02:30 -04:00
|
|
|
initialiseCovariance();
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2016-03-07 05:31:19 -04:00
|
|
|
// initialise the terrain estimator
|
|
|
|
initHagl();
|
|
|
|
|
2016-04-21 19:33:11 -03:00
|
|
|
// reset the essential fusion timeout counters
|
|
|
|
_time_last_hgt_fuse = _time_last_imu;
|
|
|
|
_time_last_pos_fuse = _time_last_imu;
|
|
|
|
_time_last_vel_fuse = _time_last_imu;
|
|
|
|
_time_last_hagl_fuse = _time_last_imu;
|
|
|
|
_time_last_of_fuse = _time_last_imu;
|
|
|
|
|
2016-05-23 07:50:58 -03:00
|
|
|
// reset the output predictor state history to match the EKF initial values
|
|
|
|
alignOutputFilter();
|
|
|
|
|
2016-02-09 19:02:30 -04:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ekf::predictState()
|
|
|
|
{
|
2015-12-07 04:26:30 -04:00
|
|
|
if (!_earth_rate_initialised) {
|
2016-01-31 04:01:44 -04:00
|
|
|
if (_NED_origin_initialised) {
|
|
|
|
calcEarthRateNED(_earth_rate_NED, _pos_ref.lat_rad);
|
2015-12-07 04:26:30 -04:00
|
|
|
_earth_rate_initialised = true;
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2016-04-27 23:05:54 -03:00
|
|
|
// apply imu bias corrections
|
|
|
|
Vector3f corrected_delta_ang = _imu_sample_delayed.delta_ang - _state.gyro_bias;
|
|
|
|
Vector3f corrected_delta_vel = _imu_sample_delayed.delta_vel - _state.accel_bias;
|
2016-04-29 19:44:07 -03:00
|
|
|
|
2016-04-11 17:34:50 -03:00
|
|
|
// correct delta angles for earth rotation rate
|
2016-05-02 08:25:50 -03:00
|
|
|
corrected_delta_ang -= -_R_to_earth.transpose() * _earth_rate_NED * _imu_sample_delayed.delta_ang_dt;
|
2016-04-11 17:34:50 -03:00
|
|
|
|
|
|
|
// convert the delta angle to a delta quaternion
|
|
|
|
Quaternion dq;
|
2015-12-07 04:26:30 -04:00
|
|
|
dq.from_axis_angle(corrected_delta_ang);
|
2016-04-11 17:34:50 -03:00
|
|
|
|
|
|
|
// rotate the previous quaternion by the delta quaternion using a quaternion multiplication
|
2015-12-06 07:57:43 -04:00
|
|
|
_state.quat_nominal = dq * _state.quat_nominal;
|
|
|
|
|
2016-04-11 17:34:50 -03:00
|
|
|
// quaternions must be normalised whenever they are modified
|
|
|
|
_state.quat_nominal.normalize();
|
2015-12-07 04:26:30 -04:00
|
|
|
|
2016-04-11 17:34:50 -03:00
|
|
|
// save the previous value of velocity so we can use trapzoidal integration
|
2015-12-06 07:57:43 -04:00
|
|
|
Vector3f vel_last = _state.vel;
|
|
|
|
|
2016-05-22 11:03:30 -03:00
|
|
|
// update transformation matrix from body to world frame
|
2016-04-11 17:58:10 -03:00
|
|
|
_R_to_earth = quat_to_invrotmat(_state.quat_nominal);
|
2016-05-22 11:03:30 -03:00
|
|
|
|
|
|
|
// calculate the increment in velocity using the current orientation
|
2016-04-29 19:44:07 -03:00
|
|
|
_state.vel += _R_to_earth * corrected_delta_vel;
|
2016-04-11 17:58:10 -03:00
|
|
|
|
|
|
|
// compensate for acceleration due to gravity
|
|
|
|
_state.vel(2) += _gravity_mss * _imu_sample_delayed.delta_vel_dt;
|
2015-12-06 07:57:43 -04:00
|
|
|
|
|
|
|
// predict position states via trapezoidal integration of velocity
|
|
|
|
_state.pos += (vel_last + _state.vel) * _imu_sample_delayed.delta_vel_dt * 0.5f;
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
constrainStates();
|
2016-04-27 23:05:54 -03:00
|
|
|
|
|
|
|
// calculate an average filter update time
|
2016-05-04 00:59:18 -03:00
|
|
|
float input = 0.5f*(_imu_sample_delayed.delta_vel_dt + _imu_sample_delayed.delta_ang_dt);
|
|
|
|
|
|
|
|
// filter and limit input between -50% and +100% of nominal value
|
2016-05-22 12:40:43 -03:00
|
|
|
input = math::constrain(input,0.0005f * (float)(FILTER_UPDATE_PERIOD_MS),0.002f * (float)(FILTER_UPDATE_PERIOD_MS));
|
2016-05-15 07:36:51 -03:00
|
|
|
_dt_ekf_avg = 0.99f * _dt_ekf_avg + 0.01f * input;
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
2016-01-30 00:03:25 -04:00
|
|
|
bool Ekf::collect_imu(imuSample &imu)
|
|
|
|
{
|
2016-05-22 12:40:43 -03:00
|
|
|
// accumulate and downsample IMU data across a period FILTER_UPDATE_PERIOD_MS long
|
2016-05-02 07:29:46 -03:00
|
|
|
|
|
|
|
// copy imu data to local variables
|
2016-02-18 06:21:04 -04:00
|
|
|
_imu_sample_new.delta_ang = imu.delta_ang;
|
|
|
|
_imu_sample_new.delta_vel = imu.delta_vel;
|
|
|
|
_imu_sample_new.delta_ang_dt = imu.delta_ang_dt;
|
|
|
|
_imu_sample_new.delta_vel_dt = imu.delta_vel_dt;
|
2016-05-02 07:29:46 -03:00
|
|
|
_imu_sample_new.time_us = imu.time_us;
|
2016-01-30 00:03:25 -04:00
|
|
|
|
2016-05-02 07:29:46 -03:00
|
|
|
// accumulate the time deltas
|
2016-01-30 00:03:25 -04:00
|
|
|
_imu_down_sampled.delta_ang_dt += imu.delta_ang_dt;
|
|
|
|
_imu_down_sampled.delta_vel_dt += imu.delta_vel_dt;
|
|
|
|
|
2016-05-02 07:29:46 -03:00
|
|
|
// use a quaternion to accumulate delta angle data
|
|
|
|
// this quaternion represents the rotation from the start to end of the accumulation period
|
2016-01-30 00:03:25 -04:00
|
|
|
Quaternion delta_q;
|
|
|
|
delta_q.rotate(imu.delta_ang);
|
|
|
|
_q_down_sampled = _q_down_sampled * delta_q;
|
|
|
|
_q_down_sampled.normalize();
|
|
|
|
|
2016-05-02 07:29:46 -03:00
|
|
|
// rotate the accumulated delta velocity data forward each time so it is always in the updated rotation frame
|
2016-01-30 00:03:25 -04:00
|
|
|
matrix::Dcm<float> delta_R(delta_q.inversed());
|
|
|
|
_imu_down_sampled.delta_vel = delta_R * _imu_down_sampled.delta_vel;
|
|
|
|
|
2016-05-02 07:29:46 -03:00
|
|
|
// accumulate the most recent delta velocity data at the updated rotation frame
|
|
|
|
// assume effective sample time is halfway between the previous and current rotation frame
|
|
|
|
_imu_down_sampled.delta_vel += (_imu_sample_new.delta_vel + delta_R * _imu_sample_new.delta_vel) * 0.5f;
|
|
|
|
|
|
|
|
// if the target time delta between filter prediction steps has been exceeded
|
2016-05-05 06:49:28 -03:00
|
|
|
// write the accumulated IMU data to the ring buffer
|
2016-05-22 12:40:43 -03:00
|
|
|
float target_dt = (float)(FILTER_UPDATE_PERIOD_MS) / 1000;
|
2016-05-05 06:49:28 -03:00
|
|
|
if (_imu_down_sampled.delta_ang_dt >= target_dt - _last_dt_overrun) {
|
|
|
|
|
|
|
|
// store the amount we have over-run the target update rate by
|
|
|
|
_last_dt_overrun = _imu_down_sampled.delta_ang_dt - target_dt;
|
2016-02-18 06:21:04 -04:00
|
|
|
|
|
|
|
imu.delta_ang = _q_down_sampled.to_axis_angle();
|
|
|
|
imu.delta_vel = _imu_down_sampled.delta_vel;
|
|
|
|
imu.delta_ang_dt = _imu_down_sampled.delta_ang_dt;
|
|
|
|
imu.delta_vel_dt = _imu_down_sampled.delta_vel_dt;
|
|
|
|
imu.time_us = imu.time_us;
|
|
|
|
|
2016-01-30 00:03:25 -04:00
|
|
|
_imu_down_sampled.delta_ang.setZero();
|
|
|
|
_imu_down_sampled.delta_vel.setZero();
|
|
|
|
_imu_down_sampled.delta_ang_dt = 0.0f;
|
|
|
|
_imu_down_sampled.delta_vel_dt = 0.0f;
|
|
|
|
_q_down_sampled(0) = 1.0f;
|
|
|
|
_q_down_sampled(1) = _q_down_sampled(2) = _q_down_sampled(3) = 0.0f;
|
2016-01-31 04:01:44 -04:00
|
|
|
return true;
|
2016-01-30 00:03:25 -04:00
|
|
|
}
|
2016-01-31 04:01:44 -04:00
|
|
|
|
2016-01-30 00:03:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
/*
|
|
|
|
* Implement a strapdown INS algorithm using the latest IMU data at the current time horizon.
|
|
|
|
* Buffer the INS states and calculate the difference with the EKF states at the delayed fusion time horizon.
|
|
|
|
* Calculate delta angle, delta velocity and velocity corrections from the differences and apply them at the
|
|
|
|
* current time horizon so that the INS states track the EKF states at the delayed fusion time horizon.
|
|
|
|
* The inspiration for using a complementary filter to correct for time delays in the EKF
|
|
|
|
* is based on the work by A Khosravian:
|
|
|
|
* “Recursive Attitude Estimation in the Presence of Multi-rate and Multi-delay Vector Measurements”
|
|
|
|
* A Khosravian, J Trumpf, R Mahony, T Hamel, Australian National University
|
|
|
|
*/
|
2015-12-10 11:36:10 -04:00
|
|
|
void Ekf::calculateOutputStates()
|
|
|
|
{
|
2016-04-11 18:32:24 -03:00
|
|
|
// use latest IMU data
|
2015-12-10 11:36:10 -04:00
|
|
|
imuSample imu_new = _imu_sample_new;
|
|
|
|
|
2016-04-29 19:44:07 -03:00
|
|
|
// correct delta angles for bias offsets and scale factors
|
2016-04-11 18:32:24 -03:00
|
|
|
Vector3f delta_angle;
|
2016-05-04 00:59:18 -03:00
|
|
|
float dt_scale_correction = _dt_imu_avg/_dt_ekf_avg;
|
|
|
|
delta_angle(0) = _imu_sample_new.delta_ang(0) - _state.gyro_bias(0)*dt_scale_correction;
|
|
|
|
delta_angle(1) = _imu_sample_new.delta_ang(1) - _state.gyro_bias(1)*dt_scale_correction;
|
|
|
|
delta_angle(2) = _imu_sample_new.delta_ang(2) - _state.gyro_bias(2)*dt_scale_correction;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-04-29 19:44:07 -03:00
|
|
|
// correct delta velocity for bias offsets
|
2016-05-04 00:59:18 -03:00
|
|
|
Vector3f delta_vel = _imu_sample_new.delta_vel - _state.accel_bias*dt_scale_correction;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// Apply corrections to the delta angle required to track the quaternion states at the EKF fusion time horizon
|
2015-12-10 11:36:10 -04:00
|
|
|
delta_angle += _delta_angle_corr;
|
2016-04-11 18:32:24 -03:00
|
|
|
|
|
|
|
// convert the delta angle to an equivalent delta quaternions
|
2015-12-10 11:36:10 -04:00
|
|
|
Quaternion dq;
|
|
|
|
dq.from_axis_angle(delta_angle);
|
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// rotate the previous INS quaternion by the delta quaternions
|
2015-12-10 11:36:10 -04:00
|
|
|
_output_new.time_us = imu_new.time_us;
|
|
|
|
_output_new.quat_nominal = dq * _output_new.quat_nominal;
|
2016-04-11 18:32:24 -03:00
|
|
|
|
|
|
|
// the quaternions must always be normalised afer modification
|
2015-12-10 11:36:10 -04:00
|
|
|
_output_new.quat_nominal.normalize();
|
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// calculate the rotation matrix from body to earth frame
|
2016-04-09 16:46:27 -03:00
|
|
|
_R_to_earth_now = quat_to_invrotmat(_output_new.quat_nominal);
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// rotate the delta velocity to earth frame
|
2016-05-13 22:04:01 -03:00
|
|
|
Vector3f delta_vel_NED = _R_to_earth_now * delta_vel;
|
2016-04-11 18:32:24 -03:00
|
|
|
|
|
|
|
// corrrect for measured accceleration due to gravity
|
2016-04-11 18:56:06 -03:00
|
|
|
delta_vel_NED(2) += _gravity_mss * imu_new.delta_vel_dt;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// save the previous velocity so we can use trapezidal integration
|
2015-12-10 11:36:10 -04:00
|
|
|
Vector3f vel_last = _output_new.vel;
|
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// increment the INS velocity states by the measurement plus corrections
|
2015-12-10 11:36:10 -04:00
|
|
|
_output_new.vel += delta_vel_NED;
|
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// use trapezoidal integration to calculate the INS position states
|
2016-05-13 22:04:01 -03:00
|
|
|
_output_new.pos += (_output_new.vel + vel_last) * (imu_new.delta_vel_dt * 0.5f);
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-04-11 18:32:24 -03:00
|
|
|
// store INS states in a ring buffer that with the same length and time coordinates as the IMU data buffer
|
2016-05-13 23:56:46 -03:00
|
|
|
if (_imu_updated) {
|
|
|
|
_output_buffer.push(_output_new);
|
|
|
|
_imu_updated = false;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
// get the oldest INS state data from the ring buffer
|
|
|
|
// this data will be at the EKF fusion time horizon
|
|
|
|
_output_sample_delayed = _output_buffer.get_oldest();
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
// calculate the quaternion delta between the INS and EKF quaternions at the EKF fusion time horizon
|
|
|
|
Quaternion quat_inv = _state.quat_nominal.inversed();
|
|
|
|
Quaternion q_error = _output_sample_delayed.quat_nominal * quat_inv;
|
|
|
|
q_error.normalize();
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
// convert the quaternion delta to a delta angle
|
|
|
|
Vector3f delta_ang_error;
|
|
|
|
float scalar;
|
|
|
|
if (q_error(0) >= 0.0f) {
|
|
|
|
scalar = -2.0f;
|
2016-04-29 22:03:45 -03:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
} else {
|
|
|
|
scalar = 2.0f;
|
|
|
|
}
|
|
|
|
delta_ang_error(0) = scalar * q_error(1);
|
|
|
|
delta_ang_error(1) = scalar * q_error(2);
|
|
|
|
delta_ang_error(2) = scalar * q_error(3);
|
|
|
|
|
|
|
|
// calculate a gain that provides tight tracking of the estimator attitude states and
|
2016-05-22 12:40:43 -03:00
|
|
|
// adjust for changes in time delay to maintain consistent damping ratio of ~0.7
|
2016-05-13 22:04:01 -03:00
|
|
|
float time_delay = 1e-6f * (float)(_imu_sample_new.time_us - _imu_sample_delayed.time_us);
|
|
|
|
time_delay = fmaxf(time_delay, _dt_imu_avg);
|
|
|
|
float att_gain = 0.5f * _dt_imu_avg / time_delay;
|
|
|
|
|
|
|
|
// calculate a corrrection to the delta angle
|
|
|
|
// that will cause the INS to track the EKF quaternions
|
|
|
|
_delta_angle_corr = delta_ang_error * att_gain;
|
|
|
|
|
2016-05-19 20:25:36 -03:00
|
|
|
// calculate a position correction that will be applied to the output state history
|
2016-05-13 22:04:01 -03:00
|
|
|
float pos_gain = _dt_ekf_avg / math::constrain(_params.pos_Tau, _dt_ekf_avg, 10.0f);
|
|
|
|
Vector3f pos_delta = (_state.pos - _output_sample_delayed.pos) * pos_gain;
|
|
|
|
|
2016-05-19 20:25:36 -03:00
|
|
|
// calculate a velocity correction that will be applied to the output state history
|
|
|
|
Vector3f vel_delta;
|
|
|
|
if (_params.pos_Tau <= 0.0f) {
|
|
|
|
// this method will cause the velocity to be kinematically consistent with
|
|
|
|
// the position corretions rather than tracking the EKF states
|
|
|
|
vel_delta = pos_delta * (1.0f / time_delay);
|
|
|
|
} else {
|
|
|
|
// this method makes the velocity track the EKF states with the specified time constant
|
|
|
|
float vel_gain = _dt_ekf_avg / math::constrain(_params.vel_Tau, _dt_ekf_avg, 10.0f);
|
|
|
|
vel_delta = (_state.vel - _output_sample_delayed.vel) * vel_gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the output filter state history and apply the corrections to the velocity and position states
|
2016-05-13 22:04:01 -03:00
|
|
|
// this method is too expensive to use for the attitude states due to the quaternion operations required
|
2016-05-19 20:25:36 -03:00
|
|
|
// but does not introduce a time delay in the 'correction loop' and allows smaller tracking time constants
|
2016-05-13 22:04:01 -03:00
|
|
|
// to be used
|
|
|
|
outputSample output_states;
|
2016-05-19 20:25:36 -03:00
|
|
|
unsigned max_index = _output_buffer.get_length() - 1;
|
|
|
|
for (unsigned index=0; index <= max_index; index++) {
|
|
|
|
output_states = _output_buffer.get_from_index(index);
|
|
|
|
|
|
|
|
// a constant velocity correction is applied
|
2016-05-13 22:04:01 -03:00
|
|
|
output_states.vel += vel_delta;
|
2016-05-19 20:25:36 -03:00
|
|
|
|
|
|
|
// a constant position correction is applied
|
2016-05-13 22:04:01 -03:00
|
|
|
output_states.pos += pos_delta;
|
2016-05-19 20:25:36 -03:00
|
|
|
|
|
|
|
// push the updated data to the buffer
|
|
|
|
_output_buffer.push_to_index(index,output_states);
|
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
}
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
// update output state to corrected values
|
|
|
|
_output_new = _output_buffer.get_newest();
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-05-13 22:04:01 -03:00
|
|
|
}
|
2016-04-03 23:39:19 -03:00
|
|
|
}
|