2012-11-27 19:43:11 -04:00
|
|
|
# drive APMrover2 in SITL
|
2016-11-08 07:06:05 -04:00
|
|
|
from __future__ import print_function
|
2016-07-31 07:22:06 -03:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import pexpect
|
2013-06-18 03:27:21 -03:00
|
|
|
from pymavlink import mavutil
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
from common import *
|
|
|
|
from pysim import util
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
# get location of scripts
|
2016-07-31 07:22:06 -03:00
|
|
|
testdir = os.path.dirname(os.path.realpath(__file__))
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
# HOME=mavutil.location(-35.362938,149.165085,584,270)
|
|
|
|
HOME = mavutil.location(40.071374969556928, -105.22978898137808, 1583.702759, 246)
|
2012-11-27 19:43:11 -04:00
|
|
|
homeloc = None
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2017-01-26 01:53:13 -04:00
|
|
|
def wait_ready_to_arm(mavproxy):
|
2016-07-04 21:45:33 -03:00
|
|
|
# wait for EKF and GPS checks to pass
|
2017-01-26 01:53:13 -04:00
|
|
|
mavproxy.expect('IMU0 is using GPS')
|
|
|
|
|
|
|
|
def arm_rover(mavproxy, mav):
|
|
|
|
wait_ready_to_arm(mavproxy);
|
2015-11-10 22:15:48 -04:00
|
|
|
|
|
|
|
mavproxy.send('arm throttle\n')
|
|
|
|
mavproxy.expect('ARMED')
|
|
|
|
|
|
|
|
print("ROVER ARMED")
|
|
|
|
return True
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-11-27 19:43:11 -04:00
|
|
|
def drive_left_circuit(mavproxy, mav):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Drive a left circuit, 50m on a side."""
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.send('switch 6\n')
|
|
|
|
wait_mode(mav, 'MANUAL')
|
|
|
|
mavproxy.send('rc 3 2000\n')
|
|
|
|
|
|
|
|
print("Driving left circuit")
|
|
|
|
# do 4 turns
|
2016-07-31 07:22:06 -03:00
|
|
|
for i in range(0, 4):
|
2012-11-27 19:43:11 -04:00
|
|
|
# hard left
|
|
|
|
print("Starting turn %u" % i)
|
|
|
|
mavproxy.send('rc 1 1000\n')
|
|
|
|
if not wait_heading(mav, 270 - (90*i), accuracy=10):
|
|
|
|
return False
|
|
|
|
mavproxy.send('rc 1 1500\n')
|
|
|
|
print("Starting leg %u" % i)
|
|
|
|
if not wait_distance(mav, 50, accuracy=7):
|
|
|
|
return False
|
2012-11-29 05:53:10 -04:00
|
|
|
mavproxy.send('rc 3 1500\n')
|
2012-11-27 19:43:11 -04:00
|
|
|
print("Circuit complete")
|
|
|
|
return True
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-11-27 19:43:11 -04:00
|
|
|
def drive_RTL(mavproxy, mav):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Drive to home."""
|
2012-11-27 19:43:11 -04:00
|
|
|
print("Driving home in RTL")
|
|
|
|
mavproxy.send('switch 3\n')
|
|
|
|
if not wait_location(mav, homeloc, accuracy=22, timeout=90):
|
|
|
|
return False
|
|
|
|
print("RTL Complete")
|
|
|
|
return True
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-11-27 19:43:11 -04:00
|
|
|
def setup_rc(mavproxy):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Setup RC override control."""
|
|
|
|
for chan in [1, 2, 3, 4, 5, 6, 7]:
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.send('rc %u 1500\n' % chan)
|
|
|
|
mavproxy.send('rc 8 1800\n')
|
|
|
|
|
|
|
|
|
|
|
|
def drive_mission(mavproxy, mav, filename):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Drive a mission from a file."""
|
2012-11-27 19:43:11 -04:00
|
|
|
global homeloc
|
|
|
|
print("Driving mission %s" % filename)
|
|
|
|
mavproxy.send('wp load %s\n' % filename)
|
2015-11-25 21:52:19 -04:00
|
|
|
mavproxy.expect('Flight plan received')
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.send('wp list\n')
|
|
|
|
mavproxy.expect('Requesting [0-9]+ waypoints')
|
2016-07-31 07:22:06 -03:00
|
|
|
mavproxy.send('switch 4\n') # auto mode
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.send('rc 3 1500\n')
|
|
|
|
wait_mode(mav, 'AUTO')
|
|
|
|
if not wait_waypoint(mav, 1, 4, max_dist=5):
|
|
|
|
return False
|
2013-03-29 07:19:04 -03:00
|
|
|
wait_mode(mav, 'HOLD')
|
2012-11-27 19:43:11 -04:00
|
|
|
print("Mission OK")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
def drive_APMrover2(binary, viewerip=None, use_map=False, valgrind=False, gdb=False):
|
|
|
|
"""Drive APMrover2 in SITL.
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
you can pass viewerip as an IP address to optionally send fg and
|
|
|
|
mavproxy packets too for local viewing of the mission in real time
|
2016-07-31 07:22:06 -03:00
|
|
|
"""
|
2012-11-27 19:43:11 -04:00
|
|
|
global homeloc
|
|
|
|
|
|
|
|
options = '--sitl=127.0.0.1:5501 --out=127.0.0.1:19550 --streamrate=10'
|
|
|
|
if viewerip:
|
|
|
|
options += " --out=%s:14550" % viewerip
|
2016-07-31 07:22:06 -03:00
|
|
|
if use_map:
|
2013-09-09 06:55:38 -03:00
|
|
|
options += ' --map'
|
2012-11-27 19:43:11 -04:00
|
|
|
|
2015-05-05 02:46:39 -03:00
|
|
|
home = "%f,%f,%u,%u" % (HOME.lat, HOME.lng, HOME.alt, HOME.heading)
|
2016-07-31 07:22:06 -03:00
|
|
|
sitl = util.start_SITL(binary, wipe=True, model='rover', home=home, speedup=10)
|
|
|
|
mavproxy = util.start_MAVProxy_SITL('APMrover2', options=options)
|
2015-03-21 11:29:28 -03:00
|
|
|
|
|
|
|
print("WAITING FOR PARAMETERS")
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.expect('Received [0-9]+ parameters')
|
|
|
|
|
|
|
|
# setup test parameters
|
2016-08-03 17:48:36 -03:00
|
|
|
mavproxy.send("param load %s/default_params/rover.parm\n" % testdir)
|
2012-11-27 19:43:11 -04:00
|
|
|
mavproxy.expect('Loaded [0-9]+ parameters')
|
2016-05-27 07:54:12 -03:00
|
|
|
mavproxy.send("param set LOG_REPLAY 1\n")
|
|
|
|
mavproxy.send("param set LOG_DISARMED 1\n")
|
|
|
|
time.sleep(3)
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
# restart with new parms
|
|
|
|
util.pexpect_close(mavproxy)
|
2016-07-31 07:22:06 -03:00
|
|
|
util.pexpect_close(sitl)
|
2012-11-27 19:43:11 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
sitl = util.start_SITL(binary, model='rover', home=home, speedup=10, valgrind=valgrind, gdb=gdb)
|
|
|
|
mavproxy = util.start_MAVProxy_SITL('APMrover2', options=options)
|
2015-06-30 01:06:17 -03:00
|
|
|
mavproxy.expect('Telemetry log: (\S+)')
|
2015-03-21 11:29:28 -03:00
|
|
|
logfile = mavproxy.match.group(1)
|
|
|
|
print("LOGFILE %s" % logfile)
|
|
|
|
|
2013-04-28 02:01:31 -03:00
|
|
|
buildlog = util.reltopdir("../buildlogs/APMrover2-test.tlog")
|
2012-11-27 19:43:11 -04:00
|
|
|
print("buildlog=%s" % buildlog)
|
|
|
|
if os.path.exists(buildlog):
|
|
|
|
os.unlink(buildlog)
|
2013-06-24 00:06:11 -03:00
|
|
|
try:
|
|
|
|
os.link(logfile, buildlog)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
mavproxy.expect('Received [0-9]+ parameters')
|
|
|
|
|
|
|
|
util.expect_setup_callback(mavproxy, expect_callback)
|
|
|
|
|
|
|
|
expect_list_clear()
|
2016-07-31 07:22:06 -03:00
|
|
|
expect_list_extend([sitl, mavproxy])
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
print("Started simulator")
|
|
|
|
|
|
|
|
# get a mavlink connection going
|
|
|
|
try:
|
|
|
|
mav = mavutil.mavlink_connection('127.0.0.1:19550', robust_parsing=True)
|
2016-07-31 07:22:06 -03:00
|
|
|
except Exception as msg:
|
2012-11-27 19:43:11 -04:00
|
|
|
print("Failed to start mavlink connection on 127.0.0.1:19550" % msg)
|
|
|
|
raise
|
|
|
|
mav.message_hooks.append(message_hook)
|
|
|
|
mav.idle_hooks.append(idle_hook)
|
|
|
|
|
|
|
|
failed = False
|
|
|
|
e = 'None'
|
|
|
|
try:
|
|
|
|
print("Waiting for a heartbeat with mavlink protocol %s" % mav.WIRE_PROTOCOL_VERSION)
|
|
|
|
mav.wait_heartbeat()
|
|
|
|
print("Setting up RC parameters")
|
|
|
|
setup_rc(mavproxy)
|
|
|
|
print("Waiting for GPS fix")
|
|
|
|
mav.wait_gps_fix()
|
|
|
|
homeloc = mav.location()
|
|
|
|
print("Home location: %s" % homeloc)
|
2015-11-10 22:15:48 -04:00
|
|
|
if not arm_rover(mavproxy, mav):
|
|
|
|
print("Failed to ARM")
|
|
|
|
failed = True
|
2012-11-27 19:43:11 -04:00
|
|
|
if not drive_mission(mavproxy, mav, os.path.join(testdir, "rover1.txt")):
|
|
|
|
print("Failed mission")
|
|
|
|
failed = True
|
2013-12-28 06:47:12 -04:00
|
|
|
if not log_download(mavproxy, mav, util.reltopdir("../buildlogs/APMrover2-log.bin")):
|
|
|
|
print("Failed log download")
|
|
|
|
failed = True
|
2013-05-16 21:37:11 -03:00
|
|
|
# if not drive_left_circuit(mavproxy, mav):
|
|
|
|
# print("Failed left circuit")
|
|
|
|
# failed = True
|
|
|
|
# if not drive_RTL(mavproxy, mav):
|
|
|
|
# print("Failed RTL")
|
|
|
|
# failed = True
|
2016-07-31 07:22:06 -03:00
|
|
|
except pexpect.TIMEOUT as e:
|
2012-11-27 19:43:11 -04:00
|
|
|
print("Failed with timeout")
|
|
|
|
failed = True
|
|
|
|
|
|
|
|
mav.close()
|
|
|
|
util.pexpect_close(mavproxy)
|
2016-07-31 07:22:06 -03:00
|
|
|
util.pexpect_close(sitl)
|
2012-11-27 19:43:11 -04:00
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
valgrind_log = util.valgrind_log_filepath(binary=binary, model='rover')
|
2016-05-27 03:45:25 -03:00
|
|
|
if os.path.exists(valgrind_log):
|
2016-11-08 07:06:05 -04:00
|
|
|
os.chmod(valgrind_log, 0o644)
|
2016-05-27 03:45:25 -03:00
|
|
|
shutil.copy(valgrind_log, util.reltopdir("../buildlogs/APMrover2-valgrind.log"))
|
2012-11-27 19:43:11 -04:00
|
|
|
|
|
|
|
if failed:
|
|
|
|
print("FAILED: %s" % e)
|
|
|
|
return False
|
|
|
|
return True
|