diff --git a/libraries/AP_Terrain/AP_Terrain.cpp b/libraries/AP_Terrain/AP_Terrain.cpp index 3e9c7e02bf..d550661a08 100644 --- a/libraries/AP_Terrain/AP_Terrain.cpp +++ b/libraries/AP_Terrain/AP_Terrain.cpp @@ -241,6 +241,49 @@ bool AP_Terrain::height_relative_home_equivalent(float terrain_altitude, return true; } + +/* + calculate lookahead rise in terrain. This returns extra altitude + needed to clear upcoming terrain in meters +*/ +float AP_Terrain::lookahead(float bearing, float distance, float climb_ratio) +{ + if (!enable || grid_spacing <= 0) { + return 0; + } + + Location loc; + if (!ahrs.get_position(loc)) { + // we don't know where we are + return 0; + } + float base_height; + if (!height_amsl(loc, base_height)) { + // we don't know our current terrain height + return 0; + } + + float climb = 0; + float lookahead_estimate = 0; + + // check for terrain at grid spacing intervals + while (distance > 0) { + location_update(loc, bearing, grid_spacing); + climb += climb_ratio * grid_spacing; + distance -= grid_spacing; + float height; + if (height_amsl(loc, height)) { + float rise = (height - base_height) - climb; + if (rise > lookahead_estimate) { + lookahead_estimate = rise; + } + } + } + + return lookahead_estimate; +} + + /* 1hz update function. This is here to ensure progress is made on disk IO even if no MAVLink send_request() operations are called for a diff --git a/libraries/AP_Terrain/AP_Terrain.h b/libraries/AP_Terrain/AP_Terrain.h index d0a59fb1b6..adc63f0a73 100644 --- a/libraries/AP_Terrain/AP_Terrain.h +++ b/libraries/AP_Terrain/AP_Terrain.h @@ -154,6 +154,12 @@ public: */ bool height_above_terrain(float &terrain_altitude, bool extrapolate = false); + /* + calculate lookahead rise in terrain. This returns extra altitude + needed to clear upcoming terrain in meters + */ + float lookahead(float bearing, float distance, float climb_ratio); + /* log terrain status to DataFlash */