2016-01-28 06:52:39 -04:00
/****************************************************************************
*
* 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 control . cpp
* Control functions for ekf attitude and position estimator .
*
* @ author Paul Riseborough < p_riseborough @ live . com . au >
*
*/
# include "ekf.h"
void Ekf : : controlFusionModes ( )
{
2016-01-31 04:01:44 -04:00
// Determine the vehicle status
calculateVehicleStatus ( ) ;
2016-02-11 23:27:25 -04:00
// Get the magnetic declination
calcMagDeclination ( ) ;
2016-03-07 05:31:19 -04:00
// Check for tilt convergence during initial alignment
// filter the tilt error vector using a 1 sec time constant LPF
float filt_coef = 1.0f * _imu_sample_delayed . delta_ang_dt ;
_tilt_err_length_filt = filt_coef * _tilt_err_vec . norm ( ) + ( 1.0f - filt_coef ) * _tilt_err_length_filt ;
// Once the tilt error has reduced sufficiently, initialise the yaw and magnetic field states
if ( _tilt_err_length_filt < 0.005f & & ! _control_status . flags . tilt_align ) {
_control_status . flags . tilt_align = true ;
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
}
2016-01-31 04:01:44 -04:00
// optical flow fusion mode selection logic
2016-03-07 05:31:19 -04:00
// to start using optical flow data we need angular alignment complete, and fresh optical flow and height above terrain data
if ( ( _params . fusion_mode & MASK_USE_OF ) & & ! _control_status . flags . opt_flow & & _control_status . flags . tilt_align
& & ( _time_last_imu - _time_last_optflow ) < 5e5 & & ( _time_last_imu - _time_last_hagl_fuse ) < 5e5 ) {
// If the heading is not aligned, reset the yaw and magnetic field states
if ( ! _control_status . flags . yaw_align ) {
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
}
// If the heading is valid, start using optical flow aiding
if ( _control_status . flags . yaw_align ) {
// set the flag and reset the fusion timeout
_control_status . flags . opt_flow = true ;
_time_last_of_fuse = _time_last_imu ;
// if we are not using GPS and are in air, then we need to reset the velocity to be consistent with the optical flow reading
if ( ! _control_status . flags . gps ) {
// calculate the rotation matrix from body to earth frame
matrix : : Dcm < float > body_to_earth ( _state . quat_nominal ) ;
// constrain height above ground to be above minimum possible
float heightAboveGndEst = fmaxf ( ( _terrain_vpos - _state . pos ( 2 ) ) , _params . rng_gnd_clearance ) ;
// calculate absolute distance from focal point to centre of frame assuming a flat earth
float range = heightAboveGndEst / body_to_earth ( 2 , 2 ) ;
if ( _in_air & & ( range - _params . rng_gnd_clearance ) > 0.3f & & _flow_sample_delayed . dt > 0.05f ) {
// calculate X and Y body relative velocities from OF measurements
Vector3f vel_optflow_body ;
vel_optflow_body ( 0 ) = - range * _flow_sample_delayed . flowRadXYcomp ( 1 ) / _flow_sample_delayed . dt ;
vel_optflow_body ( 1 ) = range * _flow_sample_delayed . flowRadXYcomp ( 0 ) / _flow_sample_delayed . dt ;
vel_optflow_body ( 2 ) = 0.0f ;
// rotate from body to earth frame
Vector3f vel_optflow_earth ;
vel_optflow_earth = body_to_earth * vel_optflow_body ;
// take x and Y components
_state . vel ( 0 ) = vel_optflow_earth ( 0 ) ;
_state . vel ( 1 ) = vel_optflow_earth ( 1 ) ;
} else {
_state . vel . setZero ( ) ;
}
}
}
} else if ( ! ( _params . fusion_mode & MASK_USE_OF ) ) {
_control_status . flags . opt_flow = false ;
}
2016-01-31 04:01:44 -04:00
// GPS fusion mode selection logic
2016-03-07 05:31:19 -04:00
// To start use GPS we need angular alignment completed, the local NED origin set and fresh GPS data
if ( ( _params . fusion_mode & MASK_USE_GPS ) & & ! _control_status . flags . gps ) {
2016-02-11 23:28:40 -04:00
if ( _control_status . flags . tilt_align & & ( _time_last_imu - _time_last_gps ) < 5e5 & & _NED_origin_initialised
2016-01-31 04:01:44 -04:00
& & ( _time_last_imu - _last_gps_fail_us > 5e6 ) ) {
2016-02-24 05:13:42 -04:00
// If the heading is not aligned, reset the yaw and magnetic field states
if ( ! _control_status . flags . yaw_align ) {
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
}
2016-02-11 23:34:12 -04:00
// If the heading is valid, reset the positon and velocity and start using gps aiding
if ( _control_status . flags . yaw_align ) {
2016-02-11 23:38:24 -04:00
resetPosition ( ) ;
resetVelocity ( ) ;
2016-02-11 23:34:12 -04:00
_control_status . flags . gps = true ;
2016-03-07 05:31:19 -04:00
_time_last_gps = _time_last_imu ;
2016-02-11 23:34:12 -04:00
}
2016-01-31 04:01:44 -04:00
}
2016-03-07 05:31:19 -04:00
} else if ( ! ( _params . fusion_mode & MASK_USE_GPS ) ) {
_control_status . flags . gps = false ;
2016-01-31 04:01:44 -04:00
}
// handle the case when we are relying on GPS fusion and lose it
if ( _control_status . flags . gps & & ! _control_status . flags . opt_flow ) {
// We are relying on GPS aiding to constrain attitude drift so after 10 seconds without aiding we need to do something
if ( ( _time_last_imu - _time_last_pos_fuse > 10e6 ) & & ( _time_last_imu - _time_last_vel_fuse > 10e6 ) ) {
if ( _time_last_imu - _time_last_gps > 5e5 ) {
// if we don't have gps then we need to switch to the non-aiding mode, zero the veloity states
// and set the synthetic GPS position to the current estimate
_control_status . flags . gps = false ;
_last_known_posNE ( 0 ) = _state . pos ( 0 ) ;
_last_known_posNE ( 1 ) = _state . pos ( 1 ) ;
_state . vel . setZero ( ) ;
} else {
// Reset states to the last GPS measurement
resetPosition ( ) ;
resetVelocity ( ) ;
}
}
}
2016-02-24 21:53:58 -04:00
// Handle the case where we have rejected height measurements for an extended period
// This excessive vibration levels can cause this so a reset gives the filter a chance to recover
// After 10 seconds without aiding we reset to the height measurement provided the data is fresh
if ( ( _time_last_imu - _time_last_hgt_fuse > 10e6 ) & & ( _time_last_imu - _time_last_baro < 5e5 ) ) {
// Reset vertical position and velocity states to the last measurement
resetHeight ( ) ;
}
2016-01-31 04:01:44 -04:00
// handle the case when we are relying on optical flow fusion and lose it
if ( _control_status . flags . opt_flow & & ! _control_status . flags . gps ) {
2016-03-07 05:31:19 -04:00
// We are relying on flow aiding to constrain attitude drift so after 5s without aiding we need to do something
if ( ( _time_last_imu - _time_last_of_fuse > 5e6 ) ) {
// Switch to the non-aiding mode, zero the veloity states
// and set the synthetic position to the current estimate
_control_status . flags . opt_flow = false ;
_last_known_posNE ( 0 ) = _state . pos ( 0 ) ;
_last_known_posNE ( 1 ) = _state . pos ( 1 ) ;
_state . vel . setZero ( ) ;
}
2016-01-31 04:01:44 -04:00
}
// Determine if we should use simple magnetic heading fusion which works better when there are large external disturbances
// or the more accurate 3-axis fusion
2016-02-11 23:38:24 -04:00
if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_AUTO ) {
if ( ! _control_status . flags . armed ) {
2016-03-01 00:35:17 -04:00
// use heading fusion for initial startup
_control_status . flags . mag_hdg = true ;
_control_status . flags . mag_2D = false ;
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_3D = false ;
} else {
if ( _control_status . flags . in_air ) {
2016-03-01 00:35:45 -04:00
// if transitioning into 3-axis fusion mode, we need to initialise the yaw angle and field states
2016-02-24 05:17:22 -04:00
if ( ! _control_status . flags . mag_3D ) {
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
}
2016-03-01 00:35:45 -04:00
// use 3D mag fusion when airborne
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_hdg = false ;
2016-02-19 23:42:17 -04:00
_control_status . flags . mag_2D = false ;
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_3D = true ;
} else {
2016-03-01 00:35:17 -04:00
// use heading fusion when on the ground
_control_status . flags . mag_hdg = true ;
_control_status . flags . mag_2D = false ;
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_3D = false ;
}
}
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_HEADING ) {
2016-03-01 00:35:17 -04:00
// always use heading fusion
_control_status . flags . mag_hdg = true ;
_control_status . flags . mag_2D = false ;
2016-02-19 23:42:17 -04:00
_control_status . flags . mag_3D = false ;
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_2D ) {
// always use 2D mag fusion
_control_status . flags . mag_hdg = false ;
_control_status . flags . mag_2D = true ;
2016-01-31 04:01:44 -04:00
_control_status . flags . mag_3D = false ;
2016-02-11 23:38:24 -04:00
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_3D ) {
2016-03-01 00:35:45 -04:00
// if transitioning into 3-axis fusion mode, we need to initialise the yaw angle and field states
2016-02-24 05:17:22 -04:00
if ( ! _control_status . flags . mag_3D ) {
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
}
2016-02-11 23:38:24 -04:00
// always use 3-axis mag fusion
_control_status . flags . mag_hdg = false ;
2016-02-19 23:42:17 -04:00
_control_status . flags . mag_2D = false ;
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_3D = true ;
2016-01-31 04:01:44 -04:00
2016-02-11 23:38:24 -04:00
} else {
// do no magnetometer fusion at all
_control_status . flags . mag_hdg = false ;
2016-02-19 23:42:17 -04:00
_control_status . flags . mag_2D = false ;
2016-02-11 23:38:24 -04:00
_control_status . flags . mag_3D = false ;
2016-01-31 04:01:44 -04:00
}
2016-02-07 23:15:41 -04:00
2016-02-11 23:38:24 -04:00
// if we are using 3-axis magnetometer fusion, but without external aiding, then the declination must be fused as an observation to prevent long term heading drift
// fusing declination when gps aiding is available is optional, but recommneded to prevent problem if the vehicle is static for extended periods of time
if ( _control_status . flags . mag_3D & & ( ! _control_status . flags . gps | | ( _params . mag_declination_source & MASK_FUSE_DECL ) ) ) {
2016-02-08 16:44:34 -04:00
_control_status . flags . mag_dec = true ;
2016-02-08 00:12:38 -04:00
} else {
2016-02-08 16:44:34 -04:00
_control_status . flags . mag_dec = false ;
2016-02-08 00:12:38 -04:00
}
2016-02-11 23:38:24 -04:00
2016-03-07 05:31:19 -04:00
// Control the soure of height measurements for the main filter
_control_status . flags . baro_hgt = true ;
_control_status . flags . rng_hgt = false ;
_control_status . flags . gps_hgt = false ;
2016-02-15 19:04:37 -04:00
// Placeholder for control of wind velocity states estimation
// TODO add methods for true airspeed and/or sidelsip fusion or some type of drag force measurement
if ( false ) {
_control_status . flags . wind = false ;
}
2016-02-15 19:40:05 -04:00
// Store the status to enable change detection
_control_status_prev . value = _control_status . value ;
2016-01-28 06:52:39 -04:00
}
void Ekf : : calculateVehicleStatus ( )
{
2016-01-31 04:01:44 -04:00
// determine if the vehicle is armed
_control_status . flags . armed = _vehicle_armed ;
// record vertical position whilst disarmed to use as a height change reference
if ( ! _control_status . flags . armed ) {
_last_disarmed_posD = _state . pos ( 2 ) ;
}
// Transition to in-air occurs when armed and when altitude has increased sufficiently from the altitude at arming
2016-03-03 05:46:48 -04:00
bool in_air = _control_status . flags . armed & & ( _state . pos ( 2 ) - _last_disarmed_posD ) < - 1.0f ;
2016-03-07 05:31:19 -04:00
2016-03-03 05:46:48 -04:00
if ( ! _control_status . flags . in_air & & in_air ) {
2016-01-31 04:01:44 -04:00
_control_status . flags . in_air = true ;
}
2016-03-03 05:46:48 -04:00
// Transition to on-ground occurs when disarmed or if the land detector indicated landed state
if ( _control_status . flags . in_air & & ( ! _control_status . flags . armed | | ! _in_air ) ) {
2016-01-31 04:01:44 -04:00
_control_status . flags . in_air = false ;
}
2016-01-28 06:52:39 -04:00
}