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 >
*
*/
2016-06-02 12:29:22 -03:00
# include "../ecl.h"
2016-01-28 06:52:39 -04:00
# include "ekf.h"
2018-03-22 15:10:30 -03:00
# include <mathlib/mathlib.h>
2016-01-28 06:52:39 -04:00
void Ekf : : controlFusionModes ( )
{
2016-04-05 10:14:04 -03:00
// Store the status to enable change detection
_control_status_prev . value = _control_status . value ;
2016-01-31 04:01:44 -04:00
2016-02-11 23:27:25 -04:00
// Get the magnetic declination
calcMagDeclination ( ) ;
2016-05-16 21:42:37 -03:00
// monitor the tilt alignment
if ( ! _control_status . flags . tilt_align ) {
// whilst we are aligning the tilt, monitor the variances
Vector3f angle_err_var_vec = calcRotVecVariances ( ) ;
2016-05-18 07:34:12 -03:00
// Once the tilt variances have reduced to equivalent of 3deg uncertainty, re-set the yaw and magnetic field states
// and declare the tilt alignment complete
if ( ( angle_err_var_vec ( 0 ) + angle_err_var_vec ( 1 ) ) < sq ( 0.05235f ) ) {
2016-05-16 21:42:37 -03:00
_control_status . flags . tilt_align = true ;
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) ;
2016-12-28 00:58:03 -04:00
// send alignment status message to the console
if ( _control_status . flags . baro_hgt ) {
2018-05-03 15:14:12 -03:00
ECL_INFO ( " EKF aligned, (pressure height, IMU buf: %i, OBS buf: %i) " , ( int ) _imu_buffer_length , ( int ) _obs_buffer_length ) ;
2016-12-28 00:58:03 -04:00
} else if ( _control_status . flags . ev_hgt ) {
2018-05-03 15:14:12 -03:00
ECL_INFO ( " EKF aligned, (EV height, IMU buf: %i, OBS buf: %i) " , ( int ) _imu_buffer_length , ( int ) _obs_buffer_length ) ;
2016-12-28 00:58:03 -04:00
} else if ( _control_status . flags . gps_hgt ) {
2018-05-03 15:14:12 -03:00
ECL_INFO ( " EKF aligned, (GPS height, IMU buf: %i, OBS buf: %i) " , ( int ) _imu_buffer_length , ( int ) _obs_buffer_length ) ;
2016-12-28 00:58:03 -04:00
} else if ( _control_status . flags . rng_hgt ) {
2018-05-03 15:14:12 -03:00
ECL_INFO ( " EKF aligned, (range height, IMU buf: %i, OBS buf: %i) " , ( int ) _imu_buffer_length , ( int ) _obs_buffer_length ) ;
2016-12-28 00:58:03 -04:00
} else {
2018-05-03 15:14:12 -03:00
ECL_ERR ( " EKF aligned, (unknown height, IMU buf: %i, OBS buf: %i) " , ( int ) _imu_buffer_length , ( int ) _obs_buffer_length ) ;
2016-12-28 00:58:03 -04:00
}
2016-06-07 03:54:12 -03:00
2016-05-16 21:42:37 -03:00
}
2016-06-07 03:54:12 -03:00
2016-03-07 05:31:19 -04:00
}
2017-06-29 12:07:58 -03:00
// check faultiness (before pop_first_older_than) to see if we can change back to original height sensor
2018-05-03 15:14:12 -03:00
const baroSample & baro_init = _baro_buffer . get_newest ( ) ;
2017-06-29 12:07:58 -03:00
_baro_hgt_faulty = ! ( ( _time_last_imu - baro_init . time_us ) < 2 * BARO_MAX_INTERVAL ) ;
2017-11-14 21:14:33 -04:00
2018-05-03 15:14:12 -03:00
const gpsSample & gps_init = _gps_buffer . get_newest ( ) ;
2017-06-29 12:07:58 -03:00
_gps_hgt_faulty = ! ( ( _time_last_imu - gps_init . time_us ) < 2 * GPS_MAX_INTERVAL ) ;
2017-11-14 21:14:33 -04:00
2018-05-03 15:14:12 -03:00
const rangeSample & rng_init = _range_buffer . get_newest ( ) ;
2017-06-29 12:07:58 -03:00
_rng_hgt_faulty = ! ( ( _time_last_imu - rng_init . time_us ) < 2 * RNG_MAX_INTERVAL ) ;
2016-06-07 03:54:12 -03:00
// check for arrival of new sensor data at the fusion time horizon
_gps_data_ready = _gps_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _gps_sample_delayed ) ;
_mag_data_ready = _mag_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _mag_sample_delayed ) ;
2017-02-28 05:49:29 -04:00
_delta_time_baro_us = _baro_sample_delayed . time_us ;
2016-06-07 03:54:12 -03:00
_baro_data_ready = _baro_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _baro_sample_delayed ) ;
2016-11-27 17:39:37 -04:00
2017-02-28 05:49:29 -04:00
// if we have a new baro sample save the delta time between this sample and the last sample which is
// used below for baro offset calculations
if ( _baro_data_ready ) {
_delta_time_baro_us = _baro_sample_delayed . time_us - _delta_time_baro_us ;
}
2016-11-27 17:39:37 -04:00
// calculate 2,2 element of rotation matrix from sensor frame to earth frame
_R_rng_to_earth_2_2 = _R_to_earth ( 2 , 0 ) * _sin_tilt_rng + _R_to_earth ( 2 , 2 ) * _cos_tilt_rng ;
2016-06-07 03:54:12 -03:00
_range_data_ready = _range_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _range_sample_delayed )
2018-05-03 15:14:12 -03:00
& & ( _R_rng_to_earth_2_2 > _params . range_cos_max_tilt ) ;
2016-11-27 17:39:37 -04:00
2017-07-31 12:59:16 -03:00
checkForStuckRange ( ) ;
2016-06-07 03:54:12 -03:00
_flow_data_ready = _flow_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _flow_sample_delayed )
2018-05-03 15:14:12 -03:00
& & ( _R_to_earth ( 2 , 2 ) > 0.7071f ) ;
2016-06-07 03:54:12 -03:00
_ev_data_ready = _ext_vision_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _ev_sample_delayed ) ;
_tas_data_ready = _airspeed_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _airspeed_sample_delayed ) ;
// check for height sensor timeouts and reset and change sensor if necessary
controlHeightSensorTimeouts ( ) ;
// control use of observations for aiding
2016-06-07 05:44:47 -03:00
controlMagFusion ( ) ;
controlOpticalFlowFusion ( ) ;
controlGpsFusion ( ) ;
controlAirDataFusion ( ) ;
2016-07-12 14:54:14 -03:00
controlBetaFusion ( ) ;
2017-04-09 20:21:29 -03:00
controlDragFusion ( ) ;
2017-02-28 05:56:39 -04:00
controlHeightFusion ( ) ;
2016-06-07 03:54:12 -03:00
2017-07-26 04:10:52 -03:00
// For efficiency, fusion of direct state observations for position and velocity is performed sequentially
// in a single function using sensor data from multiple sources (GPS, baro, range finder, etc)
2016-06-07 03:54:12 -03:00
controlVelPosFusion ( ) ;
2016-04-24 09:20:18 -03:00
2017-11-17 16:44:28 -04:00
// Additional data from an external vision pose estimator can be fused.
2017-07-26 04:10:52 -03:00
controlExternalVisionFusion ( ) ;
2017-11-17 16:44:28 -04:00
// Additional NE velocity data from an auxiliary sensor can be fused
controlAuxVelFusion ( ) ;
2018-03-21 19:31:52 -03:00
// check if we are no longer fusing measurements that directly constrain velocity drift
update_deadreckoning_status ( ) ;
2016-04-24 09:20:18 -03:00
}
2016-06-07 05:44:47 -03:00
void Ekf : : controlExternalVisionFusion ( )
2016-04-24 09:20:18 -03:00
{
2016-06-07 03:54:12 -03:00
// Check for new exernal vision data
if ( _ev_data_ready ) {
2017-10-22 18:28:22 -03:00
// if the ev data is not in a NED reference frame, then the transformation between EV and EKF navigation frames
// needs to be calculated and the observations rotated into the EKF frame of reference
2018-05-03 15:14:12 -03:00
if ( ( _params . fusion_mode & MASK_ROTATE_EV ) & & ( _params . fusion_mode & MASK_USE_EVPOS )
& & ! ( _params . fusion_mode & MASK_USE_EVYAW ) ) {
2017-10-22 18:28:22 -03:00
// rotate EV measurements into the EKF Navigation frame
calcExtVisRotMat ( ) ;
}
2016-06-07 03:54:12 -03:00
// external vision position aiding selection logic
2018-05-03 15:14:12 -03:00
if ( ( _params . fusion_mode & MASK_USE_EVPOS ) & & ! _control_status . flags . ev_pos & & _control_status . flags . tilt_align
& & _control_status . flags . yaw_align ) {
2016-06-07 03:54:12 -03:00
// check for a exernal vision measurement that has fallen behind the fusion time horizon
if ( _time_last_imu - _time_last_ext_vision < 2 * EV_MAX_INTERVAL ) {
2017-07-26 04:10:52 -03:00
// turn on use of external vision measurements for position
_control_status . flags . ev_pos = true ;
2016-12-28 00:58:03 -04:00
ECL_INFO ( " EKF commencing external vision position fusion " ) ;
2017-07-26 04:10:52 -03:00
// reset the position if we are not already aiding using GPS, else use a relative position
// method for fusing the position data
if ( _control_status . flags . gps ) {
2017-10-22 18:28:22 -03:00
_fuse_hpos_as_odom = true ;
2017-07-26 04:10:52 -03:00
} else {
resetPosition ( ) ;
resetVelocity ( ) ;
}
2016-06-07 03:54:12 -03:00
}
2016-03-20 07:00:52 -03:00
}
2016-06-07 03:54:12 -03:00
// external vision yaw aiding selection logic
if ( ( _params . fusion_mode & MASK_USE_EVYAW ) & & ! _control_status . flags . ev_yaw & & _control_status . flags . tilt_align ) {
2017-07-26 04:10:52 -03:00
// don't start using EV data unless daa is arriving frequently
2016-06-07 03:54:12 -03:00
if ( _time_last_imu - _time_last_ext_vision < 2 * EV_MAX_INTERVAL ) {
// reset the yaw angle to the value from the observaton quaternion
// get the roll, pitch, yaw estimates from the quaternion states
2017-06-19 12:10:01 -03:00
Quatf q_init ( _state . quat_nominal ) ;
Eulerf euler_init ( q_init ) ;
2016-06-07 03:54:12 -03:00
// get initial yaw from the observation quaternion
2018-05-03 15:14:12 -03:00
const extVisionSample & ev_newest = _ext_vision_buffer . get_newest ( ) ;
2017-06-19 12:10:01 -03:00
Quatf q_obs ( ev_newest . quat ) ;
Eulerf euler_obs ( q_obs ) ;
2016-06-07 03:54:12 -03:00
euler_init ( 2 ) = euler_obs ( 2 ) ;
// save a copy of the quaternion state for later use in calculating the amount of reset change
2017-06-19 12:10:01 -03:00
Quatf quat_before_reset = _state . quat_nominal ;
2016-06-07 03:54:12 -03:00
// calculate initial quaternion states for the ekf
2017-06-19 12:10:01 -03:00
_state . quat_nominal = Quatf ( euler_init ) ;
2016-06-07 03:54:12 -03:00
// calculate the amount that the quaternion has changed by
2017-08-30 10:08:02 -03:00
_state_reset_status . quat_change = quat_before_reset . inversed ( ) * _state . quat_nominal ;
2016-06-07 03:54:12 -03:00
// add the reset amount to the output observer buffered data
2017-11-15 20:52:19 -04:00
// Note q1 *= q2 is equivalent to q1 = q2 * q1
2017-11-14 21:14:33 -04:00
for ( uint8_t i = 0 ; i < _output_buffer . get_length ( ) ; i + + ) {
_output_buffer [ i ] . quat_nominal * = _state_reset_status . quat_change ;
2016-06-07 03:54:12 -03:00
}
2016-05-23 07:45:03 -03:00
2016-10-11 12:05:05 -03:00
// apply the change in attitude quaternion to our newest quaternion estimate
// which was already taken out from the output buffer
2017-08-30 10:08:02 -03:00
_output_new . quat_nominal = _state_reset_status . quat_change * _output_new . quat_nominal ;
2016-10-11 12:05:05 -03:00
2016-06-07 03:54:12 -03:00
// capture the reset event
_state_reset_status . quat_counter + + ;
2016-05-23 07:45:03 -03:00
2016-06-07 03:54:12 -03:00
// flag the yaw as aligned
_control_status . flags . yaw_align = true ;
2016-03-20 09:22:53 -03:00
2016-06-07 03:54:12 -03:00
// turn on fusion of external vision yaw measurements and disable all magnetoemter fusion
_control_status . flags . ev_yaw = true ;
_control_status . flags . mag_hdg = false ;
_control_status . flags . mag_3D = false ;
_control_status . flags . mag_dec = false ;
2016-05-14 03:40:34 -03:00
2016-12-28 00:58:03 -04:00
ECL_INFO ( " EKF commencing external vision yaw fusion " ) ;
2016-06-07 03:54:12 -03:00
}
2016-03-20 09:22:53 -03:00
}
2016-03-20 07:00:52 -03:00
2017-07-26 04:10:52 -03:00
// determine if we should start using the height observations
2016-06-07 03:54:12 -03:00
if ( _params . vdist_sensor_type = = VDIST_SENSOR_EV ) {
2018-01-15 05:15:56 -04:00
// don't start using EV data unless data is arriving frequently
2017-07-26 04:10:52 -03:00
if ( ! _control_status . flags . ev_hgt & & ( _time_last_imu - _time_last_ext_vision < 2 * EV_MAX_INTERVAL ) ) {
setControlEVHeight ( ) ;
resetHeight ( ) ;
}
}
2016-04-24 09:20:18 -03:00
2017-07-26 04:10:52 -03:00
// determine if we should use the vertical position observation
if ( _control_status . flags . ev_hgt ) {
_fuse_height = true ;
2016-03-07 05:31:19 -04:00
}
2016-06-07 03:54:12 -03:00
// determine if we should use the horizontal position observations
if ( _control_status . flags . ev_pos ) {
_fuse_pos = true ;
2016-03-07 05:31:19 -04:00
2016-06-07 03:54:12 -03:00
// correct position and height for offset relative to IMU
Vector3f pos_offset_body = _params . ev_pos_body - _params . imu_pos_body ;
Vector3f pos_offset_earth = _R_to_earth * pos_offset_body ;
_ev_sample_delayed . posNED ( 0 ) - = pos_offset_earth ( 0 ) ;
_ev_sample_delayed . posNED ( 1 ) - = pos_offset_earth ( 1 ) ;
_ev_sample_delayed . posNED ( 2 ) - = pos_offset_earth ( 2 ) ;
2017-07-26 04:10:52 -03:00
2017-10-22 18:28:22 -03:00
// Use an incremental position fusion method for EV data if using GPS or if the observations are not in NED
if ( _control_status . flags . gps | | ( _params . fusion_mode & MASK_ROTATE_EV ) ) {
_fuse_hpos_as_odom = true ;
2018-05-03 15:14:12 -03:00
2017-10-22 18:28:22 -03:00
} else {
_fuse_hpos_as_odom = false ;
}
2018-05-03 15:14:12 -03:00
if ( _fuse_hpos_as_odom ) {
if ( ! _hpos_prev_available ) {
2017-10-22 18:28:22 -03:00
// no previous observation available to calculate position change
_fuse_pos = false ;
_hpos_prev_available = true ;
} else {
// calculate the change in position since the last measurement
Vector3f ev_delta_pos = _ev_sample_delayed . posNED - _pos_meas_prev ;
// rotate measurement into body frame if required
if ( _params . fusion_mode & MASK_ROTATE_EV ) {
ev_delta_pos = _ev_rot_mat * ev_delta_pos ;
}
// use the change in position since the last measurement
_vel_pos_innov [ 3 ] = _state . pos ( 0 ) - _hpos_pred_prev ( 0 ) - ev_delta_pos ( 0 ) ;
_vel_pos_innov [ 4 ] = _state . pos ( 1 ) - _hpos_pred_prev ( 1 ) - ev_delta_pos ( 1 ) ;
}
// record observation and estimate for use next time
_pos_meas_prev = _ev_sample_delayed . posNED ;
_hpos_pred_prev ( 0 ) = _state . pos ( 0 ) ;
_hpos_pred_prev ( 1 ) = _state . pos ( 1 ) ;
} else {
// use the absolute position
_vel_pos_innov [ 3 ] = _state . pos ( 0 ) - _ev_sample_delayed . posNED ( 0 ) ;
_vel_pos_innov [ 4 ] = _state . pos ( 1 ) - _ev_sample_delayed . posNED ( 1 ) ;
2018-04-09 05:35:15 -03:00
// check if we have been deadreckoning too long
if ( _time_last_imu - _time_last_pos_fuse > _params . no_gps_timeout_max ) {
// don't reset velocity if we have another source of aiding constraining it
if ( _time_last_imu - _time_last_of_fuse > ( uint64_t ) 1E6 ) {
resetVelocity ( ) ;
}
2018-05-03 15:14:12 -03:00
2018-04-09 05:35:15 -03:00
resetPosition ( ) ;
}
2017-07-26 04:10:52 -03:00
}
2017-10-22 18:28:22 -03:00
// observation 1-STD error
_posObsNoiseNE = fmaxf ( _ev_sample_delayed . posErr , 0.01f ) ;
// innovation gate size
_posInnovGateNE = fmaxf ( _params . ev_innov_gate , 1.0f ) ;
2017-07-26 04:10:52 -03:00
}
// Fuse available NED position data into the main filter
if ( _fuse_height | | _fuse_pos ) {
fuseVelPosHeight ( ) ;
_fuse_pos = _fuse_height = false ;
2017-10-22 18:28:22 -03:00
_fuse_hpos_as_odom = false ;
2017-07-26 04:10:52 -03:00
2016-06-07 03:54:12 -03:00
}
2016-03-07 05:31:19 -04:00
2016-06-07 03:54:12 -03:00
// determine if we should use the yaw observation
if ( _control_status . flags . ev_yaw ) {
fuseHeading ( ) ;
2017-07-26 04:10:52 -03:00
2016-06-07 03:54:12 -03:00
}
2018-05-03 15:14:12 -03:00
} else if ( _control_status . flags . ev_pos
& & ( _time_last_imu - _time_last_ext_vision > ( uint64_t ) _params . no_gps_timeout_max ) ) {
2017-10-19 18:25:50 -03:00
// Turn off EV fusion mode if no data has been received
_control_status . flags . ev_pos = false ;
ECL_INFO ( " EKF External Vision Data Stopped " ) ;
2017-07-04 07:32:45 -03:00
}
2016-06-07 03:54:12 -03:00
}
2016-03-07 05:31:19 -04:00
2016-06-07 05:44:47 -03:00
void Ekf : : controlOpticalFlowFusion ( )
2016-06-07 03:54:12 -03:00
{
2018-04-09 05:35:15 -03:00
// Detect if the vehicle is on the ground and is being excessively tilted, shaken or rotated.
if ( ! _control_status . flags . in_air ) {
bool motion_is_excessive = ( ( _accel_mag_filt > 10.8f )
| | ( _accel_mag_filt < 8.8f )
| | ( _ang_rate_mag_filt > 0.5f )
2018-05-03 15:14:12 -03:00
| | ( _R_to_earth ( 2 , 2 ) < 0.866f ) ) ;
2018-04-09 05:35:15 -03:00
if ( motion_is_excessive ) {
_time_bad_motion_us = _imu_sample_delayed . time_us ;
2018-05-03 15:14:12 -03:00
2018-04-09 05:35:15 -03:00
} else {
_time_good_motion_us = _imu_sample_delayed . time_us ;
}
2018-05-03 15:14:12 -03:00
2018-04-09 05:35:15 -03:00
} else {
_time_bad_motion_us = 0 ;
_time_good_motion_us = _imu_sample_delayed . time_us ;
}
// Inhibit flow use if on ground and motion is excessive
// Apply a time based hysteresis to prevent rapid mode switching
if ( ! _inhibit_gndobs_use ) {
if ( ( _imu_sample_delayed . time_us - _time_good_motion_us ) > ( uint64_t ) 1E5 ) {
_inhibit_gndobs_use = true ;
}
2018-05-03 15:14:12 -03:00
2018-04-09 05:35:15 -03:00
} else {
if ( ( _imu_sample_delayed . time_us - _time_bad_motion_us ) > ( uint64_t ) 5E6 ) {
_inhibit_gndobs_use = false ;
}
}
// Handle cases where we are using optical flow but are no longer able to because data is old
// or its use has been inhibited.
if ( _control_status . flags . opt_flow ) {
2018-05-03 15:14:12 -03:00
if ( _inhibit_gndobs_use ) {
_control_status . flags . opt_flow = false ;
_time_last_of_fuse = 0 ;
2018-04-09 05:35:15 -03:00
2018-05-03 15:14:12 -03:00
} else if ( _time_last_imu - _flow_sample_delayed . time_us > ( uint64_t ) _params . no_gps_timeout_max ) {
_control_status . flags . opt_flow = false ;
2018-04-09 05:35:15 -03:00
2018-05-03 15:14:12 -03:00
}
2018-04-09 05:35:15 -03:00
}
2016-06-07 03:54:12 -03:00
if ( _flow_data_ready ) {
2018-04-09 05:35:15 -03:00
// New optical flow data has fallen behind the fusion time horizon and is ready to be fused
// Accumulate autopilot gyro data across the same time interval as the flow sensor
_imu_del_ang_of + = _imu_sample_delayed . delta_ang - _state . gyro_bias ;
_delta_time_of + = _imu_sample_delayed . delta_ang_dt ;
2016-03-07 05:31:19 -04:00
2016-06-07 03:54:12 -03:00
// optical flow fusion mode selection logic
if ( ( _params . fusion_mode & MASK_USE_OF ) // optical flow has been selected by the user
2018-05-03 15:14:12 -03:00
& & ! _control_status . flags . opt_flow // we are not yet using flow data
& & _control_status . flags . tilt_align // we know our tilt attitude
& & get_terrain_valid ( ) ) { // we have a valid distance to ground estimate
2016-06-07 03:54:12 -03: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-03-07 05:31:19 -04:00
2018-04-09 05:35:15 -03:00
// If the heading is valid and use is no tinhibited , start using optical flow aiding
if ( _control_status . flags . yaw_align & & ! _inhibit_gndobs_use ) {
2016-06-07 03:54:12 -03:00
// set the flag and reset the fusion timeout
_control_status . flags . opt_flow = true ;
_time_last_of_fuse = _time_last_imu ;
2017-10-11 17:58:54 -03:00
ECL_INFO ( " EKF Starting Optical Flow Use " ) ;
2016-06-07 03:54:12 -03:00
// if we are not using GPS then the velocity and position states and covariances need to be set
2018-04-09 05:35:15 -03:00
if ( ! _control_status . flags . gps | | ! _control_status . flags . ev_pos ) {
resetVelocity ( ) ;
resetPosition ( ) ;
2017-01-16 17:58:01 -04:00
// align the output observer to the EKF states
alignOutputFilter ( ) ;
2016-05-07 21:30:40 -03:00
}
2016-06-07 03:54:12 -03:00
}
2018-05-03 15:14:12 -03:00
2016-06-07 03:54:12 -03:00
} else if ( ! ( _params . fusion_mode & MASK_USE_OF ) ) {
_control_status . flags . opt_flow = false ;
2016-05-07 21:30:40 -03:00
2016-06-07 03:54:12 -03:00
}
2016-05-08 05:03:09 -03:00
2018-04-09 05:35:15 -03:00
// handle the case when we have optical flow, are reliant on it, but have not been using it for an extended period
if ( _control_status . flags . opt_flow
2018-05-03 15:14:12 -03:00
& & ! _control_status . flags . gps
& & ! _control_status . flags . ev_pos ) {
2018-04-09 05:35:15 -03:00
bool do_reset = _time_last_imu - _time_last_of_fuse > _params . no_gps_timeout_max ;
2018-05-03 15:14:12 -03:00
2018-04-09 05:35:15 -03:00
if ( do_reset ) {
resetVelocity ( ) ;
resetPosition ( ) ;
}
}
2017-09-14 02:47:15 -03:00
2017-10-10 12:51:56 -03:00
// fuse the data if the terrain/distance to bottom is valid
if ( _control_status . flags . opt_flow & & get_terrain_valid ( ) ) {
2016-06-07 03:54:12 -03:00
// Update optical flow bias estimates
calcOptFlowBias ( ) ;
2016-01-31 04:01:44 -04:00
2016-06-07 03:54:12 -03:00
// Fuse optical flow LOS rate observations into the main filter
fuseOptFlow ( ) ;
2016-04-24 09:20:18 -03:00
_last_known_posNE ( 0 ) = _state . pos ( 0 ) ;
_last_known_posNE ( 1 ) = _state . pos ( 1 ) ;
2016-06-07 03:54:12 -03:00
2016-04-24 09:20:18 -03:00
}
2017-10-11 17:58:54 -03:00
}
2016-04-24 09:20:18 -03:00
}
2016-06-07 05:44:47 -03:00
void Ekf : : controlGpsFusion ( )
2016-04-24 09:20:18 -03:00
{
2016-06-07 03:54:12 -03:00
// Check for new GPS data that has fallen behind the fusion time horizon
if ( _gps_data_ready ) {
2016-02-11 23:34:12 -04:00
2016-06-07 03:54:12 -03:00
// Determine if we should use GPS aiding for velocity and horizontal position
// To start using GPS we need angular alignment completed, the local NED origin set and GPS data that has not failed checks recently
if ( ( _params . fusion_mode & MASK_USE_GPS ) & & ! _control_status . flags . gps ) {
2018-05-03 15:14:12 -03:00
if ( _control_status . flags . tilt_align & & _NED_origin_initialised
& & ( _time_last_imu - _last_gps_fail_us > ( uint64_t ) 5e6 ) ) {
2016-06-07 03:54:12 -03: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 ) ;
}
// If the heading is valid start using gps aiding
if ( _control_status . flags . yaw_align ) {
// if we are not already aiding with optical flow, then we need to reset the position and velocity
2016-11-14 04:25:36 -04:00
// otherwise we only need to reset the position
_control_status . flags . gps = true ;
2018-05-03 15:14:12 -03:00
2016-06-07 03:54:12 -03:00
if ( ! _control_status . flags . opt_flow ) {
2016-11-14 04:25:36 -04:00
if ( ! resetPosition ( ) | | ! resetVelocity ( ) ) {
2016-06-07 03:54:12 -03:00
_control_status . flags . gps = false ;
}
2018-05-03 15:14:12 -03:00
2016-11-14 04:25:36 -04:00
} else if ( ! resetPosition ( ) ) {
_control_status . flags . gps = false ;
2016-06-07 03:54:12 -03:00
}
2018-05-03 15:14:12 -03:00
2016-06-07 03:54:12 -03:00
if ( _control_status . flags . gps ) {
2016-12-28 00:58:03 -04:00
ECL_INFO ( " EKF commencing GPS fusion " ) ;
2017-08-23 22:18:48 -03:00
_time_last_gps = _time_last_imu ;
2016-06-07 03:54:12 -03:00
}
2016-03-10 00:14:50 -04:00
}
2016-02-11 23:34:12 -04:00
}
2016-06-07 03:54:12 -03:00
} else if ( ! ( _params . fusion_mode & MASK_USE_GPS ) ) {
_control_status . flags . gps = false ;
2016-01-31 04:01:44 -04:00
}
2016-10-24 18:32:01 -03:00
// handle the case when we now have GPS, but have not been using it for an extended period
2017-10-22 18:28:22 -03:00
if ( _control_status . flags . gps ) {
// We are relying on aiding to constrain drift so after a specified time
// with no aiding we need to do something
bool do_reset = ( _time_last_imu - _time_last_pos_fuse > _params . no_gps_timeout_max )
& & ( _time_last_imu - _time_last_delpos_fuse > _params . no_gps_timeout_max )
& & ( _time_last_imu - _time_last_vel_fuse > _params . no_gps_timeout_max )
& & ( _time_last_imu - _time_last_of_fuse > _params . no_gps_timeout_max ) ;
2016-10-24 18:32:01 -03:00
2017-10-22 18:28:22 -03:00
// We haven't had an absolute position fix for a longer time so need to do something
do_reset = do_reset | | ( _time_last_imu - _time_last_pos_fuse > 2 * _params . no_gps_timeout_max ) ;
2016-10-24 18:32:01 -03:00
if ( do_reset ) {
2017-11-09 03:48:48 -04:00
// use GPS velocity data to check and correct yaw angle if a FW vehicle
2017-11-09 02:39:56 -04:00
if ( _control_status . flags . fixed_wing & & _control_status . flags . in_air ) {
2017-11-08 19:16:48 -04:00
// if flying a fixed wing aircraft, do a complete reset that includes yaw
2017-07-29 20:39:02 -03:00
realignYawGPS ( ) ;
}
2018-05-03 15:14:12 -03:00
2017-11-08 19:16:48 -04:00
resetVelocity ( ) ;
resetPosition ( ) ;
_velpos_reset_request = false ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF GPS fusion timeout - reset to GPS " ) ;
2016-10-24 18:32:01 -03:00
// Reset the timeout counters
_time_last_pos_fuse = _time_last_imu ;
_time_last_vel_fuse = _time_last_imu ;
2016-10-24 04:40:22 -03:00
2016-10-24 18:32:01 -03:00
}
2016-01-31 04:01:44 -04:00
}
2016-06-07 03:54:12 -03:00
// Only use GPS data for position and velocity aiding if enabled
if ( _control_status . flags . gps ) {
_fuse_pos = true ;
_fuse_vert_vel = true ;
_fuse_hor_vel = true ;
// correct velocity for offset relative to IMU
2018-05-03 15:14:12 -03:00
Vector3f ang_rate = _imu_sample_delayed . delta_ang * ( 1.0f / _imu_sample_delayed . delta_ang_dt ) ;
2016-06-07 03:54:12 -03:00
Vector3f pos_offset_body = _params . gps_pos_body - _params . imu_pos_body ;
2017-08-23 22:18:48 -03:00
Vector3f vel_offset_body = cross_product ( ang_rate , pos_offset_body ) ;
2016-06-07 03:54:12 -03:00
Vector3f vel_offset_earth = _R_to_earth * vel_offset_body ;
_gps_sample_delayed . vel - = vel_offset_earth ;
// correct position and height for offset relative to IMU
Vector3f pos_offset_earth = _R_to_earth * pos_offset_body ;
_gps_sample_delayed . pos ( 0 ) - = pos_offset_earth ( 0 ) ;
_gps_sample_delayed . pos ( 1 ) - = pos_offset_earth ( 1 ) ;
_gps_sample_delayed . hgt + = pos_offset_earth ( 2 ) ;
2017-10-22 18:28:22 -03:00
// calculate observation process noise
float lower_limit = fmaxf ( _params . gps_pos_noise , 0.01f ) ;
2018-05-03 15:14:12 -03:00
2018-03-26 21:31:37 -03:00
if ( _control_status . flags . opt_flow | | _control_status . flags . ev_pos ) {
// if we are using other sources of aiding, then relax the upper observation
// noise limit which prevents bad GPS perturbing the position estimate
_posObsNoiseNE = fmaxf ( _gps_sample_delayed . hacc , lower_limit ) ;
2018-05-03 15:14:12 -03:00
2018-03-26 21:31:37 -03:00
} else {
// if we are not using another source of aiding, then we are reliant on the GPS
// observations to constrain attitude errors and must limit the observation noise value.
float upper_limit = fmaxf ( _params . pos_noaid_noise , lower_limit ) ;
_posObsNoiseNE = math : : constrain ( _gps_sample_delayed . hacc , lower_limit , upper_limit ) ;
}
2018-05-03 15:14:12 -03:00
2018-02-27 20:48:53 -04:00
_velObsVarNE ( 1 ) = _velObsVarNE ( 0 ) = sq ( fmaxf ( _gps_sample_delayed . sacc , _params . gps_vel_noise ) ) ;
2017-10-22 18:28:22 -03:00
// calculate innovations
2017-11-17 16:44:28 -04:00
_vel_pos_innov [ 0 ] = _state . vel ( 0 ) - _gps_sample_delayed . vel ( 0 ) ;
_vel_pos_innov [ 1 ] = _state . vel ( 1 ) - _gps_sample_delayed . vel ( 1 ) ;
2017-11-23 23:25:44 -04:00
_vel_pos_innov [ 2 ] = _state . vel ( 2 ) - _gps_sample_delayed . vel ( 2 ) ;
2017-10-22 18:28:22 -03:00
_vel_pos_innov [ 3 ] = _state . pos ( 0 ) - _gps_sample_delayed . pos ( 0 ) ;
_vel_pos_innov [ 4 ] = _state . pos ( 1 ) - _gps_sample_delayed . pos ( 1 ) ;
// set innovation gate size
_posInnovGateNE = fmaxf ( _params . posNE_innov_gate , 1.0f ) ;
2017-11-17 16:44:28 -04:00
_hvelInnovGate = fmaxf ( _params . vel_innov_gate , 1.0f ) ;
2016-06-07 03:54:12 -03:00
}
2017-11-08 06:40:10 -04:00
} else if ( _control_status . flags . gps & & ( _time_last_imu - _time_last_gps > ( uint64_t ) 10e6 ) ) {
2018-05-03 15:14:12 -03:00
_control_status . flags . gps = false ;
ECL_WARN ( " EKF GPS data stopped " ) ;
2016-01-31 04:01:44 -04:00
}
2016-04-24 09:20:18 -03:00
}
2016-01-31 04:01:44 -04:00
2016-04-24 09:20:18 -03:00
void Ekf : : controlHeightSensorTimeouts ( )
{
2016-03-15 03:07:33 -03:00
/*
* Handle the case where we have not fused height measurements recently and
* uncertainty exceeds the max allowable . Reset using the best available height
* measurement source , continue using it after the reset and declare the current
* source failed if we have switched .
*/
2016-04-21 20:24:04 -03:00
2017-04-24 22:11:59 -03:00
// Check for IMU accelerometer vibration induced clipping as evidenced by the vertical innovations being positive and not stale.
// Clipping causes the average accel reading to move towards zero which makes the INS think it is falling and produces positive vertical innovations
float var_product_lim = sq ( _params . vert_innov_test_lim ) * sq ( _params . vert_innov_test_lim ) ;
2016-04-21 20:24:04 -03:00
bool bad_vert_accel = ( _control_status . flags . baro_hgt & & // we can only run this check if vertical position and velocity observations are indepedant
2017-04-24 22:11:59 -03:00
( sq ( _vel_pos_innov [ 5 ] * _vel_pos_innov [ 2 ] ) > var_product_lim * ( _vel_pos_innov_var [ 5 ] * _vel_pos_innov_var [ 2 ] ) ) & & // vertical position and velocity sensors are in agreement that we have a significant error
( _vel_pos_innov [ 2 ] > 0.0f ) & & // positive innovation indicates that the inertial nav thinks it is falling
2016-04-21 20:24:04 -03:00
( ( _imu_sample_delayed . time_us - _baro_sample_delayed . time_us ) < 2 * BARO_MAX_INTERVAL ) & & // vertical position data is fresh
2017-04-24 22:11:59 -03:00
( ( _imu_sample_delayed . time_us - _gps_sample_delayed . time_us ) < 2 * GPS_MAX_INTERVAL ) ) ; // vertical velocity data is fresh
2016-04-21 20:24:04 -03:00
// record time of last bad vert accel
if ( bad_vert_accel ) {
_time_bad_vert_accel = _time_last_imu ;
2018-05-03 15:14:12 -03:00
2017-03-16 20:45:24 -03:00
} else {
_time_good_vert_accel = _time_last_imu ;
}
// declare a bad vertical acceleration measurement and make the declaration persist
// for a minimum of 10 seconds
if ( _bad_vert_accel_detected ) {
_bad_vert_accel_detected = ( _time_last_imu - _time_bad_vert_accel < BADACC_PROBATION ) ;
2018-05-03 15:14:12 -03:00
2017-03-16 20:45:24 -03:00
} else {
_bad_vert_accel_detected = bad_vert_accel ;
2016-04-21 20:24:04 -03:00
}
2017-03-16 20:45:24 -03:00
// check if height is continuously failing becasue of accel errors
2017-04-24 22:11:59 -03:00
bool continuous_bad_accel_hgt = ( ( _time_last_imu - _time_good_vert_accel ) > ( unsigned ) _params . bad_acc_reset_delay_us ) ;
2017-03-16 20:45:24 -03:00
// check if height has been inertial deadreckoning for too long
2017-11-08 06:40:10 -04:00
bool hgt_fusion_timeout = ( ( _time_last_imu - _time_last_hgt_fuse ) > ( uint64_t ) 5e6 ) ;
2017-03-16 20:45:24 -03:00
// reset the vertical position and velocity states
2017-11-15 07:01:24 -04:00
if ( hgt_fusion_timeout | | continuous_bad_accel_hgt ) {
2016-04-21 21:50:53 -03:00
// boolean that indicates we will do a height reset
bool reset_height = false ;
2016-03-15 03:07:33 -03:00
// handle the case where we are using baro for height
if ( _control_status . flags . baro_hgt ) {
// check if GPS height is available
2018-05-03 15:14:12 -03:00
const gpsSample & gps_init = _gps_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool gps_hgt_available = ( ( _time_last_imu - gps_init . time_us ) < 2 * GPS_MAX_INTERVAL ) ;
bool gps_hgt_accurate = ( gps_init . vacc < _params . req_vacc ) ;
2017-11-14 21:14:33 -04:00
2018-05-03 15:14:12 -03:00
const baroSample & baro_init = _baro_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool baro_hgt_available = ( ( _time_last_imu - baro_init . time_us ) < 2 * BARO_MAX_INTERVAL ) ;
2016-04-21 20:24:04 -03:00
// check for inertial sensing errors in the last 10 seconds
2017-03-16 20:45:24 -03:00
bool prev_bad_vert_accel = ( _time_last_imu - _time_bad_vert_accel < BADACC_PROBATION ) ;
2016-04-21 19:39:24 -03:00
2016-04-21 21:50:53 -03:00
// reset to GPS if adequate GPS data is available and the timeout cannot be blamed on IMU data
bool reset_to_gps = gps_hgt_available & & gps_hgt_accurate & & ! _gps_hgt_faulty & & ! prev_bad_vert_accel ;
// reset to GPS if GPS data is available and there is no Baro data
reset_to_gps = reset_to_gps | | ( gps_hgt_available & & ! baro_hgt_available ) ;
// reset to Baro if we are not doing a GPS reset and baro data is available
bool reset_to_baro = ! reset_to_gps & & baro_hgt_available ;
if ( reset_to_gps ) {
// set height sensor health
2016-03-15 03:07:33 -03:00
_baro_hgt_faulty = true ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// declare the GPS height healthy
_gps_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlGPSHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF baro hgt timeout - reset to GPS " ) ;
2016-06-07 03:54:12 -03:00
2017-08-23 22:18:48 -03:00
} else if ( reset_to_baro ) {
2016-04-21 21:50:53 -03:00
// set height sensor health
_baro_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlBaroHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF baro hgt timeout - reset to baro " ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
} else {
// we have nothing we can reset to
// deny a reset
reset_height = false ;
2016-06-07 03:54:12 -03:00
2016-03-15 03:07:33 -03:00
}
}
// handle the case we are using GPS for height
if ( _control_status . flags . gps_hgt ) {
// check if GPS height is available
2018-05-03 15:14:12 -03:00
const gpsSample & gps_init = _gps_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool gps_hgt_available = ( ( _time_last_imu - gps_init . time_us ) < 2 * GPS_MAX_INTERVAL ) ;
bool gps_hgt_accurate = ( gps_init . vacc < _params . req_vacc ) ;
2016-06-07 03:54:12 -03:00
2016-03-15 03:07:33 -03:00
// check the baro height source for consistency and freshness
2018-05-03 15:14:12 -03:00
const baroSample & baro_init = _baro_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool baro_data_fresh = ( ( _time_last_imu - baro_init . time_us ) < 2 * BARO_MAX_INTERVAL ) ;
float baro_innov = _state . pos ( 2 ) - ( _hgt_sensor_offset - baro_init . hgt + _baro_hgt_offset ) ;
2018-01-28 17:09:55 -04:00
bool baro_data_consistent = fabsf ( baro_innov ) < ( sq ( _params . baro_noise ) + P [ 9 ] [ 9 ] ) * sq ( _params . baro_innov_gate ) ;
2016-03-15 03:07:33 -03:00
2016-04-21 21:50:53 -03:00
// if baro data is acceptable and GPS data is inaccurate, reset height to baro
bool reset_to_baro = baro_data_consistent & & baro_data_fresh & & ! _baro_hgt_faulty & & ! gps_hgt_accurate ;
// if GPS height is unavailable and baro data is available, reset height to baro
reset_to_baro = reset_to_baro | | ( ! gps_hgt_available & & baro_data_fresh ) ;
2016-05-22 12:40:43 -03:00
// if we cannot switch to baro and GPS data is available, reset height to GPS
2016-04-21 21:50:53 -03:00
bool reset_to_gps = ! reset_to_baro & & gps_hgt_available ;
if ( reset_to_baro ) {
// set height sensor health
2016-03-15 03:07:33 -03:00
_gps_hgt_faulty = true ;
2016-04-21 21:50:53 -03:00
_baro_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlBaroHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF gps hgt timeout - reset to baro " ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
} else if ( reset_to_gps ) {
// set height sensor health
_gps_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlGPSHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF gps hgt timeout - reset to GPS " ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
} else {
// we have nothing to reset to
reset_height = false ;
2016-06-07 03:54:12 -03:00
2016-03-15 03:07:33 -03:00
}
}
// handle the case we are using range finder for height
if ( _control_status . flags . rng_hgt ) {
// check if range finder data is available
2018-05-03 15:14:12 -03:00
const rangeSample & rng_init = _range_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool rng_data_available = ( ( _time_last_imu - rng_init . time_us ) < 2 * RNG_MAX_INTERVAL ) ;
2016-06-07 03:54:12 -03:00
2016-03-15 03:07:33 -03:00
// check if baro data is available
2018-05-03 15:14:12 -03:00
const baroSample & baro_init = _baro_buffer . get_newest ( ) ;
2016-03-15 03:07:33 -03:00
bool baro_data_available = ( ( _time_last_imu - baro_init . time_us ) < 2 * BARO_MAX_INTERVAL ) ;
2016-05-06 23:52:45 -03:00
// reset to baro if we have no range data and baro data is available
2016-04-21 21:50:53 -03:00
bool reset_to_baro = ! rng_data_available & & baro_data_available ;
2016-05-06 23:52:45 -03:00
// reset to range data if it is available
bool reset_to_rng = rng_data_available ;
2016-04-21 21:50:53 -03:00
if ( reset_to_baro ) {
// set height sensor health
2016-03-15 03:07:33 -03:00
_rng_hgt_faulty = true ;
2016-04-21 21:50:53 -03:00
_baro_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlBaroHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF rng hgt timeout - reset to baro " ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
} else if ( reset_to_rng ) {
// set height sensor health
_rng_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlRangeHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF rng hgt timeout - reset to rng hgt " ) ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
} else {
// we have nothing to reset to
reset_height = false ;
2016-06-07 03:54:12 -03:00
2016-03-15 03:07:33 -03:00
}
}
2016-05-27 00:15:53 -03:00
// handle the case where we are using external vision data for height
if ( _control_status . flags . ev_hgt ) {
// check if vision data is available
2018-05-03 15:14:12 -03:00
const extVisionSample & ev_init = _ext_vision_buffer . get_newest ( ) ;
2016-05-27 00:15:53 -03:00
bool ev_data_available = ( ( _time_last_imu - ev_init . time_us ) < 2 * EV_MAX_INTERVAL ) ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
// check if baro data is available
2018-05-03 15:14:12 -03:00
const baroSample & baro_init = _baro_buffer . get_newest ( ) ;
2016-05-27 00:15:53 -03:00
bool baro_data_available = ( ( _time_last_imu - baro_init . time_us ) < 2 * BARO_MAX_INTERVAL ) ;
// reset to baro if we have no vision data and baro data is available
bool reset_to_baro = ! ev_data_available & & baro_data_available ;
// reset to ev data if it is available
bool reset_to_ev = ev_data_available ;
if ( reset_to_baro ) {
// set height sensor health
_baro_hgt_faulty = false ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlBaroHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF ev hgt timeout - reset to baro " ) ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
} else if ( reset_to_ev ) {
// reset the height mode
2017-02-28 05:56:39 -04:00
setControlEVHeight ( ) ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
// request a reset
reset_height = true ;
2016-12-28 00:58:03 -04:00
ECL_WARN ( " EKF ev hgt timeout - reset to ev hgt " ) ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
} else {
// we have nothing to reset to
reset_height = false ;
2016-06-07 03:54:12 -03:00
2016-05-27 00:15:53 -03:00
}
}
2016-02-24 21:53:58 -04:00
// Reset vertical position and velocity states to the last measurement
2016-04-21 21:50:53 -03:00
if ( reset_height ) {
resetHeight ( ) ;
// Reset the timout timer
_time_last_hgt_fuse = _time_last_imu ;
2016-06-07 03:54:12 -03:00
2016-04-21 21:50:53 -03:00
}
2016-04-21 19:33:11 -03:00
2016-02-24 21:53:58 -04:00
}
2016-04-24 09:20:18 -03:00
}
2016-02-24 21:53:58 -04:00
2017-02-28 05:56:39 -04:00
void Ekf : : controlHeightFusion ( )
2016-04-24 09:20:18 -03:00
{
2017-02-28 05:56:39 -04:00
// set control flags for the desired primary height source
if ( _range_data_ready ) {
// correct the range data for position offset relative to the IMU
Vector3f pos_offset_body = _params . rng_pos_body - _params . imu_pos_body ;
Vector3f pos_offset_earth = _R_to_earth * pos_offset_body ;
_range_sample_delayed . rng + = pos_offset_earth ( 2 ) / _R_rng_to_earth_2_2 ;
}
if ( _params . vdist_sensor_type = = VDIST_SENSOR_BARO ) {
2017-05-11 05:15:43 -03:00
_in_range_aid_mode = rangeAidConditionsMet ( _in_range_aid_mode ) ;
2017-02-28 05:56:39 -04:00
2017-05-11 05:15:43 -03:00
if ( _in_range_aid_mode & & _range_data_ready & & ! _rng_hgt_faulty ) {
2017-02-28 05:56:39 -04:00
setControlRangeHeight ( ) ;
2016-06-07 03:54:12 -03:00
_fuse_height = true ;
2016-04-24 09:20:18 -03:00
2017-02-28 05:56:39 -04:00
// we have just switched to using range finder, calculate height sensor offset such that current
// measurment matches our current height estimate
if ( _control_status_prev . flags . rng_hgt ! = _control_status . flags . rng_hgt ) {
2017-07-19 04:52:35 -03:00
if ( get_terrain_valid ( ) ) {
2017-04-27 12:24:04 -03:00
_hgt_sensor_offset = _terrain_vpos ;
2018-05-03 15:14:12 -03:00
2017-04-27 12:24:04 -03:00
} else {
_hgt_sensor_offset = _R_rng_to_earth_2_2 * _range_sample_delayed . rng + _state . pos ( 2 ) ;
}
2017-02-28 05:56:39 -04:00
}
2016-05-27 00:14:52 -03:00
2018-05-03 00:24:33 -03:00
} else if ( ! _in_range_aid_mode & & _baro_data_ready & & ! _baro_hgt_faulty ) {
2017-02-28 05:56:39 -04:00
setControlBaroHeight ( ) ;
_fuse_height = true ;
2017-06-26 15:16:53 -03:00
_in_range_aid_mode = false ;
2016-05-27 00:14:52 -03:00
2017-04-27 12:25:09 -03:00
// we have just switched to using baro height, we don't need to set a height sensor offset
// since we track a separate _baro_hgt_offset
2017-02-28 05:56:39 -04:00
if ( _control_status_prev . flags . baro_hgt ! = _control_status . flags . baro_hgt ) {
2017-04-27 12:25:09 -03:00
_hgt_sensor_offset = 0.0f ;
2017-02-28 05:56:39 -04:00
}
2017-11-02 22:11:32 -03:00
// Turn off ground effect compensation if it times out or sufficient height has been gained
// since takeoff.
if ( _control_status . flags . gnd_effect ) {
if ( ( _time_last_imu - _time_last_gnd_effect_on > GNDEFFECT_TIMEOUT ) | |
2018-05-03 15:14:12 -03:00
( ( ( _last_on_ground_posD - _state . pos ( 2 ) ) > _params . gnd_effect_max_hgt ) & &
_control_status . flags . in_air ) ) {
2017-11-02 22:11:32 -03:00
_control_status . flags . gnd_effect = false ;
}
}
2017-06-29 12:07:42 -03:00
} else if ( _control_status . flags . gps_hgt & & _gps_data_ready & & ! _gps_hgt_faulty ) {
// switch to gps if there was a reset to gps
_fuse_height = true ;
_in_range_aid_mode = false ;
// we have just switched to using gps height, calculate height sensor offset such that current
// measurment matches our current height estimate
if ( _control_status_prev . flags . gps_hgt ! = _control_status . flags . gps_hgt ) {
_hgt_sensor_offset = _gps_sample_delayed . hgt - _gps_alt_ref + _state . pos ( 2 ) ;
}
2016-06-07 03:54:12 -03:00
}
}
2016-04-24 09:20:18 -03:00
2017-02-28 05:56:39 -04:00
// set the height data source to range if requested
if ( ( _params . vdist_sensor_type = = VDIST_SENSOR_RANGE ) & & ! _rng_hgt_faulty ) {
setControlRangeHeight ( ) ;
_fuse_height = _range_data_ready ;
2017-06-23 04:53:22 -03:00
// we have just switched to using range finder, calculate height sensor offset such that current
// measurment matches our current height estimate
if ( _control_status_prev . flags . rng_hgt ! = _control_status . flags . rng_hgt ) {
2017-07-12 08:43:49 -03:00
// use the parameter rng_gnd_clearance if on ground to avoid a noisy offset initialization (e.g. sonar)
2017-07-19 04:52:35 -03:00
if ( _control_status . flags . in_air & & get_terrain_valid ( ) ) {
2017-07-12 08:43:49 -03:00
2017-06-23 04:53:22 -03:00
_hgt_sensor_offset = _terrain_vpos ;
2018-05-03 15:14:12 -03:00
2017-07-12 08:43:49 -03:00
} else if ( _control_status . flags . in_air ) {
2017-06-23 04:53:22 -03:00
_hgt_sensor_offset = _R_rng_to_earth_2_2 * _range_sample_delayed . rng + _state . pos ( 2 ) ;
2018-05-03 15:14:12 -03:00
2017-07-12 08:43:49 -03:00
} else {
_hgt_sensor_offset = _params . rng_gnd_clearance ;
2017-06-23 04:53:22 -03:00
}
}
2018-05-03 15:14:12 -03:00
2017-06-23 04:53:22 -03:00
} else if ( ( _params . vdist_sensor_type = = VDIST_SENSOR_RANGE ) & & _baro_data_ready & & ! _baro_hgt_faulty ) {
setControlBaroHeight ( ) ;
_fuse_height = true ;
// we have just switched to using baro height, we don't need to set a height sensor offset
// since we track a separate _baro_hgt_offset
if ( _control_status_prev . flags . baro_hgt ! = _control_status . flags . baro_hgt ) {
_hgt_sensor_offset = 0.0f ;
}
2017-02-28 05:56:39 -04:00
}
2016-05-27 00:14:52 -03:00
2017-02-28 05:56:39 -04:00
// Determine if GPS should be used as the height source
if ( _params . vdist_sensor_type = = VDIST_SENSOR_GPS ) {
2017-05-11 05:15:43 -03:00
_in_range_aid_mode = rangeAidConditionsMet ( _in_range_aid_mode ) ;
2016-06-07 03:54:12 -03:00
2017-05-11 05:15:43 -03:00
if ( _in_range_aid_mode & & _range_data_ready & & ! _rng_hgt_faulty ) {
2017-02-28 05:56:39 -04:00
setControlRangeHeight ( ) ;
_fuse_height = true ;
2016-06-07 03:54:12 -03:00
2017-02-28 05:56:39 -04:00
// we have just switched to using range finder, calculate height sensor offset such that current
// measurment matches our current height estimate
if ( _control_status_prev . flags . rng_hgt ! = _control_status . flags . rng_hgt ) {
2017-07-19 04:52:35 -03:00
if ( get_terrain_valid ( ) ) {
2017-04-27 12:24:04 -03:00
_hgt_sensor_offset = _terrain_vpos ;
2018-05-03 15:14:12 -03:00
2017-04-27 12:24:04 -03:00
} else {
_hgt_sensor_offset = _R_rng_to_earth_2_2 * _range_sample_delayed . rng + _state . pos ( 2 ) ;
}
2017-02-28 05:56:39 -04:00
}
2018-05-03 00:24:33 -03:00
} else if ( ! _in_range_aid_mode & & _gps_data_ready & & ! _gps_hgt_faulty ) {
2017-02-28 05:56:39 -04:00
setControlGPSHeight ( ) ;
2016-06-07 03:54:12 -03:00
_fuse_height = true ;
2017-06-26 15:16:53 -03:00
_in_range_aid_mode = false ;
2016-05-27 00:14:52 -03:00
2017-02-28 05:56:39 -04:00
// we have just switched to using gps height, calculate height sensor offset such that current
// measurment matches our current height estimate
if ( _control_status_prev . flags . gps_hgt ! = _control_status . flags . gps_hgt ) {
_hgt_sensor_offset = _gps_sample_delayed . hgt - _gps_alt_ref + _state . pos ( 2 ) ;
}
2018-05-03 15:14:12 -03:00
2017-06-29 12:07:42 -03:00
} else if ( _control_status . flags . baro_hgt & & _baro_data_ready & & ! _baro_hgt_faulty ) {
// switch to baro if there was a reset to baro
_fuse_height = true ;
_in_range_aid_mode = false ;
// we have just switched to using baro height, we don't need to set a height sensor offset
// since we track a separate _baro_hgt_offset
if ( _control_status_prev . flags . baro_hgt ! = _control_status . flags . baro_hgt ) {
_hgt_sensor_offset = 0.0f ;
}
2016-06-07 03:54:12 -03:00
}
2017-02-28 05:56:39 -04:00
}
2018-01-15 05:15:56 -04:00
// Determine if we rely on EV height but switched to baro
if ( _params . vdist_sensor_type = = VDIST_SENSOR_EV ) {
if ( _control_status . flags . baro_hgt & & _baro_data_ready & & ! _baro_hgt_faulty ) {
// switch to baro if there was a reset to baro
_fuse_height = true ;
_in_range_aid_mode = false ;
// we have just switched to using baro height, we don't need to set a height sensor offset
// since we track a separate _baro_hgt_offset
if ( _control_status_prev . flags . baro_hgt ! = _control_status . flags . baro_hgt ) {
_hgt_sensor_offset = 0.0f ;
}
}
}
2017-02-28 05:56:39 -04:00
// calculate a filtered offset between the baro origin and local NED origin if we are not using the baro as a height reference
if ( ! _control_status . flags . baro_hgt & & _baro_data_ready ) {
float local_time_step = 1e-6 f * _delta_time_baro_us ;
local_time_step = math : : constrain ( local_time_step , 0.0f , 1.0f ) ;
2016-06-07 03:54:12 -03:00
2017-02-28 05:56:39 -04:00
// apply a 10 second first order low pass filter to baro offset
2017-04-27 12:19:31 -03:00
float offset_rate_correction = 0.1f * ( _baro_sample_delayed . hgt + _state . pos (
2017-02-28 05:56:39 -04:00
2 ) - _baro_hgt_offset ) ;
_baro_hgt_offset + = local_time_step * math : : constrain ( offset_rate_correction , - 0.1f , 0.1f ) ;
}
2018-05-03 15:14:12 -03:00
if ( ( _time_last_imu - _time_last_hgt_fuse ) > 2 * RNG_MAX_INTERVAL & & _control_status . flags . rng_hgt
& & ! _range_data_ready ) {
2016-06-07 03:54:12 -03:00
// If we are supposed to be using range finder data as the primary height sensor, have missed or rejected measurements
// and are on the ground, then synthesise a measurement at the expected on ground value
if ( ! _control_status . flags . in_air ) {
_range_sample_delayed . rng = _params . rng_gnd_clearance ;
_range_sample_delayed . time_us = _imu_sample_delayed . time_us ;
}
_fuse_height = true ;
2017-02-28 05:56:39 -04:00
}
2016-05-27 00:14:52 -03:00
2017-02-28 05:56:39 -04:00
}
2017-04-06 05:07:55 -03:00
bool Ekf : : rangeAidConditionsMet ( bool in_range_aid_mode )
2017-02-28 05:56:39 -04:00
{
// if the parameter for range aid is enabled we allow to switch from using the primary height source to using range finder as height source
// under the following conditions
// 1) we are not further than max_range_for_dual_fusion away from the ground
// 2) our ground speed is not higher than max_vel_for_dual_fusion
// 3) Our terrain estimate is stable (needs better checks)
if ( _params . range_aid ) {
2017-05-11 05:15:43 -03:00
// check if we should use range finder measurements to estimate height, use hysteresis to avoid rapid switching
bool use_range_finder ;
2018-05-03 15:14:12 -03:00
2017-05-11 05:15:43 -03:00
if ( in_range_aid_mode ) {
2017-07-19 04:52:35 -03:00
use_range_finder = ( _terrain_vpos - _state . pos ( 2 ) < _params . max_hagl_for_range_aid ) & & get_terrain_valid ( ) ;
2017-05-11 05:15:43 -03:00
} else {
// if we were not using range aid in the previous iteration then require the current height above terrain to be
// smaller than 70 % of the maximum allowed ground distance for range aid
2017-07-19 04:52:35 -03:00
use_range_finder = ( _terrain_vpos - _state . pos ( 2 ) < 0.7f * _params . max_hagl_for_range_aid ) & & get_terrain_valid ( ) ;
2017-05-11 05:15:43 -03:00
}
2017-02-28 05:56:39 -04:00
bool horz_vel_valid = ( _control_status . flags . gps | | _control_status . flags . ev_pos | | _control_status . flags . opt_flow )
2018-05-03 15:14:12 -03:00
& & ( _fault_status . value = = 0 ) ;
2017-02-28 05:56:39 -04:00
if ( horz_vel_valid ) {
float ground_vel = sqrtf ( _state . vel ( 0 ) * _state . vel ( 0 ) + _state . vel ( 1 ) * _state . vel ( 1 ) ) ;
2017-05-11 05:15:43 -03:00
if ( in_range_aid_mode ) {
use_range_finder & = ground_vel < _params . max_vel_for_range_aid ;
2017-04-06 05:07:55 -03:00
2017-05-11 05:15:43 -03:00
} else {
// if we were not using range aid in the previous iteration then require the ground velocity to be
// smaller than 70 % of the maximum allowed ground velocity for range aid
use_range_finder & = ground_vel < 0.7f * _params . max_vel_for_range_aid ;
}
2017-02-28 05:56:39 -04:00
} else {
use_range_finder = false ;
}
2017-07-07 12:17:36 -03:00
// use hysteresis to check for hagl
if ( in_range_aid_mode ) {
use_range_finder & = ( ( _hagl_innov * _hagl_innov / ( sq ( _params . range_aid_innov_gate ) * _hagl_innov_var ) ) < 1.0f ) ;
} else {
// if we were not using range aid in the previous iteration then use a much lower (1/100) threshold to avoid
// switching to range finder too soon (wait for terrain to update).
use_range_finder & = ( ( _hagl_innov * _hagl_innov / ( sq ( _params . range_aid_innov_gate ) * _hagl_innov_var ) ) < 0.01f ) ;
}
2017-02-28 05:56:39 -04:00
return use_range_finder ;
2017-05-11 05:15:43 -03:00
2017-02-28 05:56:39 -04:00
} else {
return false ;
2016-01-31 04:01:44 -04:00
}
2016-06-07 03:54:12 -03:00
}
2017-07-31 12:59:16 -03:00
void Ekf : : checkForStuckRange ( )
{
2017-11-08 06:40:10 -04:00
if ( _range_data_ready & & _range_sample_delayed . time_us - _time_last_rng_ready > ( uint64_t ) 10e6 & &
2018-05-03 15:14:12 -03:00
_control_status . flags . in_air ) {
2018-03-20 21:46:27 -03:00
_control_status . flags . rng_stuck = true ;
2017-07-31 12:59:16 -03:00
//require a variance of rangefinder values to check for "stuck" measurements
if ( _rng_check_max_val - _rng_check_min_val > 1.0f ) {
_time_last_rng_ready = _range_sample_delayed . time_us ;
_rng_check_min_val = 0.0f ;
_rng_check_max_val = 0.0f ;
2018-03-20 21:46:27 -03:00
_control_status . flags . rng_stuck = false ;
2017-07-31 12:59:16 -03:00
} else {
2017-08-23 22:18:48 -03:00
if ( _range_sample_delayed . rng > _rng_check_max_val ) {
2017-07-31 12:59:16 -03:00
_rng_check_max_val = _range_sample_delayed . rng ;
2017-08-23 22:18:48 -03:00
}
2017-07-31 12:59:16 -03:00
2017-08-23 22:18:48 -03:00
if ( _rng_check_min_val < 0.1f | | _range_sample_delayed . rng < _rng_check_min_val ) {
2017-07-31 12:59:16 -03:00
_rng_check_min_val = _range_sample_delayed . rng ;
2017-08-23 22:18:48 -03:00
}
2017-07-31 12:59:16 -03:00
_range_data_ready = false ;
}
} else if ( _range_data_ready ) {
_time_last_rng_ready = _range_sample_delayed . time_us ;
}
}
2016-06-07 05:44:47 -03:00
void Ekf : : controlAirDataFusion ( )
2016-06-07 03:54:12 -03:00
{
2017-08-23 22:18:48 -03:00
// control activation and initialisation/reset of wind states required for airspeed fusion
2016-08-23 15:15:03 -03:00
2017-06-29 21:32:23 -03:00
// If both airspeed and sideslip fusion have timed out and we are not using a drag observation model then we no longer have valid wind estimates
2017-11-08 06:40:10 -04:00
bool airspeed_timed_out = _time_last_imu - _time_last_arsp_fuse > ( uint64_t ) 10e6 ;
bool sideslip_timed_out = _time_last_imu - _time_last_beta_fuse > ( uint64_t ) 10e6 ;
2017-08-23 22:18:48 -03:00
2017-04-09 20:21:29 -03:00
if ( _control_status . flags . wind & & airspeed_timed_out & & sideslip_timed_out & & ! ( _params . fusion_mode & MASK_USE_DRAG ) ) {
2016-06-07 03:54:12 -03:00
_control_status . flags . wind = false ;
2016-08-09 10:46:16 -03:00
}
2017-10-19 04:21:47 -03:00
if ( _control_status . flags . fuse_aspd & & airspeed_timed_out ) {
_control_status . flags . fuse_aspd = false ;
}
// Always try to fuse airspeed data if available and we are in flight
if ( _tas_data_ready & & _control_status . flags . in_air ) {
// always fuse airsped data if we are flying and data is present
if ( ! _control_status . flags . fuse_aspd ) {
_control_status . flags . fuse_aspd = true ;
}
2016-10-27 01:17:02 -03:00
// If starting wind state estimation, reset the wind states and covariances before fusing any data
2016-07-31 02:00:25 -03:00
if ( ! _control_status . flags . wind ) {
2016-10-27 01:17:02 -03:00
// activate the wind states
2016-07-31 02:00:25 -03:00
_control_status . flags . wind = true ;
2016-10-27 01:17:02 -03:00
// reset the timout timer to prevent repeated resets
_time_last_arsp_fuse = _time_last_imu ;
_time_last_beta_fuse = _time_last_imu ;
// reset the wind speed states and corresponding covariances
2016-07-31 02:00:25 -03:00
resetWindStates ( ) ;
resetWindCovariance ( ) ;
2016-10-27 01:17:02 -03:00
2016-07-31 02:00:25 -03:00
}
fuseAirspeed ( ) ;
2016-06-06 08:59:46 -03:00
}
2016-04-24 09:20:18 -03:00
}
2016-01-31 04:01:44 -04:00
2016-07-12 14:54:14 -03:00
void Ekf : : controlBetaFusion ( )
{
2017-05-11 05:15:43 -03:00
// control activation and initialisation/reset of wind states required for synthetic sideslip fusion fusion
2016-10-27 01:17:02 -03:00
2017-06-29 21:32:23 -03:00
// If both airspeed and sideslip fusion have timed out and we are not using a drag observation model then we no longer have valid wind estimates
2017-11-08 06:40:10 -04:00
bool sideslip_timed_out = _time_last_imu - _time_last_beta_fuse > ( uint64_t ) 10e6 ;
bool airspeed_timed_out = _time_last_imu - _time_last_arsp_fuse > ( uint64_t ) 10e6 ;
2018-05-03 15:14:12 -03:00
if ( _control_status . flags . wind & & airspeed_timed_out & & sideslip_timed_out & & ! ( _params . fusion_mode & MASK_USE_DRAG ) ) {
2017-05-11 05:15:43 -03:00
_control_status . flags . wind = false ;
}
2016-10-27 01:17:02 -03:00
2016-11-14 02:23:42 -04:00
// Perform synthetic sideslip fusion when in-air and sideslip fuson had been enabled externally in addition to the following criteria:
// Suffient time has lapsed sice the last fusion
2016-07-25 17:07:01 -03:00
bool beta_fusion_time_triggered = _time_last_imu - _time_last_beta_fuse > _params . beta_avg_ft_us ;
2016-11-14 02:23:42 -04:00
2018-05-03 15:14:12 -03:00
if ( beta_fusion_time_triggered & & _control_status . flags . fuse_beta & & _control_status . flags . in_air ) {
2016-10-27 01:17:02 -03:00
// If starting wind state estimation, reset the wind states and covariances before fusing any data
if ( ! _control_status . flags . wind ) {
// activate the wind states
_control_status . flags . wind = true ;
2016-11-02 03:48:52 -03:00
// reset the timeout timers to prevent repeated resets
2016-10-27 01:17:02 -03:00
_time_last_beta_fuse = _time_last_imu ;
_time_last_arsp_fuse = _time_last_imu ;
// reset the wind speed states and corresponding covariances
resetWindStates ( ) ;
resetWindCovariance ( ) ;
}
2016-08-23 15:15:03 -03:00
2017-05-11 05:15:43 -03:00
fuseSideslip ( ) ;
2017-08-23 22:18:48 -03:00
}
2016-07-12 14:54:14 -03:00
}
2017-04-09 20:21:29 -03:00
void Ekf : : controlDragFusion ( )
{
2017-08-23 22:18:48 -03:00
if ( _params . fusion_mode & MASK_USE_DRAG ) {
2017-06-29 21:32:54 -03:00
if ( _control_status . flags . in_air ) {
if ( ! _control_status . flags . wind ) {
// reset the wind states and covariances when starting drag accel fusion
_control_status . flags . wind = true ;
resetWindStates ( ) ;
resetWindCovariance ( ) ;
2017-04-09 20:21:29 -03:00
2017-06-29 21:32:54 -03:00
} else if ( _drag_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _drag_sample_delayed ) ) {
fuseDrag ( ) ;
}
2018-05-03 15:14:12 -03:00
2017-06-29 21:32:54 -03:00
} else {
_control_status . flags . wind = false ;
2017-04-11 07:59:34 -03:00
2017-04-09 20:21:29 -03:00
}
}
}
2016-06-07 05:44:47 -03:00
void Ekf : : controlMagFusion ( )
2016-04-24 09:20:18 -03:00
{
2016-06-07 03:54:12 -03:00
// If we are on ground, store the local position and time to use as a reference
2017-05-16 05:50:19 -03:00
// Also reset the flight alignment flag so that the mag fields will be re-initialised next time we achieve flight altitude
2016-06-07 03:54:12 -03:00
if ( ! _control_status . flags . in_air ) {
_last_on_ground_posD = _state . pos ( 2 ) ;
2017-05-11 20:14:39 -03:00
_flt_mag_align_complete = false ;
2017-07-29 20:39:02 -03:00
_num_bad_flight_yaw_events = 0 ;
2016-06-07 03:54:12 -03:00
}
2016-11-02 03:48:52 -03:00
// check for new magnetometer data that has fallen behind the fusion time horizon
2017-11-02 22:11:32 -03:00
// If we are using external vision data for heading then no magnetometer fusion is used
if ( ! _control_status . flags . ev_yaw & & _mag_data_ready ) {
2016-06-07 03:54:12 -03: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
2017-07-29 20:39:02 -03:00
if ( _control_status . flags . mag_fault ) {
// do no magnetometer fusion at all
_control_status . flags . mag_hdg = false ;
_control_status . flags . mag_3D = false ;
2018-05-03 15:14:12 -03:00
2017-04-02 21:42:26 -03:00
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_AUTO | | _params . mag_fusion_type = = MAG_FUSE_TYPE_AUTOFW ) {
2017-05-11 20:14:39 -03:00
// Check if height has increased sufficiently to be away from ground magnetic anomalies
2016-06-07 03:54:12 -03:00
bool height_achieved = ( _last_on_ground_posD - _state . pos ( 2 ) ) > 1.5f ;
2017-05-11 20:14:39 -03:00
// Check if there has been enough change in horizontal velocity to make yaw observable
// Apply hysteresis to check to avoid rapid toggling
if ( _yaw_angle_observable ) {
_yaw_angle_observable = _accel_lpf_NE . norm ( ) > _params . mag_acc_gate ;
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
} else {
_yaw_angle_observable = _accel_lpf_NE . norm ( ) > 2.0f * _params . mag_acc_gate ;
}
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
_yaw_angle_observable = _yaw_angle_observable & & ( _control_status . flags . gps | | _control_status . flags . ev_pos ) ;
// check if there is enough yaw rotation to make the mag bias states observable
if ( ! _mag_bias_observable & & ( fabsf ( _yaw_rate_lpf_ef ) > _params . mag_yaw_rate_gate ) ) {
// initial yaw motion is detected
_mag_bias_observable = true ;
_yaw_delta_ef = 0.0f ;
_time_yaw_started = _imu_sample_delayed . time_us ;
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
} else if ( _mag_bias_observable ) {
// monitor yaw rotation in 45 deg sections.
// a rotation of 45 deg is sufficient to make the mag bias observable
if ( fabsf ( _yaw_delta_ef ) > 0.7854f ) {
_time_yaw_started = _imu_sample_delayed . time_us ;
_yaw_delta_ef = 0.0f ;
}
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
// require sustained yaw motion of 50% the initial yaw rate threshold
float min_yaw_change_req = 0.5f * _params . mag_yaw_rate_gate * ( 1e-6 f * ( float ) ( _imu_sample_delayed . time_us - _time_yaw_started ) ) ;
_mag_bias_observable = fabsf ( _yaw_delta_ef ) > min_yaw_change_req ;
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
} else {
_mag_bias_observable = false ;
}
// record the last time that movement was suitable for use of 3-axis magnetometer fusion
if ( _mag_bias_observable | | _yaw_angle_observable ) {
_time_last_movement = _imu_sample_delayed . time_us ;
}
// decide whether 3-axis magnetomer fusion can be used
bool use_3D_fusion = _control_status . flags . tilt_align & & // Use of 3D fusion requires valid tilt estimates
_control_status . flags . in_air & & // don't use when on the ground becasue of magnetic anomalies
( _flt_mag_align_complete | | height_achieved ) & & // once in-flight field alignment has been performed, ignore relative height
2017-05-16 05:50:19 -03:00
( ( _imu_sample_delayed . time_us - _time_last_movement ) < 2 * 1000 * 1000 ) ; // Using 3-axis fusion for a minimum period after to allow for false negatives
2017-05-11 20:14:39 -03:00
// perform switch-over
if ( use_3D_fusion ) {
2016-06-07 03:54:12 -03:00
if ( ! _control_status . flags . mag_3D ) {
2017-05-11 20:14:39 -03:00
if ( ! _flt_mag_align_complete ) {
2016-11-02 03:48:52 -03:00
// If we are flying a vehicle that flies forward, eg plane, then we can use the GPS course to check and correct the heading
2017-07-07 07:42:31 -03:00
if ( _control_status . flags . fixed_wing & & _control_status . flags . in_air ) {
2017-11-08 19:16:48 -04:00
_flt_mag_align_complete = realignYawGPS ( ) ;
2018-05-03 15:14:12 -03:00
2017-11-08 19:16:48 -04:00
if ( _velpos_reset_request ) {
resetVelocity ( ) ;
resetPosition ( ) ;
_velpos_reset_request = false ;
}
2018-05-03 15:14:12 -03:00
2016-11-02 03:48:52 -03:00
} else {
2017-11-08 19:16:48 -04:00
_flt_mag_align_complete = resetMagHeading ( _mag_sample_delayed . mag ) ;
2016-11-02 03:48:52 -03:00
}
2018-05-03 15:14:12 -03:00
2017-11-08 19:16:48 -04:00
_control_status . flags . yaw_align = _control_status . flags . yaw_align | | _flt_mag_align_complete ;
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
} else {
// reset the mag field covariances
zeroRows ( P , 16 , 21 ) ;
zeroCols ( P , 16 , 21 ) ;
// re-instate the last used variances
for ( uint8_t index = 0 ; index < = 5 ; index + + ) {
2018-05-03 15:14:12 -03:00
P [ index + 16 ] [ index + 16 ] = _saved_mag_variance [ index ] ;
2017-05-11 20:14:39 -03:00
}
}
2016-06-07 03:54:12 -03:00
}
2017-07-07 07:42:31 -03:00
// only use one type of mag fusion at the same time
_control_status . flags . mag_3D = _flt_mag_align_complete ;
_control_status . flags . mag_hdg = ! _control_status . flags . mag_3D ;
2016-06-07 03:54:12 -03:00
} else {
2017-05-11 20:14:39 -03:00
// save magnetic field state variances for next time
if ( _control_status . flags . mag_3D ) {
for ( uint8_t index = 0 ; index < = 5 ; index + + ) {
2018-05-03 15:14:12 -03:00
_saved_mag_variance [ index ] = P [ index + 16 ] [ index + 16 ] ;
2017-05-11 20:14:39 -03:00
}
2018-05-03 15:14:12 -03:00
2017-05-11 20:14:39 -03:00
_control_status . flags . mag_3D = false ;
}
2018-05-03 15:14:12 -03:00
2016-06-07 03:54:12 -03:00
_control_status . flags . mag_hdg = true ;
}
2017-09-12 21:17:15 -03:00
/*
Control switch - over between only updating the mag states to updating all states
When flying as a fixed wing aircraft , a misaligned magnetometer can cause an error in pitch / roll and accel bias estimates .
When MAG_FUSE_TYPE_AUTOFW is selected and the vehicle is flying as a fixed wing , then magnetometer fusion is only allowed
to access the magnetic field states .
*/
_control_status . flags . update_mag_states_only = ( _params . mag_fusion_type = = MAG_FUSE_TYPE_AUTOFW )
& & _control_status . flags . fixed_wing ;
2017-11-09 19:32:49 -04:00
// For the first 5 seconds after switching to 3-axis fusion we allow the magnetic field state estimates to stabilise
// before they are used to constrain heading drift
_flt_mag_align_converging = ( ( _imu_sample_delayed . time_us - _flt_mag_align_start_time ) < ( uint64_t ) 5e6 ) ;
2017-11-08 19:29:28 -04:00
2017-05-25 10:20:10 -03:00
if ( ! _control_status . flags . update_mag_states_only & & _control_status_prev . flags . update_mag_states_only ) {
2017-05-31 05:18:40 -03:00
// When re-commencing use of magnetometer to correct vehicle states
// set the field state variance to the observation variance and zero
// the covariance terms to allow the field states re-learn rapidly
2017-05-25 10:20:10 -03:00
zeroRows ( P , 16 , 21 ) ;
zeroCols ( P , 16 , 21 ) ;
2018-05-03 15:14:12 -03:00
2017-05-25 10:20:10 -03:00
for ( uint8_t index = 0 ; index < = 5 ; index + + ) {
2018-05-03 15:14:12 -03:00
P [ index + 16 ] [ index + 16 ] = sq ( _params . mag_noise ) ;
2017-05-25 10:20:10 -03:00
}
}
2016-06-07 03:54:12 -03:00
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_HEADING ) {
// always use heading fusion
_control_status . flags . mag_hdg = true ;
_control_status . flags . mag_3D = false ;
2016-06-06 08:59:46 -03:00
2016-06-07 03:54:12 -03:00
} else if ( _params . mag_fusion_type = = MAG_FUSE_TYPE_3D ) {
2016-06-06 08:59:46 -03:00
// if transitioning into 3-axis fusion mode, we need to initialise the yaw angle and field states
if ( ! _control_status . flags . mag_3D ) {
2017-11-08 19:16:48 -04:00
_control_status . flags . yaw_align = resetMagHeading ( _mag_sample_delayed . mag ) | | _control_status . flags . yaw_align ;
2016-06-06 08:59:46 -03:00
}
2016-02-11 23:38:24 -04:00
2016-06-07 03:54:12 -03:00
// always use 3-axis mag fusion
2016-06-06 08:59:46 -03:00
_control_status . flags . mag_hdg = false ;
_control_status . flags . mag_3D = true ;
2016-05-09 21:21:45 -03:00
2016-06-06 08:59:46 -03:00
} else {
2016-06-07 03:54:12 -03:00
// do no magnetometer fusion at all
_control_status . flags . mag_hdg = false ;
2016-06-06 08:59:46 -03:00
_control_status . flags . mag_3D = false ;
}
2016-05-09 21:21:45 -03:00
2016-06-07 03:54:12 -03: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
2017-05-11 20:14:39 -03:00
// fusing declination when gps aiding is available is optional, but recommended to prevent problem if the vehicle is static for extended periods of time
2016-06-07 03:54:12 -03:00
if ( _control_status . flags . mag_3D & & ( ! _control_status . flags . gps | | ( _params . mag_declination_source & MASK_FUSE_DECL ) ) ) {
_control_status . flags . mag_dec = true ;
2016-01-31 04:01:44 -04:00
2016-06-07 03:54:12 -03:00
} else {
_control_status . flags . mag_dec = false ;
2016-02-24 05:17:22 -04:00
}
2016-06-07 03:54:12 -03:00
// fuse magnetometer data using the selected methods
if ( _control_status . flags . mag_3D & & _control_status . flags . yaw_align ) {
fuseMag ( ) ;
2016-01-31 04:01:44 -04:00
2016-06-07 03:54:12 -03:00
if ( _control_status . flags . mag_dec ) {
fuseDeclination ( ) ;
}
} else if ( _control_status . flags . mag_hdg & & _control_status . flags . yaw_align ) {
// fusion of an Euler yaw angle from either a 321 or 312 rotation sequence
fuseHeading ( ) ;
} else {
// do no fusion at all
}
2016-01-31 04:01:44 -04:00
}
2016-06-07 03:54:12 -03:00
}
2016-02-07 23:15:41 -04:00
2016-06-07 03:54:12 -03:00
void Ekf : : controlVelPosFusion ( )
{
// 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
2018-03-22 08:39:00 -03:00
if ( ! ( _params . fusion_mode & MASK_USE_GPS ) ) {
_control_status . flags . gps = false ;
}
2018-05-03 15:14:12 -03:00
2017-10-19 04:21:47 -03:00
if ( ! _control_status . flags . gps & &
2018-05-03 15:14:12 -03:00
! _control_status . flags . opt_flow & &
! _control_status . flags . ev_pos & &
! ( _control_status . flags . fuse_aspd & & _control_status . flags . fuse_beta ) ) {
2018-04-18 22:03:50 -03:00
// We now need to use a synthetic positon observation to prevent unconstrained drift of the INS states.
_using_synthetic_position = true ;
// Fuse synthetic position observations every 200msec
if ( ( _time_last_imu - _time_last_fake_gps > ( uint64_t ) 2e5 ) | | _fuse_height ) {
// Reset position and velocity states if we re-commence this aiding method
if ( ( _time_last_imu - _time_last_fake_gps ) > ( uint64_t ) 4e5 ) {
resetPosition ( ) ;
resetVelocity ( ) ;
_fuse_hpos_as_odom = false ;
2018-05-03 15:14:12 -03:00
2018-04-18 22:03:50 -03:00
if ( _time_last_fake_gps ! = 0 ) {
ECL_WARN ( " EKF stopping navigation " ) ;
}
2017-10-19 18:25:50 -03:00
2018-04-18 22:03:50 -03:00
}
2017-10-19 18:25:50 -03:00
2018-04-18 22:03:50 -03:00
_fuse_pos = true ;
_fuse_hor_vel = false ;
_fuse_vert_vel = false ;
_time_last_fake_gps = _time_last_imu ;
2016-02-08 00:12:38 -04:00
2018-04-18 22:03:50 -03:00
if ( _control_status . flags . in_air & & _control_status . flags . tilt_align ) {
_posObsNoiseNE = fmaxf ( _params . pos_noaid_noise , _params . gps_pos_noise ) ;
2018-05-03 15:14:12 -03:00
2018-04-18 22:03:50 -03:00
} else {
_posObsNoiseNE = 0.5f ;
}
2018-05-03 15:14:12 -03:00
2018-04-18 22:03:50 -03:00
_vel_pos_innov [ 0 ] = 0.0f ;
_vel_pos_innov [ 1 ] = 0.0f ;
_vel_pos_innov [ 2 ] = 0.0f ;
_vel_pos_innov [ 3 ] = _state . pos ( 0 ) - _last_known_posNE ( 0 ) ;
_vel_pos_innov [ 4 ] = _state . pos ( 1 ) - _last_known_posNE ( 1 ) ;
2017-10-22 18:28:22 -03:00
2018-04-18 22:03:50 -03:00
// glitch protection is not required so set gate to a large value
_posInnovGateNE = 100.0f ;
2017-10-22 18:28:22 -03:00
2018-04-18 22:03:50 -03:00
}
2018-05-03 15:14:12 -03:00
2018-04-18 22:03:50 -03:00
} else {
_using_synthetic_position = false ;
2016-02-08 00:12:38 -04:00
}
2016-02-11 23:38:24 -04:00
2016-06-07 03:54:12 -03:00
// Fuse available NED velocity and position data into the main filter
if ( _fuse_height | | _fuse_pos | | _fuse_hor_vel | | _fuse_vert_vel ) {
fuseVelPosHeight ( ) ;
2016-02-15 19:04:37 -04:00
}
2016-01-28 06:52:39 -04:00
}
2017-11-17 16:44:28 -04:00
void Ekf : : controlAuxVelFusion ( )
{
bool data_ready = _auxvel_buffer . pop_first_older_than ( _imu_sample_delayed . time_us , & _auxvel_sample_delayed ) ;
bool primary_aiding = _control_status . flags . gps | | _control_status . flags . ev_pos | | _control_status . flags . opt_flow ;
if ( data_ready & & primary_aiding ) {
2017-11-20 20:00:16 -04:00
_fuse_hor_vel = _fuse_vert_vel = _fuse_pos = _fuse_height = false ;
_fuse_hor_vel_aux = true ;
_aux_vel_innov [ 0 ] = _state . vel ( 0 ) - _auxvel_sample_delayed . velNE ( 0 ) ;
_aux_vel_innov [ 1 ] = _state . vel ( 1 ) - _auxvel_sample_delayed . velNE ( 1 ) ;
2017-11-17 16:44:28 -04:00
_velObsVarNE = _auxvel_sample_delayed . velVarNE ;
_hvelInnovGate = _params . auxvel_gate ;
fuseVelPosHeight ( ) ;
}
}