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>
|
2015-11-19 13:08:04 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ekf.h"
|
2015-12-06 07:57:43 -04:00
|
|
|
#include <drivers/drv_hrt.h>
|
2015-11-19 13:08:04 -04:00
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
Ekf::Ekf():
|
2016-01-28 06:52:39 -04:00
|
|
|
_control_status{},
|
|
|
|
_filter_initialised(false),
|
2015-12-07 04:26:30 -04:00
|
|
|
_earth_rate_initialised(false),
|
|
|
|
_fuse_height(false),
|
|
|
|
_fuse_pos(false),
|
2016-01-06 14:51:27 -04:00
|
|
|
_fuse_hor_vel(false),
|
|
|
|
_fuse_vert_vel(false),
|
2016-01-28 06:52:39 -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),
|
|
|
|
_vel_pos_innov{},
|
2016-01-12 02:30:53 -04:00
|
|
|
_mag_innov{},
|
|
|
|
_heading_innov{},
|
|
|
|
_vel_pos_innov_var{},
|
|
|
|
_mag_innov_var{},
|
2016-01-28 06:52:39 -04:00
|
|
|
_heading_innov_var{}
|
2015-11-19 13:08:04 -04:00
|
|
|
{
|
2015-12-07 04:26:30 -04:00
|
|
|
_earth_rate_NED.setZero();
|
|
|
|
_R_prev = matrix::Dcm<float>();
|
2015-12-10 11:36:10 -04:00
|
|
|
_delta_angle_corr.setZero();
|
|
|
|
_delta_vel_corr.setZero();
|
|
|
|
_vel_corr.setZero();
|
2016-01-28 06:52:39 -04:00
|
|
|
_last_known_posNE.setZero();
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ekf::~Ekf()
|
|
|
|
{
|
|
|
|
|
2016-01-30 16:42:24 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
bool Ekf::init(uint64_t timestamp)
|
|
|
|
{
|
|
|
|
bool ret = initialise_interface(timestamp);
|
|
|
|
_state.ang_error.setZero();
|
|
|
|
_state.vel.setZero();
|
|
|
|
_state.pos.setZero();
|
|
|
|
_state.gyro_bias.setZero();
|
|
|
|
_state.gyro_scale(0) = 1.0f;
|
|
|
|
_state.gyro_scale(1) = 1.0f;
|
|
|
|
_state.gyro_scale(2) = 1.0f;
|
|
|
|
_state.accel_z_bias = 0.0f;
|
|
|
|
_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>();
|
|
|
|
|
|
|
|
|
|
|
|
_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;
|
|
|
|
_mag_healthy = false;
|
|
|
|
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
|
|
|
{
|
2015-12-07 04:26:30 -04:00
|
|
|
bool ret = false; // indicates if there has been an update
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
if (!_filter_initialised) {
|
|
|
|
_filter_initialised = initialiseFilter();
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-12-10 11:36:10 -04:00
|
|
|
if (!_filter_initialised) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
2015-12-24 08:48:22 -04:00
|
|
|
//printStates();
|
2015-12-10 11:36:10 -04:00
|
|
|
//printStatesFast();
|
2015-11-19 13:08:04 -04:00
|
|
|
// prediction
|
|
|
|
if (_imu_updated) {
|
2015-12-07 04:26:30 -04:00
|
|
|
ret = true;
|
2015-11-19 13:08:04 -04:00
|
|
|
predictState();
|
|
|
|
predictCovariance();
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:52:39 -04:00
|
|
|
// control logic
|
|
|
|
controlFusionModes();
|
2015-12-07 04:26:30 -04:00
|
|
|
|
2016-01-28 06:52:39 -04:00
|
|
|
// measurement updates
|
|
|
|
|
|
|
|
// Fuse magnetometer data using the selected fuson method and only if angular alignment is complete
|
2015-12-06 07:57:43 -04:00
|
|
|
if (_mag_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_mag_sample_delayed)) {
|
2016-01-28 06:52:39 -04:00
|
|
|
if (_control_status.flags.mag_3D && _control_status.flags.angle_align) {
|
|
|
|
fuseMag();
|
|
|
|
if (_control_status.flags.mag_dec) {
|
|
|
|
// TODO need to fuse synthetic declination measurements if there is no GPS or equivalent aiding
|
|
|
|
// otherwise heading will slowly drift
|
|
|
|
}
|
|
|
|
} else if (_control_status.flags.mag_hdg && _control_status.flags.angle_align) {
|
|
|
|
fuseHeading();
|
|
|
|
}
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
if (_baro_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_baro_sample_delayed)) {
|
2015-11-19 13:08:04 -04:00
|
|
|
_fuse_height = true;
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:52:39 -04:00
|
|
|
// If we are using GPs aiding and data has fallen behind the fusion time horizon then fuse it
|
|
|
|
// if we aren't doing any aiding, fake GPS measurements at the last known position to constrain drift
|
|
|
|
// Coincide fake measurements with baro data for efficiency with a minimum fusion rate of 5Hz
|
|
|
|
if (_gps_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_gps_sample_delayed) && _control_status.flags.gps) {
|
2015-11-19 13:08:04 -04:00
|
|
|
_fuse_pos = true;
|
2016-01-06 14:51:27 -04:00
|
|
|
_fuse_vert_vel = true;
|
|
|
|
_fuse_hor_vel = true;
|
2016-01-28 06:52:39 -04:00
|
|
|
} else if (!_control_status.flags.gps && !_control_status.flags.opt_flow && ((_time_last_imu - _time_last_fake_gps > 2e5) || _fuse_height)) {
|
|
|
|
_fuse_pos = true;
|
|
|
|
_gps_sample_delayed.pos(0) = _last_known_posNE(0);
|
|
|
|
_gps_sample_delayed.pos(1) = _last_known_posNE(1);
|
|
|
|
_time_last_fake_gps = _time_last_imu;
|
|
|
|
}
|
2015-12-07 04:26:30 -04:00
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
if (_fuse_height || _fuse_pos || _fuse_hor_vel || _fuse_vert_vel) {
|
2015-12-07 04:26:30 -04:00
|
|
|
fuseVelPosHeight();
|
2016-01-06 14:51:27 -04:00
|
|
|
_fuse_hor_vel = _fuse_vert_vel = _fuse_pos = _fuse_height = false;
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
if (_range_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_range_sample_delayed)) {
|
2015-11-19 13:08:04 -04:00
|
|
|
fuseRange();
|
|
|
|
}
|
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
if (_airspeed_buffer.pop_first_older_than(_imu_sample_delayed.time_us, &_airspeed_sample_delayed)) {
|
2015-11-19 13:08:04 -04:00
|
|
|
fuseAirspeed();
|
|
|
|
}
|
|
|
|
|
2015-12-10 11:36:10 -04:00
|
|
|
calculateOutputStates();
|
2015-12-07 04:26:30 -04:00
|
|
|
|
|
|
|
return ret;
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Ekf::initialiseFilter(void)
|
|
|
|
{
|
|
|
|
_state.ang_error.setZero();
|
|
|
|
_state.vel.setZero();
|
|
|
|
_state.pos.setZero();
|
|
|
|
_state.gyro_bias.setZero();
|
|
|
|
_state.gyro_scale(0) = _state.gyro_scale(1) = _state.gyro_scale(2) = 1.0f;
|
|
|
|
_state.accel_z_bias = 0.0f;
|
|
|
|
_state.mag_I.setZero();
|
|
|
|
_state.mag_B.setZero();
|
|
|
|
_state.wind_vel.setZero();
|
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
// get initial roll and pitch estimate from accel vector, assuming vehicle is static
|
2015-12-06 07:57:43 -04:00
|
|
|
Vector3f accel_init = _imu_down_sampled.delta_vel / _imu_down_sampled.delta_vel_dt;
|
|
|
|
|
|
|
|
float pitch = 0.0f;
|
|
|
|
float roll = 0.0f;
|
|
|
|
|
|
|
|
if (accel_init.norm() > 0.001f) {
|
|
|
|
accel_init.normalize();
|
|
|
|
|
|
|
|
pitch = asinf(accel_init(0));
|
|
|
|
roll = -asinf(accel_init(1) / cosf(pitch));
|
|
|
|
}
|
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
matrix::Euler<float> euler_init(roll, pitch, 0.0f);
|
|
|
|
|
|
|
|
// Get the latest magnetic field measurement.
|
|
|
|
// If we don't have a measurement then we cannot initialise the filter
|
|
|
|
magSample mag_init = _mag_buffer.get_newest();
|
2016-01-02 09:03:03 -04:00
|
|
|
|
2015-12-10 11:36:10 -04:00
|
|
|
if (mag_init.time_us == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
// rotate magnetic field into earth frame assuming zero yaw and estimate yaw angle assuming zero declination
|
|
|
|
// TODO use declination if available
|
|
|
|
matrix::Dcm<float> R_to_earth_zeroyaw(euler_init);
|
|
|
|
Vector3f mag_ef_zeroyaw = R_to_earth_zeroyaw * mag_init.mag;
|
|
|
|
float declination = 0.0f;
|
|
|
|
euler_init(2) = declination - atan2f(mag_ef_zeroyaw(1), mag_ef_zeroyaw(0));
|
2015-12-22 06:22:17 -04:00
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
// calculate initial quaternion states
|
|
|
|
_state.quat_nominal = Quaternion(euler_init);
|
|
|
|
_output_new.quat_nominal = _state.quat_nominal;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-01-28 06:52:39 -04:00
|
|
|
// TODO replace this with a conditional test based on fitered angle error states.
|
|
|
|
_control_status.flags.angle_align = true;
|
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
// calculate initial earth magnetic field states
|
|
|
|
matrix::Dcm<float> R_to_earth(euler_init);
|
|
|
|
_state.mag_I = R_to_earth * mag_init.mag;
|
2015-12-10 11:36:10 -04:00
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
resetVelocity();
|
2015-12-06 07:57:43 -04:00
|
|
|
resetPosition();
|
|
|
|
|
2016-01-06 14:51:27 -04:00
|
|
|
// initialize vertical position with newest baro measurement
|
|
|
|
baroSample baro_init = _baro_buffer.get_newest();
|
|
|
|
|
|
|
|
if (baro_init.time_us == 0) {
|
2016-01-05 02:44:37 -04:00
|
|
|
return false;
|
2016-01-06 14:51:27 -04:00
|
|
|
}
|
2016-01-05 02:44:37 -04:00
|
|
|
|
|
|
|
_state.pos(2) = -baro_init.hgt;
|
2016-01-08 01:48:48 -04:00
|
|
|
_output_new.pos(2) = -baro_init.hgt;
|
2016-01-05 02:44:37 -04:00
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
initialiseCovariance();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ekf::predictState()
|
|
|
|
{
|
2015-12-07 04:26:30 -04:00
|
|
|
if (!_earth_rate_initialised) {
|
2016-01-30 01:05:18 -04:00
|
|
|
if (_NED_origin_initialised) {
|
2016-01-27 17:37:45 -04:00
|
|
|
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
|
|
|
|
|
|
|
// attitude error state prediciton
|
2015-12-07 04:26:30 -04:00
|
|
|
matrix::Dcm<float> R_to_earth(_state.quat_nominal); // transformation matrix from body to world frame
|
2016-01-06 14:51:27 -04:00
|
|
|
Vector3f corrected_delta_ang = _imu_sample_delayed.delta_ang - _R_prev * _earth_rate_NED *
|
|
|
|
_imu_sample_delayed.delta_ang_dt;
|
2015-12-07 04:26:30 -04:00
|
|
|
Quaternion dq; // delta quaternion since last update
|
|
|
|
dq.from_axis_angle(corrected_delta_ang);
|
2015-12-06 07:57:43 -04:00
|
|
|
_state.quat_nominal = dq * _state.quat_nominal;
|
|
|
|
_state.quat_nominal.normalize();
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
_R_prev = R_to_earth.transpose();
|
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
Vector3f vel_last = _state.vel;
|
|
|
|
|
|
|
|
// predict velocity states
|
2015-12-07 04:26:30 -04:00
|
|
|
_state.vel += R_to_earth * _imu_sample_delayed.delta_vel;
|
2015-12-06 07:57:43 -04:00
|
|
|
_state.vel(2) += 9.81f * _imu_sample_delayed.delta_vel_dt;
|
|
|
|
|
|
|
|
// 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();
|
2015-12-06 07:57:43 -04:00
|
|
|
}
|
|
|
|
|
2016-01-30 00:03:25 -04:00
|
|
|
|
|
|
|
bool Ekf::collect_imu(imuSample &imu)
|
|
|
|
{
|
|
|
|
|
|
|
|
imu.delta_ang(0) = imu.delta_ang(0) * _state.gyro_scale(0);
|
|
|
|
imu.delta_ang(1) = imu.delta_ang(1) * _state.gyro_scale(1);
|
|
|
|
imu.delta_ang(2) = imu.delta_ang(2) * _state.gyro_scale(2);
|
|
|
|
|
|
|
|
imu.delta_ang -= _state.gyro_bias * imu.delta_ang_dt / (_dt_imu_avg > 0 ? _dt_imu_avg : 0.01f);
|
|
|
|
imu.delta_vel(2) -= _state.accel_z_bias * imu.delta_vel_dt / (_dt_imu_avg > 0 ? _dt_imu_avg : 0.01f);;
|
|
|
|
|
|
|
|
// store the new sample for the complementary filter prediciton
|
|
|
|
_imu_sample_new = {
|
|
|
|
delta_ang : imu.delta_ang,
|
|
|
|
delta_vel : imu.delta_vel,
|
|
|
|
delta_ang_dt : imu.delta_ang_dt,
|
|
|
|
delta_vel_dt : imu.delta_vel_dt,
|
|
|
|
time_us : imu.time_us
|
|
|
|
};
|
|
|
|
|
|
|
|
_imu_down_sampled.delta_ang_dt += imu.delta_ang_dt;
|
|
|
|
_imu_down_sampled.delta_vel_dt += imu.delta_vel_dt;
|
|
|
|
|
|
|
|
|
|
|
|
Quaternion delta_q;
|
|
|
|
delta_q.rotate(imu.delta_ang);
|
|
|
|
_q_down_sampled = _q_down_sampled * delta_q;
|
|
|
|
_q_down_sampled.normalize();
|
|
|
|
|
|
|
|
matrix::Dcm<float> delta_R(delta_q.inversed());
|
|
|
|
_imu_down_sampled.delta_vel = delta_R * _imu_down_sampled.delta_vel;
|
|
|
|
_imu_down_sampled.delta_vel += imu.delta_vel;
|
|
|
|
|
2016-01-30 16:42:24 -04:00
|
|
|
if ((_dt_imu_avg * _imu_ticks >= (float)(FILTER_UPDATE_PERRIOD_MS) / 1000) ||
|
2016-01-30 00:03:25 -04:00
|
|
|
_dt_imu_avg * _imu_ticks >= 0.02f){
|
2016-01-30 03:45:50 -04:00
|
|
|
imu = {
|
2016-01-30 00:03:25 -04:00
|
|
|
delta_ang : _q_down_sampled.to_axis_angle(),
|
|
|
|
delta_vel : _imu_down_sampled.delta_vel,
|
|
|
|
delta_ang_dt : _imu_down_sampled.delta_ang_dt,
|
|
|
|
delta_vel_dt : _imu_down_sampled.delta_vel_dt,
|
|
|
|
time_us : imu.time_us
|
|
|
|
};
|
|
|
|
_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;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 11:36:10 -04:00
|
|
|
void Ekf::calculateOutputStates()
|
|
|
|
{
|
|
|
|
imuSample imu_new = _imu_sample_new;
|
|
|
|
Vector3f delta_angle;
|
|
|
|
|
2016-01-14 11:59:04 -04:00
|
|
|
// Note: We do no not need to consider any bias or scale correction here
|
|
|
|
// since the base class has already corrected the imu sample
|
|
|
|
delta_angle(0) = imu_new.delta_ang(0);
|
|
|
|
delta_angle(1) = imu_new.delta_ang(1);
|
|
|
|
delta_angle(2) = imu_new.delta_ang(2);
|
2015-12-10 11:36:10 -04:00
|
|
|
|
|
|
|
Vector3f delta_vel = imu_new.delta_vel;
|
|
|
|
|
|
|
|
delta_angle += _delta_angle_corr;
|
|
|
|
Quaternion dq;
|
|
|
|
dq.from_axis_angle(delta_angle);
|
|
|
|
|
|
|
|
_output_new.time_us = imu_new.time_us;
|
|
|
|
_output_new.quat_nominal = dq * _output_new.quat_nominal;
|
|
|
|
_output_new.quat_nominal.normalize();
|
|
|
|
|
|
|
|
matrix::Dcm<float> R_to_earth(_output_new.quat_nominal);
|
|
|
|
|
|
|
|
Vector3f delta_vel_NED = R_to_earth * delta_vel + _delta_vel_corr;
|
|
|
|
delta_vel_NED(2) += 9.81f * imu_new.delta_vel_dt;
|
|
|
|
|
|
|
|
Vector3f vel_last = _output_new.vel;
|
|
|
|
|
|
|
|
_output_new.vel += delta_vel_NED;
|
|
|
|
|
|
|
|
_output_new.pos += (_output_new.vel + vel_last) * (imu_new.delta_vel_dt * 0.5f) + _vel_corr * imu_new.delta_vel_dt;
|
|
|
|
|
|
|
|
if (_imu_updated) {
|
|
|
|
_output_buffer.push(_output_new);
|
|
|
|
_imu_updated = false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 11:59:04 -04:00
|
|
|
_output_sample_delayed = _output_buffer.get_oldest();
|
2015-12-10 11:36:10 -04:00
|
|
|
|
|
|
|
Quaternion quat_inv = _state.quat_nominal.inversed();
|
|
|
|
Quaternion q_error = _output_sample_delayed.quat_nominal * quat_inv;
|
|
|
|
q_error.normalize();
|
|
|
|
Vector3f delta_ang_error;
|
|
|
|
|
|
|
|
float scalar;
|
|
|
|
|
|
|
|
if (q_error(0) >= 0.0f) {
|
|
|
|
scalar = -2.0f;
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-12-10 11:36:10 -04: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);
|
|
|
|
|
|
|
|
_delta_angle_corr = delta_ang_error * imu_new.delta_ang_dt;
|
|
|
|
|
|
|
|
_delta_vel_corr = (_state.vel - _output_sample_delayed.vel) * imu_new.delta_vel_dt;
|
|
|
|
|
|
|
|
_vel_corr = (_state.pos - _output_sample_delayed.pos);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-06 07:57:43 -04:00
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
void Ekf::fuseAirspeed()
|
2015-12-06 07:57:43 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
void Ekf::fuseRange()
|
2015-12-06 07:57:43 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
void Ekf::printStates()
|
2015-12-06 07:57:43 -04:00
|
|
|
{
|
2015-12-07 04:26:30 -04:00
|
|
|
static int counter = 0;
|
|
|
|
|
|
|
|
if (counter % 50 == 0) {
|
2015-12-10 04:07:00 -04:00
|
|
|
printf("quaternion\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2015-12-10 04:07:00 -04:00
|
|
|
printf("quat %i %.5f\n", i, (double)_state.quat_nominal(i));
|
2015-12-07 04:26:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
matrix::Euler<float> euler(_state.quat_nominal);
|
|
|
|
printf("yaw pitch roll %.5f %.5f %.5f\n", (double)euler(2), (double)euler(1), (double)euler(0));
|
|
|
|
|
|
|
|
printf("vel\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-07 04:26:30 -04:00
|
|
|
printf("v %i %.5f\n", i, (double)_state.vel(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("pos\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-07 04:26:30 -04:00
|
|
|
printf("p %i %.5f\n", i, (double)_state.pos(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("gyro_scale\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-07 04:26:30 -04:00
|
|
|
printf("gs %i %.5f\n", i, (double)_state.gyro_scale(i));
|
|
|
|
}
|
|
|
|
|
2015-12-10 04:07:00 -04:00
|
|
|
printf("mag earth\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-07 04:26:30 -04:00
|
|
|
printf("mI %i %.5f\n", i, (double)_state.mag_I(i));
|
|
|
|
}
|
2015-12-10 04:07:00 -04:00
|
|
|
|
|
|
|
printf("mag bias\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-10 04:07:00 -04:00
|
|
|
printf("mB %i %.5f\n", i, (double)_state.mag_B(i));
|
|
|
|
}
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
counter = 0;
|
|
|
|
}
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-12-07 04:26:30 -04:00
|
|
|
counter++;
|
2015-12-06 07:57:43 -04:00
|
|
|
|
|
|
|
}
|
2015-12-10 11:36:10 -04:00
|
|
|
|
|
|
|
void Ekf::printStatesFast()
|
|
|
|
{
|
|
|
|
static int counter_fast = 0;
|
|
|
|
|
|
|
|
if (counter_fast % 50 == 0) {
|
|
|
|
printf("quaternion\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2015-12-10 11:36:10 -04:00
|
|
|
printf("quat %i %.5f\n", i, (double)_output_new.quat_nominal(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("vel\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-10 11:36:10 -04:00
|
|
|
printf("v %i %.5f\n", i, (double)_output_new.vel(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("pos\n");
|
2016-01-06 14:51:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-12-10 11:36:10 -04:00
|
|
|
printf("p %i %.5f\n", i, (double)_output_new.pos(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_fast = 0;
|
|
|
|
}
|
2016-01-06 14:51:27 -04:00
|
|
|
|
2015-12-10 11:36:10 -04:00
|
|
|
counter_fast++;
|
|
|
|
}
|