diff --git a/src/modules/ekf2/EKF/control.cpp b/src/modules/ekf2/EKF/control.cpp index cc55440909..3cd981cc6a 100644 --- a/src/modules/ekf2/EKF/control.cpp +++ b/src/modules/ekf2/EKF/control.cpp @@ -388,7 +388,7 @@ void Ekf::controlOpticalFlowFusion() // compensate for body motion to give a LOS rate _flow_compensated_XY_rad = _flow_sample_delayed.flow_xy_rad - _flow_sample_delayed.gyro_xyz.xy(); - } else if (!_control_status.flags.in_air && is_body_rate_comp_available) { + } else if (!_control_status.flags.in_air) { if (!is_delta_time_good) { // handle special case of SITL and PX4Flow where dt is forced to diff --git a/src/modules/ekf2/EKF/optflow_fusion.cpp b/src/modules/ekf2/EKF/optflow_fusion.cpp index f2845bba12..d158f90523 100644 --- a/src/modules/ekf2/EKF/optflow_fusion.cpp +++ b/src/modules/ekf2/EKF/optflow_fusion.cpp @@ -340,6 +340,7 @@ bool Ekf::calcOptFlowBodyRateComp() return false; } + bool is_body_rate_comp_available = false; const bool use_flow_sensor_gyro = PX4_ISFINITE(_flow_sample_delayed.gyro_xyz(0)) && PX4_ISFINITE(_flow_sample_delayed.gyro_xyz(1)) && PX4_ISFINITE(_flow_sample_delayed.gyro_xyz(2)); if (use_flow_sensor_gyro) { @@ -356,19 +357,26 @@ bool Ekf::calcOptFlowBodyRateComp() // calculate the bias estimate using a combined LPF and spike filter _flow_gyro_bias = _flow_gyro_bias * 0.99f + matrix::constrain(measured_body_rate - reference_body_rate, -0.1f, 0.1f) * 0.01f; + + is_body_rate_comp_available = true; } } else { // Use the EKF gyro data if optical flow sensor gyro data is not available // for clarification of the sign see definition of flowSample and imuSample in common.h - _flow_sample_delayed.gyro_xyz = -_imu_del_ang_of; - _flow_gyro_bias.zero(); + if ((_delta_time_of > FLT_EPSILON) + && (_flow_sample_delayed.dt > FLT_EPSILON)) { + _flow_sample_delayed.gyro_xyz = -_imu_del_ang_of / _delta_time_of * _flow_sample_delayed.dt; + _flow_gyro_bias.zero(); + + is_body_rate_comp_available = true; + } } // reset the accumulators _imu_del_ang_of.setZero(); _delta_time_of = 0.0f; - return true; + return is_body_rate_comp_available; } // calculate the measurement variance for the optical flow sensor (rad/sec)^2