2016-11-08 07:06:05 -04:00
|
|
|
from __future__ import print_function
|
2018-07-26 22:32:57 -03:00
|
|
|
|
|
|
|
import atexit
|
2012-03-22 08:50:47 -03:00
|
|
|
import math
|
2016-07-31 07:22:06 -03:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import re
|
2018-09-30 20:04:57 -03:00
|
|
|
import shlex
|
2019-09-22 23:50:23 -03:00
|
|
|
import signal
|
2018-07-26 22:32:57 -03:00
|
|
|
import subprocess
|
2016-07-31 07:22:06 -03:00
|
|
|
import sys
|
2018-05-17 00:29:41 -03:00
|
|
|
import tempfile
|
2016-07-31 07:22:06 -03:00
|
|
|
import time
|
|
|
|
from math import acos, atan2, cos, pi, sqrt
|
|
|
|
|
|
|
|
import pexpect
|
|
|
|
|
2016-11-08 07:06:05 -04:00
|
|
|
from . rotmat import Matrix3, Vector3
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2016-11-08 07:06:05 -04:00
|
|
|
if (sys.version_info[0] >= 3):
|
|
|
|
ENCODING = 'ascii'
|
|
|
|
else:
|
|
|
|
ENCODING = None
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2018-07-31 06:50:02 -03:00
|
|
|
RADIUS_OF_EARTH = 6378100.0 # in meters
|
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def m2ft(x):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Meters to feet."""
|
2011-12-02 00:14:15 -04:00
|
|
|
return float(x) / 0.3048
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def ft2m(x):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Feet to meters."""
|
2011-12-02 00:14:15 -04:00
|
|
|
return float(x) * 0.3048
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def kt2mps(x):
|
|
|
|
return x * 0.514444444
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def mps2kt(x):
|
|
|
|
return x / 0.514444444
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def topdir():
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return top of git tree where autotest is running from."""
|
2011-11-01 08:31:01 -03:00
|
|
|
d = os.path.dirname(os.path.realpath(__file__))
|
2016-07-31 07:22:06 -03:00
|
|
|
assert(os.path.basename(d) == 'pysim')
|
2011-12-02 00:14:15 -04:00
|
|
|
d = os.path.dirname(d)
|
2016-07-31 07:22:06 -03:00
|
|
|
assert(os.path.basename(d) == 'autotest')
|
2011-11-01 08:31:01 -03:00
|
|
|
d = os.path.dirname(d)
|
2016-07-31 07:22:06 -03:00
|
|
|
assert(os.path.basename(d) == 'Tools')
|
2011-11-01 08:31:01 -03:00
|
|
|
d = os.path.dirname(d)
|
|
|
|
return d
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def reltopdir(path):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return a path relative to topdir()."""
|
2011-11-01 08:31:01 -03:00
|
|
|
return os.path.normpath(os.path.join(topdir(), path))
|
2011-10-30 23:50:34 -03:00
|
|
|
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
def run_cmd(cmd, directory=".", show=True, output=False, checkfail=True):
|
|
|
|
"""Run a shell command."""
|
2016-05-21 12:19:50 -03:00
|
|
|
shell = False
|
|
|
|
if not isinstance(cmd, list):
|
|
|
|
cmd = [cmd]
|
|
|
|
shell = True
|
2011-10-30 23:50:34 -03:00
|
|
|
if show:
|
2016-07-31 07:22:06 -03:00
|
|
|
print("Running: (%s) in (%s)" % (cmd_as_shell(cmd), directory,))
|
2011-10-30 23:50:34 -03:00
|
|
|
if output:
|
2019-09-21 03:04:51 -03:00
|
|
|
return subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, cwd=directory).communicate()[0]
|
2011-10-30 23:50:34 -03:00
|
|
|
elif checkfail:
|
2019-09-21 03:04:51 -03:00
|
|
|
return subprocess.check_call(cmd, shell=shell, cwd=directory)
|
2011-10-30 23:50:34 -03:00
|
|
|
else:
|
2019-09-21 03:04:51 -03:00
|
|
|
return subprocess.call(cmd, shell=shell, cwd=directory)
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-10-30 23:50:34 -03:00
|
|
|
|
|
|
|
def rmfile(path):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Remove a file if it exists."""
|
2011-10-30 23:50:34 -03:00
|
|
|
try:
|
2011-11-07 07:55:21 -04:00
|
|
|
os.unlink(path)
|
2011-10-30 23:50:34 -03:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-10-30 23:50:34 -03:00
|
|
|
def deltree(path):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Delete a tree of files."""
|
2011-10-30 23:50:34 -03:00
|
|
|
run_cmd('rm -rf %s' % path)
|
2011-11-12 19:12:57 -04:00
|
|
|
|
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
def relwaf():
|
|
|
|
return "./modules/waf/waf-light"
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2018-09-30 20:04:57 -03:00
|
|
|
def waf_configure(board, j=None, debug=False, extra_args=[]):
|
2016-06-01 04:11:44 -03:00
|
|
|
cmd_configure = [relwaf(), "configure", "--board", board]
|
2016-05-21 12:19:50 -03:00
|
|
|
if debug:
|
|
|
|
cmd_configure.append('--debug')
|
|
|
|
if j is not None:
|
|
|
|
cmd_configure.extend(['-j', str(j)])
|
2018-09-30 20:04:57 -03:00
|
|
|
pieces = [shlex.split(x) for x in extra_args]
|
|
|
|
for piece in pieces:
|
|
|
|
cmd_configure.extend(piece)
|
2016-07-31 07:22:06 -03:00
|
|
|
run_cmd(cmd_configure, directory=topdir(), checkfail=True)
|
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
|
2016-06-01 04:11:44 -03:00
|
|
|
def waf_clean():
|
2016-07-31 07:22:06 -03:00
|
|
|
run_cmd([relwaf(), "clean"], directory=topdir(), checkfail=True)
|
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
|
2018-09-30 20:04:57 -03:00
|
|
|
def build_SITL(build_target, j=None, debug=False, board='sitl', clean=True, configure=True, extra_configure_args=[]):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Build desktop SITL."""
|
2016-06-01 04:11:44 -03:00
|
|
|
|
|
|
|
# first configure
|
2017-09-08 01:17:22 -03:00
|
|
|
if configure:
|
2018-09-30 20:04:57 -03:00
|
|
|
waf_configure(board, j=j, debug=debug, extra_args=extra_configure_args)
|
2016-06-01 04:11:44 -03:00
|
|
|
|
|
|
|
# then clean
|
2017-09-08 01:17:22 -03:00
|
|
|
if clean:
|
|
|
|
waf_clean()
|
2016-06-01 04:11:44 -03:00
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
# then build
|
|
|
|
cmd_make = [relwaf(), "build", "--target", build_target]
|
2016-06-04 20:45:58 -03:00
|
|
|
if j is not None:
|
|
|
|
cmd_make.extend(['-j', str(j)])
|
2016-07-31 07:22:06 -03:00
|
|
|
run_cmd(cmd_make, directory=topdir(), checkfail=True, show=True)
|
2011-11-09 01:15:53 -04:00
|
|
|
return True
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2016-06-01 04:11:44 -03:00
|
|
|
def build_examples(board, j=None, debug=False, clean=False):
|
|
|
|
# first configure
|
|
|
|
waf_configure(board, j=j, debug=debug)
|
|
|
|
|
|
|
|
# then clean
|
|
|
|
if clean:
|
|
|
|
waf_clean()
|
|
|
|
|
|
|
|
# then build
|
|
|
|
cmd_make = [relwaf(), "examples"]
|
2016-07-31 07:22:06 -03:00
|
|
|
run_cmd(cmd_make, directory=topdir(), checkfail=True, show=True)
|
2016-06-01 04:11:44 -03:00
|
|
|
return True
|
|
|
|
|
2018-08-14 21:33:52 -03:00
|
|
|
def build_tests(board, j=None, debug=False, clean=False):
|
|
|
|
# first configure
|
|
|
|
waf_configure(board, j=j, debug=debug)
|
|
|
|
|
|
|
|
# then clean
|
|
|
|
if clean:
|
|
|
|
waf_clean()
|
|
|
|
|
|
|
|
# then build
|
|
|
|
run_cmd([relwaf(), "tests"], directory=topdir(), checkfail=True, show=True)
|
|
|
|
return True
|
2016-06-01 04:11:44 -03:00
|
|
|
|
2011-11-09 01:15:53 -04:00
|
|
|
# list of pexpect children to close on exit
|
2011-11-09 00:45:18 -04:00
|
|
|
close_list = []
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-09 01:15:53 -04:00
|
|
|
def pexpect_autoclose(p):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Mark for autoclosing."""
|
2011-11-09 01:15:53 -04:00
|
|
|
global close_list
|
|
|
|
close_list.append(p)
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-09 00:45:18 -04:00
|
|
|
def pexpect_close(p):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Close a pexpect child."""
|
2011-11-09 00:45:18 -04:00
|
|
|
global close_list
|
2011-11-13 20:17:57 -04:00
|
|
|
|
2019-09-22 23:50:23 -03:00
|
|
|
ex = None
|
|
|
|
try:
|
|
|
|
p.kill(signal.SIGTERM)
|
|
|
|
except IOError as e:
|
|
|
|
print("Caught exception: %s" % str(e))
|
|
|
|
ex = e
|
|
|
|
pass
|
|
|
|
if ex is None:
|
|
|
|
# give the process some time to go away
|
|
|
|
for i in range(20):
|
|
|
|
if not p.isalive():
|
|
|
|
break
|
|
|
|
time.sleep(0.05)
|
2011-11-12 07:13:17 -04:00
|
|
|
try:
|
|
|
|
p.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
p.close(force=True)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2011-11-09 02:44:14 -04:00
|
|
|
if p in close_list:
|
|
|
|
close_list.remove(p)
|
2011-11-09 00:45:18 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-09 00:45:18 -04:00
|
|
|
def pexpect_close_all():
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Close all pexpect children."""
|
2011-11-09 00:45:18 -04:00
|
|
|
global close_list
|
|
|
|
for p in close_list[:]:
|
2011-11-13 20:17:57 -04:00
|
|
|
pexpect_close(p)
|
2011-11-09 00:45:18 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def pexpect_drain(p):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Drain any pending input."""
|
2013-03-25 18:12:57 -03:00
|
|
|
import pexpect
|
2011-12-02 00:14:15 -04:00
|
|
|
try:
|
|
|
|
p.read_nonblocking(1000, timeout=0)
|
2016-09-08 02:25:36 -03:00
|
|
|
except Exception:
|
2011-12-02 00:14:15 -04:00
|
|
|
pass
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
def cmd_as_shell(cmd):
|
2016-07-31 07:22:06 -03:00
|
|
|
return (" ".join(['"%s"' % x for x in cmd]))
|
|
|
|
|
2016-05-21 12:19:50 -03:00
|
|
|
|
2016-05-26 21:47:38 -03:00
|
|
|
def make_safe_filename(text):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return a version of text safe for use as a filename."""
|
2016-05-27 03:45:25 -03:00
|
|
|
r = re.compile("([^a-zA-Z0-9_.+-])")
|
2016-07-31 07:22:06 -03:00
|
|
|
text.replace('/', '-')
|
|
|
|
filename = r.sub(lambda m: "%" + str(hex(ord(str(m.group(1))))).upper(), text)
|
2016-05-26 21:47:38 -03:00
|
|
|
return filename
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
def valgrind_log_filepath(binary, model):
|
|
|
|
return make_safe_filename('%s-%s-valgrind.log' % (os.path.basename(binary), model,))
|
|
|
|
|
|
|
|
|
2018-07-26 22:32:57 -03:00
|
|
|
def kill_screen_gdb():
|
|
|
|
cmd = ["screen", "-X", "-S", "ardupilot-gdb", "quit"]
|
|
|
|
subprocess.Popen(cmd)
|
|
|
|
|
2018-07-31 06:50:02 -03:00
|
|
|
|
2018-07-23 04:06:00 -03:00
|
|
|
def start_SITL(binary,
|
|
|
|
valgrind=False,
|
|
|
|
gdb=False,
|
|
|
|
wipe=False,
|
|
|
|
synthetic_clock=True,
|
|
|
|
home=None,
|
|
|
|
model=None,
|
|
|
|
speedup=1,
|
|
|
|
defaults_file=None,
|
|
|
|
unhide_parameters=False,
|
|
|
|
gdbserver=False,
|
|
|
|
breakpoints=[],
|
2019-07-11 21:58:46 -03:00
|
|
|
disable_breakpoints=False,
|
2018-06-29 07:38:10 -03:00
|
|
|
vicon=False):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Launch a SITL instance."""
|
2016-11-08 06:53:54 -04:00
|
|
|
cmd = []
|
|
|
|
if valgrind and os.path.exists('/usr/bin/valgrind'):
|
2018-05-17 00:29:41 -03:00
|
|
|
# we specify a prefix for vgdb-pipe because on Vagrant virtual
|
|
|
|
# machines the pipes are created on the mountpoint for the
|
|
|
|
# shared directory with the host machine. mmap's,
|
|
|
|
# unsurprisingly, fail on files created on that mountpoint.
|
|
|
|
vgdb_prefix = os.path.join(tempfile.gettempdir(), "vgdb-pipe")
|
|
|
|
log_file = valgrind_log_filepath(binary=binary, model=model)
|
|
|
|
cmd.extend([
|
|
|
|
'valgrind',
|
2018-12-04 18:18:03 -04:00
|
|
|
# adding this option allows valgrind to cope with the overload
|
|
|
|
# of operator new
|
|
|
|
"--soname-synonyms=somalloc=nouserintercepts",
|
2018-05-17 00:29:41 -03:00
|
|
|
'--vgdb-prefix=%s' % vgdb_prefix,
|
|
|
|
'-q',
|
|
|
|
'--log-file=%s' % log_file])
|
2017-08-24 08:35:53 -03:00
|
|
|
if gdbserver:
|
|
|
|
cmd.extend(['gdbserver', 'localhost:3333'])
|
|
|
|
if gdb:
|
|
|
|
# attach gdb to the gdbserver:
|
|
|
|
f = open("/tmp/x.gdb", "w")
|
|
|
|
f.write("target extended-remote localhost:3333\nc\n")
|
2018-07-23 04:06:00 -03:00
|
|
|
for breakpoint in breakpoints:
|
|
|
|
f.write("b %s\n" % (breakpoint,))
|
2019-07-11 21:58:46 -03:00
|
|
|
if disable_breakpoints:
|
|
|
|
f.write("disable\n")
|
2017-08-24 08:35:53 -03:00
|
|
|
f.close()
|
2018-07-26 22:32:57 -03:00
|
|
|
run_cmd('screen -d -m -S ardupilot-gdbserver '
|
|
|
|
'bash -c "gdb -x /tmp/x.gdb"')
|
2017-08-24 08:35:53 -03:00
|
|
|
elif gdb:
|
2016-11-08 06:53:54 -04:00
|
|
|
f = open("/tmp/x.gdb", "w")
|
2018-07-23 04:06:00 -03:00
|
|
|
for breakpoint in breakpoints:
|
|
|
|
f.write("b %s\n" % (breakpoint,))
|
2019-07-11 21:58:46 -03:00
|
|
|
if disable_breakpoints:
|
|
|
|
f.write("disable\n")
|
2016-11-08 06:53:54 -04:00
|
|
|
f.write("r\n")
|
|
|
|
f.close()
|
2018-07-26 22:32:57 -03:00
|
|
|
if os.environ.get('DISPLAY'):
|
|
|
|
cmd.extend(['xterm', '-e', 'gdb', '-x', '/tmp/x.gdb', '--args'])
|
|
|
|
else:
|
|
|
|
cmd.extend(['screen',
|
|
|
|
'-L', '-Logfile', 'gdb.log',
|
|
|
|
'-d',
|
|
|
|
'-m',
|
|
|
|
'-S', 'ardupilot-gdb',
|
|
|
|
'gdb', '-x', '/tmp/x.gdb', binary, '--args'])
|
2017-07-07 01:05:04 -03:00
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
cmd.append(binary)
|
|
|
|
if wipe:
|
|
|
|
cmd.append('-w')
|
|
|
|
if synthetic_clock:
|
|
|
|
cmd.append('-S')
|
|
|
|
if home is not None:
|
|
|
|
cmd.extend(['--home', home])
|
|
|
|
if model is not None:
|
|
|
|
cmd.extend(['--model', model])
|
|
|
|
if speedup != 1:
|
|
|
|
cmd.extend(['--speedup', str(speedup)])
|
|
|
|
if defaults_file is not None:
|
|
|
|
cmd.extend(['--defaults', defaults_file])
|
|
|
|
if unhide_parameters:
|
|
|
|
cmd.extend(['--unhide-groups'])
|
2018-06-29 07:38:10 -03:00
|
|
|
if vicon:
|
|
|
|
cmd.extend(["--uartF=sim:vicon:"])
|
2018-07-26 22:32:57 -03:00
|
|
|
|
|
|
|
if gdb and not os.getenv('DISPLAY'):
|
2019-09-21 03:04:51 -03:00
|
|
|
subprocess.Popen(cmd)
|
2018-07-26 22:32:57 -03:00
|
|
|
atexit.register(kill_screen_gdb)
|
|
|
|
# we are expected to return a pexpect wrapped around the
|
|
|
|
# stdout of the ArduPilot binary. Not going to happen until
|
|
|
|
# AP gets a redirect-stdout-to-filehandle option. So, in the
|
|
|
|
# meantime, return a dummy:
|
|
|
|
return pexpect.spawn("true", ["true"],
|
|
|
|
logfile=sys.stdout,
|
|
|
|
encoding=ENCODING,
|
|
|
|
timeout=5)
|
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
print("Running: %s" % cmd_as_shell(cmd))
|
2018-07-26 22:32:57 -03:00
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
first = cmd[0]
|
|
|
|
rest = cmd[1:]
|
2016-11-08 07:06:05 -04:00
|
|
|
child = pexpect.spawn(first, rest, logfile=sys.stdout, encoding=ENCODING, timeout=5)
|
2016-11-08 06:53:54 -04:00
|
|
|
pexpect_autoclose(child)
|
|
|
|
# give time for parameters to properly setup
|
|
|
|
time.sleep(3)
|
|
|
|
if gdb:
|
|
|
|
# if we run GDB we do so in an xterm. "Waiting for
|
|
|
|
# connection" is never going to appear on xterm's output.
|
|
|
|
# ... so let's give it another magic second.
|
|
|
|
time.sleep(1)
|
|
|
|
# TODO: have a SITL-compiled ardupilot able to have its
|
|
|
|
# console on an output fd.
|
|
|
|
else:
|
2016-11-08 07:06:05 -04:00
|
|
|
child.expect('Waiting for connection', timeout=300)
|
2016-11-08 06:53:54 -04:00
|
|
|
return child
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
|
|
|
|
def start_MAVProxy_SITL(atype, aircraft=None, setup=False, master='tcp:127.0.0.1:5760',
|
2018-03-15 08:31:19 -03:00
|
|
|
options=[], logfile=sys.stdout):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Launch mavproxy connected to a SITL instance."""
|
2013-03-25 18:12:57 -03:00
|
|
|
import pexpect
|
2011-11-09 00:45:18 -04:00
|
|
|
global close_list
|
2013-09-17 08:05:00 -03:00
|
|
|
MAVPROXY = os.getenv('MAVPROXY_CMD', 'mavproxy.py')
|
2011-12-13 06:34:53 -04:00
|
|
|
cmd = MAVPROXY + ' --master=%s --out=127.0.0.1:14550' % master
|
2011-11-07 07:55:21 -04:00
|
|
|
if setup:
|
|
|
|
cmd += ' --setup'
|
|
|
|
if aircraft is None:
|
|
|
|
aircraft = 'test.%s' % atype
|
|
|
|
cmd += ' --aircraft=%s' % aircraft
|
2018-03-15 08:31:19 -03:00
|
|
|
cmd += ' ' + ' '.join(options)
|
2019-07-15 06:37:16 -03:00
|
|
|
cmd += ' --default-modules misc,terrain,wp,rally,fence,param,arm,mode,rc,cmdlong,output'
|
2016-11-08 07:06:05 -04:00
|
|
|
ret = pexpect.spawn(cmd, logfile=logfile, encoding=ENCODING, timeout=60)
|
2011-12-02 00:14:15 -04:00
|
|
|
ret.delaybeforesend = 0
|
2011-11-09 01:15:53 -04:00
|
|
|
pexpect_autoclose(ret)
|
2011-10-30 23:50:34 -03:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def expect_setup_callback(e, callback):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Setup a callback that is called once a second while waiting for
|
|
|
|
patterns."""
|
2013-03-25 18:12:57 -03:00
|
|
|
import pexpect
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def _expect_callback(pattern, timeout=e.timeout):
|
|
|
|
tstart = time.time()
|
|
|
|
while time.time() < tstart + timeout:
|
|
|
|
try:
|
|
|
|
ret = e.expect_saved(pattern, timeout=1)
|
|
|
|
return ret
|
|
|
|
except pexpect.TIMEOUT:
|
|
|
|
e.expect_user_callback(e)
|
2011-11-09 05:27:15 -04:00
|
|
|
print("Timed out looking for %s" % pattern)
|
|
|
|
raise pexpect.TIMEOUT(timeout)
|
2011-11-01 08:31:01 -03:00
|
|
|
|
|
|
|
e.expect_user_callback = callback
|
|
|
|
e.expect_saved = e.expect
|
|
|
|
e.expect = _expect_callback
|
2011-11-07 05:12:51 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
def mkdir_p(directory):
|
|
|
|
"""Like mkdir -p ."""
|
|
|
|
if not directory:
|
2011-11-07 05:12:51 -04:00
|
|
|
return
|
2016-07-31 07:22:06 -03:00
|
|
|
if directory.endswith("/"):
|
|
|
|
mkdir_p(directory[:-1])
|
2011-11-07 05:12:51 -04:00
|
|
|
return
|
2016-07-31 07:22:06 -03:00
|
|
|
if os.path.isdir(directory):
|
2011-11-07 05:12:51 -04:00
|
|
|
return
|
2016-07-31 07:22:06 -03:00
|
|
|
mkdir_p(os.path.dirname(directory))
|
|
|
|
os.mkdir(directory)
|
|
|
|
|
2011-11-09 08:43:25 -04:00
|
|
|
|
|
|
|
def loadfile(fname):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Load a file as a string."""
|
2011-11-09 08:43:25 -04:00
|
|
|
f = open(fname, mode='r')
|
|
|
|
r = f.read()
|
|
|
|
f.close()
|
|
|
|
return r
|
2011-11-09 08:53:09 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-11-09 08:53:09 -04:00
|
|
|
def lock_file(fname):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Lock a file."""
|
2011-11-09 08:53:09 -04:00
|
|
|
import fcntl
|
|
|
|
f = open(fname, mode='w')
|
|
|
|
try:
|
|
|
|
fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
return f
|
2011-11-13 20:17:57 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2013-03-25 18:20:40 -03:00
|
|
|
def check_parent(parent_pid=None):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Check our parent process is still alive."""
|
2013-03-25 18:20:40 -03:00
|
|
|
if parent_pid is None:
|
|
|
|
try:
|
|
|
|
parent_pid = os.getppid()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
if parent_pid is None:
|
|
|
|
return
|
2011-11-13 20:17:57 -04:00
|
|
|
try:
|
2011-12-02 00:14:15 -04:00
|
|
|
os.kill(parent_pid, 0)
|
|
|
|
except Exception:
|
|
|
|
print("Parent had finished - exiting")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
def EarthRatesToBodyRates(dcm, earth_rates):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Convert the angular velocities from earth frame to
|
2011-12-05 02:44:04 -04:00
|
|
|
body frame. Thanks to James Goppert for the formula
|
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
all inputs and outputs are in radians
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
returns a gyro vector in body frame, in rad/s .
|
|
|
|
"""
|
2012-03-22 08:50:47 -03:00
|
|
|
from math import sin, cos
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
(phi, theta, psi) = dcm.to_euler()
|
|
|
|
phiDot = earth_rates.x
|
|
|
|
thetaDot = earth_rates.y
|
|
|
|
psiDot = earth_rates.z
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
p = phiDot - psiDot * sin(theta)
|
|
|
|
q = cos(phi) * thetaDot + sin(phi) * psiDot * cos(theta)
|
|
|
|
r = cos(phi) * psiDot * cos(theta) - sin(phi) * thetaDot
|
2012-03-22 08:50:47 -03:00
|
|
|
return Vector3(p, q, r)
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
def BodyRatesToEarthRates(dcm, gyro):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Convert the angular velocities from body frame to
|
2011-12-05 02:44:04 -04:00
|
|
|
earth frame.
|
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
all inputs and outputs are in radians/s
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
returns a earth rate vector.
|
|
|
|
"""
|
2012-03-22 08:50:47 -03:00
|
|
|
from math import sin, cos, tan, fabs
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
p = gyro.x
|
|
|
|
q = gyro.y
|
|
|
|
r = gyro.z
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
(phi, theta, psi) = dcm.to_euler()
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
phiDot = p + tan(theta) * (q * sin(phi) + r * cos(phi))
|
|
|
|
thetaDot = q * cos(phi) - r * sin(phi)
|
2011-12-05 02:44:04 -04:00
|
|
|
if fabs(cos(theta)) < 1.0e-20:
|
|
|
|
theta += 1.0e-10
|
2016-07-31 07:22:06 -03:00
|
|
|
psiDot = (q * sin(phi) + r * cos(phi)) / cos(theta)
|
2012-03-22 08:50:47 -03:00
|
|
|
return Vector3(phiDot, thetaDot, psiDot)
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2015-04-17 20:26:30 -03:00
|
|
|
|
2012-06-04 22:18:45 -03:00
|
|
|
def gps_newpos(lat, lon, bearing, distance):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Extrapolate latitude/longitude given a heading and distance
|
|
|
|
thanks to http://www.movable-type.co.uk/scripts/latlong.html .
|
|
|
|
"""
|
2012-06-04 22:18:45 -03:00
|
|
|
from math import sin, asin, cos, atan2, radians, degrees
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-06-04 22:18:45 -03:00
|
|
|
lat1 = radians(lat)
|
|
|
|
lon1 = radians(lon)
|
|
|
|
brng = radians(bearing)
|
2018-07-31 06:50:02 -03:00
|
|
|
dr = distance / RADIUS_OF_EARTH
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
lat2 = asin(sin(lat1) * cos(dr) +
|
|
|
|
cos(lat1) * sin(dr) * cos(brng))
|
|
|
|
lon2 = lon1 + atan2(sin(brng) * sin(dr) * cos(lat1),
|
|
|
|
cos(dr) - sin(lat1) * sin(lat2))
|
2012-06-04 22:18:45 -03:00
|
|
|
return (degrees(lat2), degrees(lon2))
|
|
|
|
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2015-04-17 20:26:30 -03:00
|
|
|
def gps_distance(lat1, lon1, lat2, lon2):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return distance between two points in meters,
|
2015-04-17 20:26:30 -03:00
|
|
|
coordinates are in degrees
|
2016-07-31 07:22:06 -03:00
|
|
|
thanks to http://www.movable-type.co.uk/scripts/latlong.html ."""
|
2015-04-17 20:26:30 -03:00
|
|
|
lat1 = math.radians(lat1)
|
|
|
|
lat2 = math.radians(lat2)
|
|
|
|
lon1 = math.radians(lon1)
|
|
|
|
lon2 = math.radians(lon2)
|
|
|
|
dLat = lat2 - lat1
|
|
|
|
dLon = lon2 - lon1
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
a = math.sin(0.5 * dLat)**2 + math.sin(0.5 * dLon)**2 * math.cos(lat1) * math.cos(lat2)
|
|
|
|
c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a))
|
2018-07-31 06:50:02 -03:00
|
|
|
return RADIUS_OF_EARTH * c
|
2015-04-17 20:26:30 -03:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2015-04-17 20:26:30 -03:00
|
|
|
def gps_bearing(lat1, lon1, lat2, lon2):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return bearing between two points in degrees, in range 0-360
|
|
|
|
thanks to http://www.movable-type.co.uk/scripts/latlong.html ."""
|
2015-04-17 20:26:30 -03:00
|
|
|
lat1 = math.radians(lat1)
|
|
|
|
lat2 = math.radians(lat2)
|
|
|
|
lon1 = math.radians(lon1)
|
|
|
|
lon2 = math.radians(lon2)
|
|
|
|
dLon = lon2 - lon1
|
|
|
|
y = math.sin(dLon) * math.cos(lat2)
|
2016-07-31 07:22:06 -03:00
|
|
|
x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon)
|
2015-04-17 20:26:30 -03:00
|
|
|
bearing = math.degrees(math.atan2(y, x))
|
|
|
|
if bearing < 0:
|
|
|
|
bearing += 360.0
|
|
|
|
return bearing
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-12 19:10:30 -04:00
|
|
|
class Wind(object):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""A wind generation object."""
|
2011-12-12 19:10:30 -04:00
|
|
|
def __init__(self, windstring, cross_section=0.1):
|
|
|
|
a = windstring.split(',')
|
|
|
|
if len(a) != 3:
|
|
|
|
raise RuntimeError("Expected wind in speed,direction,turbulance form, not %s" % windstring)
|
2016-07-31 07:22:06 -03:00
|
|
|
self.speed = float(a[0]) # m/s
|
|
|
|
self.direction = float(a[1]) # direction the wind is going in
|
|
|
|
self.turbulance = float(a[2]) # turbulance factor (standard deviation)
|
2011-12-12 19:10:30 -04:00
|
|
|
|
|
|
|
# the cross-section of the aircraft to wind. This is multiplied by the
|
|
|
|
# difference in the wind and the velocity of the aircraft to give the acceleration
|
|
|
|
self.cross_section = cross_section
|
|
|
|
|
|
|
|
# the time constant for the turbulance - the average period of the
|
|
|
|
# changes over time
|
|
|
|
self.turbulance_time_constant = 5.0
|
|
|
|
|
|
|
|
# wind time record
|
|
|
|
self.tlast = time.time()
|
|
|
|
|
|
|
|
# initial turbulance multiplier
|
|
|
|
self.turbulance_mul = 1.0
|
|
|
|
|
2011-12-13 00:28:03 -04:00
|
|
|
def current(self, deltat=None):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return current wind speed and direction as a tuple
|
|
|
|
speed is in m/s, direction in degrees."""
|
2011-12-12 19:10:30 -04:00
|
|
|
if deltat is None:
|
|
|
|
tnow = time.time()
|
|
|
|
deltat = tnow - self.tlast
|
|
|
|
self.tlast = tnow
|
|
|
|
|
|
|
|
# update turbulance random walk
|
2016-07-31 07:22:06 -03:00
|
|
|
w_delta = math.sqrt(deltat) * (1.0 - random.gauss(1.0, self.turbulance))
|
|
|
|
w_delta -= (self.turbulance_mul - 1.0) * (deltat / self.turbulance_time_constant)
|
2011-12-12 19:10:30 -04:00
|
|
|
self.turbulance_mul += w_delta
|
2011-12-29 02:05:53 -04:00
|
|
|
speed = self.speed * math.fabs(self.turbulance_mul)
|
2011-12-13 00:28:03 -04:00
|
|
|
return (speed, self.direction)
|
|
|
|
|
2012-10-30 14:36:19 -03:00
|
|
|
# Calculate drag.
|
2018-07-31 06:50:02 -03:00
|
|
|
def drag(self, velocity, deltat=None):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Return current wind force in Earth frame. The velocity parameter is
|
|
|
|
a Vector3 of the current velocity of the aircraft in earth frame, m/s ."""
|
2012-03-22 08:50:47 -03:00
|
|
|
from math import radians
|
2011-12-13 00:28:03 -04:00
|
|
|
|
2012-10-30 14:36:19 -03:00
|
|
|
# (m/s, degrees) : wind vector as a magnitude and angle.
|
2011-12-13 00:28:03 -04:00
|
|
|
(speed, direction) = self.current(deltat=deltat)
|
2012-10-30 14:36:19 -03:00
|
|
|
# speed = self.speed
|
|
|
|
# direction = self.direction
|
|
|
|
|
|
|
|
# Get the wind vector.
|
|
|
|
w = toVec(speed, radians(direction))
|
|
|
|
|
|
|
|
obj_speed = velocity.length()
|
|
|
|
|
|
|
|
# Compute the angle between the object vector and wind vector by taking
|
|
|
|
# the dot product and dividing by the magnitudes.
|
|
|
|
d = w.length() * obj_speed
|
2016-07-31 07:22:06 -03:00
|
|
|
if d == 0:
|
2012-10-30 14:36:19 -03:00
|
|
|
alpha = 0
|
2016-07-31 07:22:06 -03:00
|
|
|
else:
|
2012-10-30 14:36:19 -03:00
|
|
|
alpha = acos((w * velocity) / d)
|
|
|
|
|
|
|
|
# Get the relative wind speed and angle from the object. Note that the
|
|
|
|
# relative wind speed includes the velocity of the object; i.e., there
|
|
|
|
# is a headwind equivalent to the object's speed even if there is no
|
|
|
|
# absolute wind.
|
|
|
|
(rel_speed, beta) = apparent_wind(speed, obj_speed, alpha)
|
|
|
|
|
|
|
|
# Return the vector of the relative wind, relative to the coordinate
|
|
|
|
# system.
|
|
|
|
relWindVec = toVec(rel_speed, beta + atan2(velocity.y, velocity.x))
|
|
|
|
|
|
|
|
# Combine them to get the acceleration vector.
|
2016-07-31 07:22:06 -03:00
|
|
|
return Vector3(acc(relWindVec.x, drag_force(self, relWindVec.x)), acc(relWindVec.y, drag_force(self, relWindVec.y)), 0)
|
|
|
|
|
|
|
|
|
2012-10-30 14:36:19 -03:00
|
|
|
def apparent_wind(wind_sp, obj_speed, alpha):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""http://en.wikipedia.org/wiki/Apparent_wind
|
|
|
|
|
|
|
|
Returns apparent wind speed and angle of apparent wind. Alpha is the angle
|
|
|
|
between the object and the true wind. alpha of 0 rads is a headwind; pi a
|
|
|
|
tailwind. Speeds should always be positive."""
|
2012-10-30 14:36:19 -03:00
|
|
|
delta = wind_sp * cos(alpha)
|
|
|
|
x = wind_sp**2 + obj_speed**2 + 2 * obj_speed * delta
|
|
|
|
rel_speed = sqrt(x)
|
|
|
|
if rel_speed == 0:
|
|
|
|
beta = pi
|
|
|
|
else:
|
|
|
|
beta = acos((delta + obj_speed) / rel_speed)
|
|
|
|
|
|
|
|
return (rel_speed, beta)
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
def drag_force(wind, sp):
|
|
|
|
"""See http://en.wikipedia.org/wiki/Drag_equation
|
|
|
|
|
|
|
|
Drag equation is F(a) = cl * p/2 * v^2 * a, where cl : drag coefficient
|
|
|
|
(let's assume it's low, .e.g., 0.2), p : density of air (assume about 1
|
|
|
|
kg/m^3, the density just over 1500m elevation), v : relative speed of wind
|
|
|
|
(to the body), a : area acted on (this is captured by the cross_section
|
|
|
|
parameter).
|
|
|
|
|
|
|
|
So then we have
|
|
|
|
F(a) = 0.2 * 1/2 * v^2 * cross_section = 0.1 * v^2 * cross_section."""
|
2012-10-30 14:36:19 -03:00
|
|
|
return (sp**2.0) * 0.1 * wind.cross_section
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-10-30 14:36:19 -03:00
|
|
|
def acc(val, mag):
|
2016-07-31 07:22:06 -03:00
|
|
|
""" Function to make the force vector. relWindVec is the direction the apparent
|
|
|
|
wind comes *from*. We want to compute the accleration vector in the direction
|
|
|
|
the wind blows to."""
|
2012-10-30 14:36:19 -03:00
|
|
|
if val == 0:
|
|
|
|
return mag
|
|
|
|
else:
|
|
|
|
return (val / abs(val)) * (0 - mag)
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2012-10-30 14:36:19 -03:00
|
|
|
def toVec(magnitude, angle):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Converts a magnitude and angle (radians) to a vector in the xy plane."""
|
2012-10-30 14:36:19 -03:00
|
|
|
v = Vector3(magnitude, 0, 0)
|
|
|
|
m = Matrix3()
|
|
|
|
m.from_euler(0, 0, angle)
|
|
|
|
return m.transposed() * v
|
2011-12-12 19:10:30 -04:00
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2015-04-17 20:24:15 -03:00
|
|
|
def constrain(value, minv, maxv):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Constrain a value to a range."""
|
2015-04-17 20:24:15 -03:00
|
|
|
if value < minv:
|
|
|
|
value = minv
|
|
|
|
if value > maxv:
|
|
|
|
value = maxv
|
|
|
|
return value
|
2011-12-12 19:10:30 -04:00
|
|
|
|
2018-07-31 06:50:02 -03:00
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|