@@ -242,6 +242,7 @@ void Ekf::fuseFlowForTerrain()
242
242
_terrain_vpos += Kx * _flow_innov[0 ];
243
243
// guard against negative variance
244
244
_terrain_var = fmaxf (_terrain_var - KxHxP, 0 .0f );
245
+ _time_last_of_fuse = _time_last_imu;
245
246
}
246
247
247
248
// Calculate observation matrix for flow around the vehicle y axis
@@ -269,6 +270,7 @@ void Ekf::fuseFlowForTerrain()
269
270
_terrain_vpos += Ky * _flow_innov[1 ];
270
271
// guard against negative variance
271
272
_terrain_var = fmaxf (_terrain_var - KyHyP, 0 .0f );
273
+ _time_last_of_fuse = _time_last_imu;
272
274
}
273
275
}
274
276
@@ -281,7 +283,15 @@ bool Ekf::get_terrain_valid()
281
283
// determine terrain validity
282
284
void Ekf::update_terrain_valid ()
283
285
{
284
- if (_terrain_initialised && ((_time_last_imu - _time_last_hagl_fuse) < (uint64_t )5e6 )) {
286
+ // we have been fusing range finder measurements in the last 5 seconds
287
+ bool recent_range_fusion = (_time_last_imu - _time_last_hagl_fuse) < 5 *1000 *1000 ;
288
+
289
+ // we have been fusing optical flow measurements for terrain estimation within the last 5 seconds
290
+ // this can only be the case if the main filter does not fuse optical flow
291
+ bool recent_flow_for_terrain_fusion = ((_time_last_imu - _time_last_of_fuse) < 5 *1000 *1000 ) && !_control_status.flags .opt_flow ;
292
+
293
+
294
+ if (_terrain_initialised && (recent_range_fusion || recent_flow_for_terrain_fusion)) {
285
295
286
296
_hagl_valid = true ;
287
297
0 commit comments