2016-07-31 07:22:06 -03:00
|
|
|
import math
|
|
|
|
import time
|
|
|
|
|
2013-06-18 03:27:21 -03:00
|
|
|
from pymavlink import mavwp
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
from pysim import util
|
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
# a list of pexpect objects to read while waiting for
|
|
|
|
# messages. This keeps the output to stdout flowing
|
|
|
|
expect_list = []
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:21:15 -04:00
|
|
|
def expect_list_clear():
|
2016-07-31 07:22:06 -03:00
|
|
|
"""clear the expect list."""
|
2011-12-02 00:21:15 -04:00
|
|
|
global expect_list
|
|
|
|
for p in expect_list[:]:
|
|
|
|
expect_list.remove(p)
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
def expect_list_extend(list_to_add):
|
|
|
|
"""Extend the expect list."""
|
2011-12-02 00:21:15 -04:00
|
|
|
global expect_list
|
2016-07-31 07:22:06 -03:00
|
|
|
expect_list.extend(list_to_add)
|
|
|
|
|
2011-12-02 00:21:15 -04:00
|
|
|
|
2011-11-27 22:52:54 -04:00
|
|
|
def idle_hook(mav):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Called when waiting for a mavlink message."""
|
2011-11-12 05:01:58 -04:00
|
|
|
global expect_list
|
|
|
|
for p in expect_list:
|
2011-12-02 00:21:15 -04:00
|
|
|
util.pexpect_drain(p)
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-27 22:52:54 -04:00
|
|
|
def message_hook(mav, msg):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Called as each mavlink msg is received."""
|
2011-11-27 22:52:54 -04:00
|
|
|
idle_hook(mav)
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def expect_callback(e):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Called when waiting for a expect pattern."""
|
2011-11-12 05:01:58 -04:00
|
|
|
global expect_list
|
|
|
|
for p in expect_list:
|
|
|
|
if p == e:
|
|
|
|
continue
|
2011-12-02 00:21:15 -04:00
|
|
|
util.pexpect_drain(p)
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def get_distance(loc1, loc2):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Get ground distance between two locations."""
|
|
|
|
dlat = loc2.lat - loc1.lat
|
|
|
|
dlong = loc2.lng - loc1.lng
|
2011-11-12 05:01:58 -04:00
|
|
|
return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def get_bearing(loc1, loc2):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Get bearing from loc1 to loc2."""
|
2011-11-12 05:01:58 -04:00
|
|
|
off_x = loc2.lng - loc1.lng
|
|
|
|
off_y = loc2.lat - loc1.lat
|
|
|
|
bearing = 90.00 + math.atan2(-off_y, off_x) * 57.2957795
|
|
|
|
if bearing < 0:
|
|
|
|
bearing += 360.00
|
2016-07-31 07:22:06 -03:00
|
|
|
return bearing
|
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2015-04-12 20:28:24 -03:00
|
|
|
def wait_seconds(mav, seconds_to_wait):
|
|
|
|
tstart = get_sim_time(mav)
|
2013-11-02 08:47:18 -03:00
|
|
|
tnow = tstart
|
|
|
|
while tstart + seconds_to_wait > tnow:
|
2015-04-12 20:28:24 -03:00
|
|
|
tnow = get_sim_time(mav)
|
2015-04-07 20:24:39 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2015-04-07 20:24:39 -03:00
|
|
|
def get_sim_time(mav):
|
|
|
|
m = mav.recv_match(type='SYSTEM_TIME', blocking=True)
|
2015-04-12 20:28:24 -03:00
|
|
|
return m.time_boot_ms * 1.0e-3
|
2015-04-07 20:24:39 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def wait_altitude(mav, alt_min, alt_max, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for a given altitude range."""
|
2011-11-19 18:03:08 -04:00
|
|
|
climb_rate = 0
|
|
|
|
previous_alt = 0
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2011-11-12 05:01:58 -04:00
|
|
|
print("Waiting for altitude between %u and %u" % (alt_min, alt_max))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2011-11-12 05:01:58 -04:00
|
|
|
m = mav.recv_match(type='VFR_HUD', blocking=True)
|
2016-07-31 07:22:06 -03:00
|
|
|
climb_rate = m.alt - previous_alt
|
2011-11-19 18:03:08 -04:00
|
|
|
previous_alt = m.alt
|
2016-07-31 07:22:06 -03:00
|
|
|
print("Wait Altitude: Cur:%u, min_alt:%u, climb_rate: %u" % (m.alt, alt_min, climb_rate))
|
2011-11-12 05:01:58 -04:00
|
|
|
if m.alt >= alt_min and m.alt <= alt_max:
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Altitude OK")
|
2011-11-12 05:01:58 -04:00
|
|
|
return True
|
|
|
|
print("Failed to attain altitude range")
|
|
|
|
return False
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-18 02:29:36 -04:00
|
|
|
def wait_groundspeed(mav, gs_min, gs_max, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for a given ground speed range."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2011-12-18 02:29:36 -04:00
|
|
|
print("Waiting for groundspeed between %.1f and %.1f" % (gs_min, gs_max))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2011-12-18 02:29:36 -04:00
|
|
|
m = mav.recv_match(type='VFR_HUD', blocking=True)
|
|
|
|
print("Wait groundspeed %.1f, target:%.1f" % (m.groundspeed, gs_min))
|
|
|
|
if m.groundspeed >= gs_min and m.groundspeed <= gs_max:
|
|
|
|
return True
|
|
|
|
print("Failed to attain groundspeed range")
|
|
|
|
return False
|
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2011-11-12 07:12:38 -04:00
|
|
|
def wait_roll(mav, roll, accuracy, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for a given roll in degrees."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Waiting for roll of %d at %s" % (roll, time.ctime()))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2011-11-12 07:12:38 -04:00
|
|
|
m = mav.recv_match(type='ATTITUDE', blocking=True)
|
2013-10-09 19:53:51 -03:00
|
|
|
p = math.degrees(m.pitch)
|
2011-11-12 07:12:38 -04:00
|
|
|
r = math.degrees(m.roll)
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Roll %d Pitch %d" % (r, p))
|
2011-11-12 07:12:38 -04:00
|
|
|
if math.fabs(r - roll) <= accuracy:
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Attained roll %d" % roll)
|
2011-11-12 07:12:38 -04:00
|
|
|
return True
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Failed to attain roll %d" % roll)
|
2011-11-12 07:12:38 -04:00
|
|
|
return False
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-13 21:59:59 -04:00
|
|
|
def wait_pitch(mav, pitch, accuracy, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for a given pitch in degrees."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Waiting for pitch of %u at %s" % (pitch, time.ctime()))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2011-11-13 21:59:59 -04:00
|
|
|
m = mav.recv_match(type='ATTITUDE', blocking=True)
|
2013-10-09 19:53:51 -03:00
|
|
|
p = math.degrees(m.pitch)
|
|
|
|
r = math.degrees(m.roll)
|
|
|
|
print("Pitch %d Roll %d" % (p, r))
|
2013-10-13 20:18:40 -03:00
|
|
|
if math.fabs(p - pitch) <= accuracy:
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Attained pitch %d" % pitch)
|
2011-11-13 21:59:59 -04:00
|
|
|
return True
|
2013-10-09 19:53:51 -03:00
|
|
|
print("Failed to attain pitch %d" % pitch)
|
2011-11-13 21:59:59 -04:00
|
|
|
return False
|
|
|
|
|
2011-11-12 07:12:38 -04:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def wait_heading(mav, heading, accuracy=5, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for a given heading."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2013-07-16 00:23:48 -03:00
|
|
|
print("Waiting for heading %u with accuracy %u" % (heading, accuracy))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2011-11-12 05:01:58 -04:00
|
|
|
m = mav.recv_match(type='VFR_HUD', blocking=True)
|
|
|
|
print("Heading %u" % m.heading)
|
|
|
|
if math.fabs(m.heading - heading) <= accuracy:
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Attained heading %u" % heading)
|
2011-11-12 05:01:58 -04:00
|
|
|
return True
|
|
|
|
print("Failed to attain heading %u" % heading)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def wait_distance(mav, distance, accuracy=5, timeout=30):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for flight of a given distance."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2012-04-27 00:32:22 -03:00
|
|
|
start = mav.location()
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2012-04-27 00:32:22 -03:00
|
|
|
pos = mav.location()
|
2011-11-12 05:01:58 -04:00
|
|
|
delta = get_distance(start, pos)
|
|
|
|
print("Distance %.2f meters" % delta)
|
|
|
|
if math.fabs(delta - distance) <= accuracy:
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Attained distance %.2f meters OK" % delta)
|
2011-11-12 05:01:58 -04:00
|
|
|
return True
|
2012-02-13 20:19:31 -04:00
|
|
|
if delta > (distance + accuracy):
|
|
|
|
print("Failed distance - overshoot delta=%f distance=%f" % (delta, distance))
|
2011-12-16 00:42:10 -04:00
|
|
|
return False
|
2011-11-12 05:01:58 -04:00
|
|
|
print("Failed to attain distance %u" % distance)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def wait_location(mav, loc, accuracy=5, timeout=30, target_altitude=None, height_accuracy=-1):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for arrival at a location."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2011-11-12 05:01:58 -04:00
|
|
|
if target_altitude is None:
|
|
|
|
target_altitude = loc.alt
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Waiting for location %.4f,%.4f at altitude %.1f height_accuracy=%.1f" % (
|
|
|
|
loc.lat, loc.lng, target_altitude, height_accuracy))
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2012-04-27 00:32:22 -03:00
|
|
|
pos = mav.location()
|
2011-11-12 05:01:58 -04:00
|
|
|
delta = get_distance(loc, pos)
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Distance %.2f meters alt %.1f" % (delta, pos.alt))
|
2011-11-12 05:01:58 -04:00
|
|
|
if delta <= accuracy:
|
|
|
|
if height_accuracy != -1 and math.fabs(pos.alt - target_altitude) > height_accuracy:
|
|
|
|
continue
|
|
|
|
print("Reached location (%.2f meters)" % delta)
|
|
|
|
return True
|
|
|
|
print("Failed to attain location")
|
|
|
|
return False
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2016-05-11 20:06:12 -03:00
|
|
|
def wait_waypoint(mav, wpnum_start, wpnum_end, allow_skip=True, max_dist=2, timeout=400):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Wait for waypoint ranges."""
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2011-11-14 02:58:27 -04:00
|
|
|
# this message arrives after we set the current WP
|
2012-04-27 00:32:22 -03:00
|
|
|
start_wp = mav.waypoint_current()
|
2011-11-12 05:01:58 -04:00
|
|
|
current_wp = start_wp
|
2016-05-11 20:06:12 -03:00
|
|
|
mode = mav.flightmode
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2011-11-14 02:58:27 -04:00
|
|
|
print("\ntest: wait for waypoint ranges start=%u end=%u\n\n" % (wpnum_start, wpnum_end))
|
|
|
|
# if start_wp != wpnum_start:
|
|
|
|
# print("test: Expected start waypoint %u but got %u" % (wpnum_start, start_wp))
|
|
|
|
# return False
|
2011-11-12 05:01:58 -04:00
|
|
|
|
2015-04-12 20:28:24 -03:00
|
|
|
while get_sim_time(mav) < tstart + timeout:
|
2012-04-27 00:32:22 -03:00
|
|
|
seq = mav.waypoint_current()
|
2011-11-13 08:50:04 -04:00
|
|
|
m = mav.recv_match(type='NAV_CONTROLLER_OUTPUT', blocking=True)
|
|
|
|
wp_dist = m.wp_dist
|
2012-01-11 02:57:19 -04:00
|
|
|
m = mav.recv_match(type='VFR_HUD', blocking=True)
|
|
|
|
|
2016-05-11 20:06:12 -03:00
|
|
|
# if we changed mode, fail
|
|
|
|
if mav.flightmode != mode:
|
2013-01-14 03:03:10 -04:00
|
|
|
print('Exited %s mode' % mode)
|
2016-05-11 20:06:12 -03:00
|
|
|
return False
|
2013-01-14 03:03:10 -04:00
|
|
|
|
2012-01-11 02:57:19 -04:00
|
|
|
print("test: WP %u (wp_dist=%u Alt=%d), current_wp: %u, wpnum_end: %u" % (seq, wp_dist, m.alt, current_wp, wpnum_end))
|
2011-11-13 08:50:04 -04:00
|
|
|
if seq == current_wp+1 or (seq > current_wp+1 and allow_skip):
|
2011-11-14 02:58:27 -04:00
|
|
|
print("test: Starting new waypoint %u" % seq)
|
2015-04-12 20:28:24 -03:00
|
|
|
tstart = get_sim_time(mav)
|
2011-11-13 08:50:04 -04:00
|
|
|
current_wp = seq
|
|
|
|
# the wp_dist check is a hack until we can sort out the right seqnum
|
|
|
|
# for end of mission
|
2016-07-31 07:22:06 -03:00
|
|
|
# if current_wp == wpnum_end or (current_wp == wpnum_end-1 and wp_dist < 2):
|
2011-12-18 02:29:36 -04:00
|
|
|
if (current_wp == wpnum_end and wp_dist < max_dist):
|
2011-12-16 00:42:10 -04:00
|
|
|
print("Reached final waypoint %u" % seq)
|
|
|
|
return True
|
2012-01-04 13:51:36 -04:00
|
|
|
if (seq >= 255):
|
2011-11-13 08:50:04 -04:00
|
|
|
print("Reached final waypoint %u" % seq)
|
|
|
|
return True
|
|
|
|
if seq > current_wp+1:
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Failed: Skipped waypoint! Got wp %u expected %u" % (seq, current_wp+1))
|
2011-11-12 05:01:58 -04:00
|
|
|
return False
|
2012-02-13 20:19:31 -04:00
|
|
|
print("Failed: Timed out waiting for waypoint %u of %u" % (wpnum_end, wpnum_end))
|
2011-11-12 05:01:58 -04:00
|
|
|
return False
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-12 05:01:58 -04:00
|
|
|
def save_wp(mavproxy, mav):
|
2013-11-02 08:47:18 -03:00
|
|
|
mavproxy.send('rc 7 1000\n')
|
2016-10-13 21:40:23 -03:00
|
|
|
mav.recv_match(condition='RC_CHANNELS.chan7_raw==1000', blocking=True)
|
2015-04-12 20:28:24 -03:00
|
|
|
wait_seconds(mav, 1)
|
2011-11-12 05:01:58 -04:00
|
|
|
mavproxy.send('rc 7 2000\n')
|
2016-10-13 21:40:23 -03:00
|
|
|
mav.recv_match(condition='RC_CHANNELS.chan7_raw==2000', blocking=True)
|
2015-04-12 20:28:24 -03:00
|
|
|
wait_seconds(mav, 1)
|
2011-11-12 05:01:58 -04:00
|
|
|
mavproxy.send('rc 7 1000\n')
|
2016-10-13 21:40:23 -03:00
|
|
|
mav.recv_match(condition='RC_CHANNELS.chan7_raw==1000', blocking=True)
|
2015-04-12 20:28:24 -03:00
|
|
|
wait_seconds(mav, 1)
|
2011-11-12 07:12:38 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2013-10-02 05:45:10 -03:00
|
|
|
def wait_mode(mav, mode, timeout=None):
|
2013-07-16 00:23:48 -03:00
|
|
|
print("Waiting for mode %s" % mode)
|
2013-10-02 05:45:10 -03:00
|
|
|
mav.recv_match(condition='MAV.flightmode.upper()=="%s".upper()' % mode, timeout=timeout, blocking=True)
|
2013-07-16 00:23:48 -03:00
|
|
|
print("Got mode %s" % mode)
|
2013-10-02 05:45:10 -03:00
|
|
|
return mav.flightmode
|
2011-12-18 02:29:36 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-18 02:29:36 -04:00
|
|
|
def mission_count(filename):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Load a mission from a file and return number of waypoints."""
|
2011-12-18 02:29:36 -04:00
|
|
|
wploader = mavwp.MAVWPLoader()
|
|
|
|
wploader.load(filename)
|
|
|
|
num_wp = wploader.count()
|
|
|
|
return num_wp
|
2013-09-19 03:51:03 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2013-09-19 03:51:03 -03:00
|
|
|
def sim_location(mav):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return current simulator location."""
|
2013-09-19 03:51:03 -03:00
|
|
|
from pymavlink import mavutil
|
|
|
|
m = mav.recv_match(type='SIMSTATE', blocking=True)
|
2013-10-09 19:53:51 -03:00
|
|
|
return mavutil.location(m.lat*1.0e-7, m.lng*1.0e-7, 0, math.degrees(m.yaw))
|
2013-12-28 06:47:12 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2014-05-08 02:15:20 -03:00
|
|
|
def log_download(mavproxy, mav, filename, timeout=360):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Download latest log."""
|
2013-12-28 06:47:12 -04:00
|
|
|
mavproxy.send("log list\n")
|
|
|
|
mavproxy.expect("numLogs")
|
|
|
|
mav.wait_heartbeat()
|
|
|
|
mav.wait_heartbeat()
|
2016-01-10 02:35:23 -04:00
|
|
|
mavproxy.send("set shownoise 0\n")
|
2013-12-28 06:47:12 -04:00
|
|
|
mavproxy.send("log download latest %s\n" % filename)
|
2014-05-08 20:50:14 -03:00
|
|
|
mavproxy.expect("Finished downloading", timeout=timeout)
|
2013-12-28 16:13:57 -04:00
|
|
|
mav.wait_heartbeat()
|
|
|
|
mav.wait_heartbeat()
|
2013-12-28 06:47:12 -04:00
|
|
|
return True
|