From 3d29489e75d755ef5d1696dd5c96e25703f1e007 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Mon, 9 Sep 2024 14:30:49 +1000 Subject: [PATCH] mavproxy_console: avoid exceptions when terrain module unloaded --- MAVProxy/modules/mavproxy_console.py | 43 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/MAVProxy/modules/mavproxy_console.py b/MAVProxy/modules/mavproxy_console.py index c85c964b02..e530c9d774 100644 --- a/MAVProxy/modules/mavproxy_console.py +++ b/MAVProxy/modules/mavproxy_console.py @@ -99,6 +99,8 @@ def __init__(self, mpstate): self.vehicle_menu = MPMenuSubMenu('Vehicle', items=[]) self.add_menu(self.vehicle_menu) + self.shown_agl = False + def cmd_console(self, args): usage = 'usage: console ' if len(args) < 1: @@ -391,28 +393,35 @@ def handle_vfr_hud(self, msg): lng = master.field('GLOBAL_POSITION_INT', 'lon', 0) * 1.0e-7 rel_alt = master.field('GLOBAL_POSITION_INT', 'relative_alt', 0) * 1.0e-3 agl_alt = None - if self.settings.basealt != 0: - agl_alt = self.module('terrain').ElevationModel.GetElevation(lat, lng) - if agl_alt is not None: - agl_alt = self.settings.basealt - agl_alt - else: - try: - agl_alt_home = self.module('terrain').ElevationModel.GetElevation(home_lat, home_lng) - except Exception as ex: - print(ex) - agl_alt_home = None - if agl_alt_home is not None: - agl_alt = self.module('terrain').ElevationModel.GetElevation(lat, lng) + if self.module('terrain') is not None: + elevation_model = self.module('terrain').ElevationModel + if self.settings.basealt != 0: + agl_alt = elevation_model.GetElevation(lat, lng) + if agl_alt is not None: + agl_alt = self.settings.basealt - agl_alt + else: + try: + agl_alt_home = elevation_model.GetElevation(home_lat, home_lng) + except Exception as ex: + print(ex) + agl_alt_home = None + if agl_alt_home is not None: + agl_alt = elevation_model.GetElevation(lat, lng) + if agl_alt is not None: + agl_alt = agl_alt_home - agl_alt + vehicle_agl = master.field('TERRAIN_REPORT', 'current_height', None) + if agl_alt is not None or vehicle_agl is not None or self.shown_agl: + self.shown_agl = True if agl_alt is not None: - agl_alt = agl_alt_home - agl_alt - if agl_alt is not None: - agl_alt += rel_alt - vehicle_agl = master.field('TERRAIN_REPORT', 'current_height', None) + agl_alt += rel_alt + agl_alt = self.height_string(agl_alt) + else: + agl_alt = "---" if vehicle_agl is None: vehicle_agl = '---' else: vehicle_agl = self.height_string(vehicle_agl) - self.console.set_status('AGL', 'AGL %s/%s' % (self.height_string(agl_alt), vehicle_agl)) + self.console.set_status('AGL', 'AGL %s/%s' % (agl_alt, vehicle_agl)) self.console.set_status('Alt', 'Alt %s' % self.height_string(rel_alt)) self.console.set_status('AirSpeed', 'AirSpeed %s' % self.speed_string(msg.airspeed)) self.console.set_status('GPSSpeed', 'GPSSpeed %s' % self.speed_string(msg.groundspeed))