From bd8384b32244fddb071e6f3b313252607010de61 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 9 Feb 2021 13:24:24 +1100 Subject: [PATCH] autotest: break out a get_altitude method --- Tools/autotest/common.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Tools/autotest/common.py b/Tools/autotest/common.py index 54efa5bbc9..8e1c4b0a14 100644 --- a/Tools/autotest/common.py +++ b/Tools/autotest/common.py @@ -4348,26 +4348,39 @@ class AutoTest(ABC): while tstart + seconds_to_wait > tnow: tnow = self.get_sim_time() + def get_altitude(self, relative=False, timeout=30): + '''returns vehicles altitude in metres, possibly relative-to-home''' + msg = self.mav.recv_match(type='GLOBAL_POSITION_INT', + blocking=True, + timeout=timeout) + if msg is None: + raise MsgRcvTimeoutException("Failed to get Global Position") + if relative: + return msg.relative_alt / 1000.0 # mm -> m + return msg.alt / 1000.0 # mm -> m + def wait_altitude(self, altitude_min, altitude_max, relative=False, timeout=30, **kwargs): """Wait for a given altitude range.""" assert altitude_min <= altitude_max, "Minimum altitude should be less than maximum altitude." - def get_altitude(alt_relative=False, timeout2=30): - msg = self.mav.recv_match(type='GLOBAL_POSITION_INT', blocking=True, timeout=timeout2) - if msg: - if alt_relative: - return msg.relative_alt / 1000.0 # mm -> m - else: - return msg.alt / 1000.0 # mm -> m - raise MsgRcvTimeoutException("Failed to get Global Position") - def validator(value2, target2=None): if altitude_min <= value2 <= altitude_max: return True else: return False - self.wait_and_maintain(value_name="Altitude", target=altitude_min, current_value_getter=lambda: get_altitude(relative, timeout), accuracy=(altitude_max - altitude_min), validator=lambda value2, target2: validator(value2, target2), timeout=timeout, **kwargs) + self.wait_and_maintain( + value_name="Altitude", + target=altitude_min, + current_value_getter=lambda: self.get_altitude( + relative=relative, + timeout=timeout, + ), + accuracy=(altitude_max - altitude_min), + validator=lambda value2, target2: validator(value2, target2), + timeout=timeout, + **kwargs, + ) def wait_groundspeed(self, speed_min, speed_max, timeout=30, **kwargs): """Wait for a given ground speed range."""