2016-11-08 07:06:05 -04:00
|
|
|
from __future__ import print_function
|
2018-07-26 22:32:57 -03:00
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
'''
|
|
|
|
AP_FLAKE8_CLEAN
|
|
|
|
'''
|
|
|
|
|
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 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
|
|
|
|
|
|
|
|
|
2023-03-13 19:30:03 -03:00
|
|
|
import pexpect
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2023-02-03 17:30:14 -04:00
|
|
|
if sys.version_info[0] >= 3:
|
2016-11-08 07:06:05 -04:00
|
|
|
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
|
|
|
|
|
2020-05-13 14:06:54 -03:00
|
|
|
# List of open terminal windows for macosx
|
|
|
|
windowID = []
|
|
|
|
|
2022-08-22 05:36:21 -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__))
|
2023-02-03 17:30:14 -04:00
|
|
|
assert os.path.basename(d) == 'pysim'
|
2011-12-02 00:14:15 -04:00
|
|
|
d = os.path.dirname(d)
|
2023-02-03 17:30:14 -04:00
|
|
|
assert os.path.basename(d) == 'autotest'
|
2011-11-01 08:31:01 -03:00
|
|
|
d = os.path.dirname(d)
|
2023-02-03 17:30:14 -04: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
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2022-02-01 20:27:03 -04:00
|
|
|
def relcurdir(path):
|
|
|
|
"""Return a path relative to current dir"""
|
|
|
|
return os.path.relpath(path, os.getcwd())
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def reltopdir(path):
|
2022-02-28 11:16:52 -04:00
|
|
|
"""Returns the normalized ABSOLUTE path for 'path', where path is a path relative to topdir"""
|
2022-02-06 18:11:39 -04: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)
|
2023-02-03 17:30:14 -04:00
|
|
|
except (OSError, FileNotFoundError):
|
2011-10-30 23:50:34 -03:00
|
|
|
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
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
def waf_configure(board,
|
|
|
|
j=None,
|
|
|
|
debug=False,
|
|
|
|
math_check_indexes=False,
|
|
|
|
coverage=False,
|
|
|
|
ekf_single=False,
|
|
|
|
postype_single=False,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=False,
|
2022-08-22 05:36:21 -03:00
|
|
|
extra_args=[],
|
|
|
|
extra_hwdef=None,
|
|
|
|
ubsan=False,
|
|
|
|
ubsan_abort=False,
|
2023-03-03 19:53:50 -04:00
|
|
|
num_aux_imus=0,
|
2023-01-22 03:32:44 -04:00
|
|
|
dronecan_tests=False,
|
2022-08-22 05:36:21 -03:00
|
|
|
extra_defines={}):
|
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')
|
2021-05-24 10:48:42 -03:00
|
|
|
if coverage:
|
|
|
|
cmd_configure.append('--coverage')
|
2020-04-27 22:37:33 -03:00
|
|
|
if math_check_indexes:
|
|
|
|
cmd_configure.append('--enable-math-check-indexes')
|
2021-07-08 19:47:07 -03:00
|
|
|
if ekf_single:
|
|
|
|
cmd_configure.append('--ekf-single')
|
|
|
|
if postype_single:
|
|
|
|
cmd_configure.append('--postype-single')
|
2023-01-08 18:56:33 -04:00
|
|
|
if force_32bit:
|
|
|
|
cmd_configure.append('--force-32bit')
|
2022-07-10 20:16:55 -03:00
|
|
|
if ubsan:
|
|
|
|
cmd_configure.append('--ubsan')
|
|
|
|
if ubsan_abort:
|
|
|
|
cmd_configure.append('--ubsan-abort')
|
2023-03-03 19:53:50 -04:00
|
|
|
if num_aux_imus > 0:
|
|
|
|
cmd_configure.append('--num-aux-imus=%u' % num_aux_imus)
|
2023-01-22 03:32:44 -04:00
|
|
|
if dronecan_tests:
|
|
|
|
cmd_configure.append('--enable-dronecan-tests')
|
2022-01-09 22:49:20 -04:00
|
|
|
if extra_hwdef is not None:
|
|
|
|
cmd_configure.extend(['--extra-hwdef', extra_hwdef])
|
2022-06-26 21:40:06 -03:00
|
|
|
for nv in extra_defines.items():
|
|
|
|
cmd_configure.extend(['--define', "%s=%s" % nv])
|
2016-05-21 12:19:50 -03:00
|
|
|
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
|
|
|
|
2022-01-09 22:49:20 -04:00
|
|
|
def waf_build(target=None):
|
|
|
|
cmd = [relwaf(), "build"]
|
|
|
|
if target is not None:
|
|
|
|
cmd.append(target)
|
|
|
|
run_cmd(cmd, directory=topdir(), checkfail=True)
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2022-06-26 21:40:06 -03:00
|
|
|
def build_SITL(
|
|
|
|
build_target,
|
|
|
|
board='sitl',
|
|
|
|
clean=True,
|
|
|
|
configure=True,
|
|
|
|
coverage=False,
|
|
|
|
debug=False,
|
|
|
|
ekf_single=False,
|
|
|
|
extra_configure_args=[],
|
|
|
|
extra_defines={},
|
|
|
|
j=None,
|
|
|
|
math_check_indexes=False,
|
|
|
|
postype_single=False,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=False,
|
2022-06-26 21:40:06 -03:00
|
|
|
ubsan=False,
|
|
|
|
ubsan_abort=False,
|
2023-03-03 19:53:50 -04:00
|
|
|
num_aux_imus=0,
|
2023-01-22 03:32:44 -04:00
|
|
|
dronecan_tests=False,
|
2022-06-26 21:40:06 -03:00
|
|
|
):
|
2016-06-01 04:11:44 -03:00
|
|
|
|
|
|
|
# first configure
|
2017-09-08 01:17:22 -03:00
|
|
|
if configure:
|
2020-04-27 22:37:33 -03:00
|
|
|
waf_configure(board,
|
|
|
|
j=j,
|
|
|
|
debug=debug,
|
|
|
|
math_check_indexes=math_check_indexes,
|
2021-07-08 19:47:07 -03:00
|
|
|
ekf_single=ekf_single,
|
|
|
|
postype_single=postype_single,
|
2021-05-24 10:48:42 -03:00
|
|
|
coverage=coverage,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=force_32bit,
|
2022-07-10 20:16:55 -03:00
|
|
|
ubsan=ubsan,
|
|
|
|
ubsan_abort=ubsan_abort,
|
2022-06-26 21:40:06 -03:00
|
|
|
extra_defines=extra_defines,
|
2023-03-03 19:53:50 -04:00
|
|
|
num_aux_imus=num_aux_imus,
|
2023-01-22 03:32:44 -04:00
|
|
|
dronecan_tests=dronecan_tests,
|
2023-03-03 19:53:50 -04:00
|
|
|
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
|
|
|
|
2021-07-08 19:47:07 -03:00
|
|
|
def build_examples(board, j=None, debug=False, clean=False, configure=True, math_check_indexes=False, coverage=False,
|
2023-01-22 03:32:44 -04:00
|
|
|
ekf_single=False, postype_single=False, force_32bit=False, ubsan=False, ubsan_abort=False,
|
|
|
|
num_aux_imus=0, dronecan_tests=False,
|
2021-07-08 19:47:07 -03:00
|
|
|
extra_configure_args=[]):
|
2016-06-01 04:11:44 -03:00
|
|
|
# first configure
|
2021-05-20 11:28:22 -03:00
|
|
|
if configure:
|
|
|
|
waf_configure(board,
|
|
|
|
j=j,
|
|
|
|
debug=debug,
|
|
|
|
math_check_indexes=math_check_indexes,
|
2021-07-08 19:47:07 -03:00
|
|
|
ekf_single=ekf_single,
|
|
|
|
postype_single=postype_single,
|
2021-05-24 10:48:42 -03:00
|
|
|
coverage=coverage,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=force_32bit,
|
2022-07-10 20:16:55 -03:00
|
|
|
ubsan=ubsan,
|
|
|
|
ubsan_abort=ubsan_abort,
|
2023-01-22 03:32:44 -04:00
|
|
|
extra_args=extra_configure_args,
|
|
|
|
dronecan_tests=dronecan_tests)
|
2016-06-01 04:11:44 -03:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2021-04-05 00:59:42 -03:00
|
|
|
def build_replay(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(), "replay"]
|
|
|
|
run_cmd(cmd_make, directory=topdir(), checkfail=True, show=True)
|
|
|
|
return True
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
|
|
|
def build_tests(board,
|
|
|
|
j=None,
|
|
|
|
debug=False,
|
|
|
|
clean=False,
|
|
|
|
configure=True,
|
|
|
|
math_check_indexes=False,
|
|
|
|
coverage=False,
|
|
|
|
ekf_single=False,
|
|
|
|
postype_single=False,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=False,
|
2022-08-22 05:36:21 -03:00
|
|
|
ubsan=False,
|
|
|
|
ubsan_abort=False,
|
2023-03-03 19:53:50 -04:00
|
|
|
num_aux_imus=0,
|
2023-01-22 03:32:44 -04:00
|
|
|
dronecan_tests=False,
|
2022-08-22 05:36:21 -03:00
|
|
|
extra_configure_args=[]):
|
2021-09-07 16:32:24 -03:00
|
|
|
|
2018-08-14 21:33:52 -03:00
|
|
|
# first configure
|
2021-05-20 11:28:22 -03:00
|
|
|
if configure:
|
|
|
|
waf_configure(board,
|
|
|
|
j=j,
|
|
|
|
debug=debug,
|
|
|
|
math_check_indexes=math_check_indexes,
|
2021-07-08 19:47:07 -03:00
|
|
|
ekf_single=ekf_single,
|
|
|
|
postype_single=postype_single,
|
2021-05-24 10:48:42 -03:00
|
|
|
coverage=coverage,
|
2023-01-08 18:56:33 -04:00
|
|
|
force_32bit=force_32bit,
|
2022-07-10 20:16:55 -03:00
|
|
|
ubsan=ubsan,
|
|
|
|
ubsan_abort=ubsan_abort,
|
2023-01-22 03:32:44 -04:00
|
|
|
num_aux_imus=num_aux_imus,
|
|
|
|
dronecan_tests=dronecan_tests,
|
|
|
|
extra_args=extra_configure_args,)
|
2018-08-14 21:33:52 -03:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2022-08-22 05:36:21 -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
|
2020-05-13 14:06:54 -03:00
|
|
|
if p is None:
|
|
|
|
print("Nothing to close")
|
|
|
|
return
|
2019-09-22 23:50:23 -03:00
|
|
|
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."""
|
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('/', '-')
|
2021-04-02 06:08:14 -03:00
|
|
|
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)
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2020-05-13 14:06:54 -03:00
|
|
|
def kill_mac_terminal():
|
|
|
|
global windowID
|
|
|
|
for window in windowID:
|
|
|
|
cmd = ("osascript -e \'tell application \"Terminal\" to close "
|
2022-08-22 05:36:21 -03:00
|
|
|
"(window(get index of window id %s))\'" % window)
|
2020-05-13 14:06:54 -03:00
|
|
|
os.system(cmd)
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2022-08-22 05:37:48 -03:00
|
|
|
class FakeMacOSXSpawn(object):
|
2023-02-03 17:30:14 -04:00
|
|
|
"""something that looks like a pspawn child so we can ignore attempts
|
2022-08-22 05:37:48 -03:00
|
|
|
to pause (and otherwise kill(1) SITL. MacOSX using osascript to
|
|
|
|
start/stop sitl
|
2023-02-03 17:30:14 -04:00
|
|
|
"""
|
2022-08-22 05:37:48 -03:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def progress(self, message):
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
def kill(self, sig):
|
|
|
|
# self.progress("FakeMacOSXSpawn: ignoring kill(%s)" % str(sig))
|
|
|
|
pass
|
|
|
|
|
|
|
|
def isalive(self):
|
|
|
|
self.progress("FakeMacOSXSpawn: assuming process is alive")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-05-30 20:17:23 -03:00
|
|
|
class PSpawnStdPrettyPrinter(object):
|
|
|
|
'''a fake filehandle-like object which prefixes a string to all lines
|
|
|
|
before printing to stdout/stderr. To be used to pass to
|
|
|
|
pexpect.spawn's logfile argument
|
|
|
|
'''
|
|
|
|
def __init__(self, output=sys.stdout, prefix="stdout"):
|
|
|
|
self.output = output
|
|
|
|
self.prefix = prefix
|
|
|
|
self.buffer = ""
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.print_prefixed_line(self.buffer)
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self.buffer += data
|
2023-06-02 04:19:11 -03:00
|
|
|
lines = self.buffer.split("\n")
|
|
|
|
self.buffer = lines[-1]
|
|
|
|
lines.pop()
|
|
|
|
for line in lines:
|
2023-05-30 20:17:23 -03:00
|
|
|
self.print_prefixed_line(line)
|
|
|
|
|
|
|
|
def print_prefixed_line(self, line):
|
|
|
|
print("%s: %s" % (self.prefix, line), file=self.output)
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-07-23 04:06:00 -03:00
|
|
|
def start_SITL(binary,
|
|
|
|
valgrind=False,
|
2021-11-29 02:18:13 -04:00
|
|
|
callgrind=False,
|
2018-07-23 04:06:00 -03:00
|
|
|
gdb=False,
|
2021-06-10 01:17:04 -03:00
|
|
|
gdb_no_tui=False,
|
2018-07-23 04:06:00 -03:00
|
|
|
wipe=False,
|
|
|
|
synthetic_clock=True,
|
|
|
|
home=None,
|
|
|
|
model=None,
|
|
|
|
speedup=1,
|
2023-07-14 02:47:51 -03:00
|
|
|
sim_rate_hz=None,
|
2024-04-09 23:40:17 -03:00
|
|
|
defaults_filepath=[],
|
2018-07-23 04:06:00 -03:00
|
|
|
unhide_parameters=False,
|
|
|
|
gdbserver=False,
|
|
|
|
breakpoints=[],
|
2019-07-11 21:58:46 -03:00
|
|
|
disable_breakpoints=False,
|
2019-12-01 22:01:06 -04:00
|
|
|
customisations=[],
|
2021-01-01 11:48:40 -04:00
|
|
|
lldb=False,
|
2024-09-03 05:45:35 -03:00
|
|
|
enable_fgview=False,
|
2023-05-30 20:17:23 -03:00
|
|
|
supplementary=False,
|
|
|
|
stdout_prefix=None):
|
2020-04-06 01:47:43 -03:00
|
|
|
|
2021-01-01 11:48:40 -04:00
|
|
|
if model is None and not supplementary:
|
2020-04-06 01:47:43 -03:00
|
|
|
raise ValueError("model must not be None")
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Launch a SITL instance."""
|
2016-11-08 06:53:54 -04:00
|
|
|
cmd = []
|
2021-11-29 02:18:13 -04:00
|
|
|
if (callgrind or 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])
|
2021-11-29 02:18:13 -04:00
|
|
|
if callgrind:
|
|
|
|
cmd.extend(["--tool=callgrind"])
|
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")
|
2023-02-03 17:30:14 -04:00
|
|
|
for breakingpoint in breakpoints:
|
|
|
|
f.write("b %s\n" % (breakingpoint,))
|
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")
|
2020-07-03 02:43:08 -03:00
|
|
|
f.write("set pagination off\n")
|
2023-02-03 17:30:14 -04:00
|
|
|
for breakingpoint in breakpoints:
|
|
|
|
f.write("b %s\n" % (breakingpoint,))
|
2019-07-11 21:58:46 -03:00
|
|
|
if disable_breakpoints:
|
|
|
|
f.write("disable\n")
|
2021-06-10 01:17:04 -03:00
|
|
|
if not gdb_no_tui:
|
|
|
|
f.write("tui enable\n")
|
2016-11-08 06:53:54 -04:00
|
|
|
f.write("r\n")
|
|
|
|
f.close()
|
2020-05-13 14:06:54 -03:00
|
|
|
if sys.platform == "darwin" and os.getenv('DISPLAY'):
|
|
|
|
cmd.extend(['gdb', '-x', '/tmp/x.gdb', '--args'])
|
|
|
|
elif os.environ.get('DISPLAY'):
|
2018-07-26 22:32:57 -03:00
|
|
|
cmd.extend(['xterm', '-e', 'gdb', '-x', '/tmp/x.gdb', '--args'])
|
|
|
|
else:
|
|
|
|
cmd.extend(['screen',
|
|
|
|
'-L', '-Logfile', 'gdb.log',
|
|
|
|
'-d',
|
|
|
|
'-m',
|
|
|
|
'-S', 'ardupilot-gdb',
|
2023-08-05 00:11:52 -03:00
|
|
|
'gdb', '--cd', os.getcwd(), '-x', '/tmp/x.gdb', binary, '--args'])
|
2019-09-24 14:44:02 -03:00
|
|
|
elif lldb:
|
|
|
|
f = open("/tmp/x.lldb", "w")
|
2023-02-03 17:30:14 -04:00
|
|
|
for breakingpoint in breakpoints:
|
|
|
|
f.write("b %s\n" % (breakingpoint,))
|
2019-09-24 14:44:02 -03:00
|
|
|
if disable_breakpoints:
|
|
|
|
f.write("disable\n")
|
|
|
|
f.write("settings set target.process.stop-on-exec false\n")
|
|
|
|
f.write("process launch\n")
|
|
|
|
f.close()
|
2020-05-13 14:06:54 -03:00
|
|
|
if sys.platform == "darwin" and os.getenv('DISPLAY'):
|
|
|
|
cmd.extend(['lldb', '-s', '/tmp/x.lldb', '--'])
|
|
|
|
elif os.environ.get('DISPLAY'):
|
2022-08-22 05:36:21 -03:00
|
|
|
cmd.extend(['xterm', '-e', 'lldb', '-s', '/tmp/x.lldb', '--'])
|
2019-09-24 14:44:02 -03:00
|
|
|
else:
|
|
|
|
raise RuntimeError("DISPLAY was not set")
|
2017-07-07 01:05:04 -03:00
|
|
|
|
2016-11-08 06:53:54 -04:00
|
|
|
cmd.append(binary)
|
2024-04-09 23:40:17 -03:00
|
|
|
|
|
|
|
if defaults_filepath is None:
|
|
|
|
defaults_filepath = []
|
|
|
|
if not isinstance(defaults_filepath, list):
|
|
|
|
defaults_filepath = [defaults_filepath]
|
|
|
|
defaults = [reltopdir(path) for path in defaults_filepath]
|
|
|
|
|
2021-01-01 11:48:40 -04:00
|
|
|
if not supplementary:
|
|
|
|
if wipe:
|
|
|
|
cmd.append('-w')
|
|
|
|
if synthetic_clock:
|
|
|
|
cmd.append('-S')
|
|
|
|
if home is not None:
|
|
|
|
cmd.extend(['--home', home])
|
|
|
|
cmd.extend(['--model', model])
|
2023-07-14 02:47:51 -03:00
|
|
|
if speedup is not None and speedup != 1:
|
2024-04-09 23:40:17 -03:00
|
|
|
ntf = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
|
|
|
print(f"SIM_SPEEDUP {speedup}", file=ntf)
|
|
|
|
ntf.close()
|
|
|
|
# prepend it so that a caller can override the speedup in
|
|
|
|
# passed-in defaults:
|
|
|
|
defaults = [ntf.name] + defaults
|
2023-07-14 02:47:51 -03:00
|
|
|
if sim_rate_hz is not None:
|
|
|
|
cmd.extend(['--rate', str(sim_rate_hz)])
|
2021-01-01 11:48:40 -04:00
|
|
|
if unhide_parameters:
|
|
|
|
cmd.extend(['--unhide-groups'])
|
|
|
|
# somewhere for MAVProxy to connect to:
|
2023-12-11 14:52:03 -04:00
|
|
|
cmd.append('--serial1=tcp:2')
|
2024-09-03 05:45:35 -03:00
|
|
|
if enable_fgview:
|
2023-12-25 17:48:58 -04:00
|
|
|
cmd.append("--enable-fgview")
|
2018-07-26 22:32:57 -03:00
|
|
|
|
2024-04-09 23:40:17 -03:00
|
|
|
if len(defaults):
|
|
|
|
cmd.extend(['--defaults', ",".join(defaults)])
|
2023-08-20 22:15:04 -03:00
|
|
|
|
2021-01-01 11:48:40 -04:00
|
|
|
cmd.extend(customisations)
|
2019-03-13 02:53:56 -03:00
|
|
|
|
2024-04-17 22:12:37 -03:00
|
|
|
if "--defaults" in customisations:
|
|
|
|
raise ValueError("--defaults must be passed in via defaults_filepath keyword argument, not as part of customisation list") # noqa
|
|
|
|
|
2023-05-30 20:17:23 -03:00
|
|
|
pexpect_logfile_prefix = stdout_prefix
|
|
|
|
if pexpect_logfile_prefix is None:
|
|
|
|
pexpect_logfile_prefix = os.path.basename(binary)
|
|
|
|
pexpect_logfile = PSpawnStdPrettyPrinter(prefix=pexpect_logfile_prefix)
|
|
|
|
|
2020-05-13 14:06:54 -03:00
|
|
|
if (gdb or lldb) and sys.platform == "darwin" and os.getenv('DISPLAY'):
|
|
|
|
global windowID
|
|
|
|
# on MacOS record the window IDs so we can close them later
|
|
|
|
atexit.register(kill_mac_terminal)
|
|
|
|
child = None
|
|
|
|
mydir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
autotest_dir = os.path.realpath(os.path.join(mydir, '..'))
|
|
|
|
runme = [os.path.join(autotest_dir, "run_in_terminal_window.sh"), 'mactest']
|
2022-08-22 05:36:21 -03:00
|
|
|
runme.extend(cmd)
|
2020-05-13 14:06:54 -03:00
|
|
|
print(cmd)
|
|
|
|
out = subprocess.Popen(runme, stdout=subprocess.PIPE).communicate()[0]
|
|
|
|
out = out.decode('utf-8')
|
|
|
|
p = re.compile('tab 1 of window id (.*)')
|
|
|
|
|
|
|
|
tstart = time.time()
|
|
|
|
while time.time() - tstart < 5:
|
|
|
|
tabs = p.findall(out)
|
|
|
|
|
|
|
|
if len(tabs) > 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
# sleep for extra 2 seconds for application to start
|
|
|
|
time.sleep(2)
|
|
|
|
if len(tabs) > 0:
|
|
|
|
windowID.append(tabs[0])
|
|
|
|
else:
|
|
|
|
print("Cannot find %s process terminal" % binary)
|
2022-08-22 05:37:48 -03:00
|
|
|
child = FakeMacOSXSpawn()
|
2020-05-13 14:06:54 -03:00
|
|
|
elif 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"],
|
2023-05-30 20:17:23 -03:00
|
|
|
logfile=pexpect_logfile,
|
2018-07-26 22:32:57 -03:00
|
|
|
encoding=ENCODING,
|
|
|
|
timeout=5)
|
2020-05-13 14:06:54 -03:00
|
|
|
else:
|
|
|
|
print("Running: %s" % cmd_as_shell(cmd))
|
2018-07-26 22:32:57 -03:00
|
|
|
|
2020-05-13 14:06:54 -03:00
|
|
|
first = cmd[0]
|
|
|
|
rest = cmd[1:]
|
2023-05-30 20:17:23 -03:00
|
|
|
child = pexpect.spawn(first, rest, logfile=pexpect_logfile, encoding=ENCODING, timeout=5)
|
2020-05-13 14:06:54 -03:00
|
|
|
pexpect_autoclose(child)
|
2019-09-24 14:44:02 -03:00
|
|
|
if gdb or lldb:
|
2016-11-08 06:53:54 -04:00
|
|
|
# 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:
|
2020-09-29 15:54:02 -03:00
|
|
|
child.expect('Waiting for ', timeout=300)
|
2016-11-08 06:53:54 -04:00
|
|
|
return child
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
|
2019-08-05 02:53:40 -03:00
|
|
|
def mavproxy_cmd():
|
2023-02-03 17:30:14 -04:00
|
|
|
"""return path to which mavproxy to use"""
|
2019-08-05 02:53:40 -03:00
|
|
|
return os.getenv('MAVPROXY_CMD', 'mavproxy.py')
|
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2019-08-05 02:53:40 -03:00
|
|
|
def MAVProxy_version():
|
2023-02-03 17:30:14 -04:00
|
|
|
"""return the current version of mavproxy as a tuple e.g. (1,8,8)"""
|
2019-08-05 02:53:40 -03:00
|
|
|
command = "%s --version" % mavproxy_cmd()
|
|
|
|
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
|
|
|
output = output.decode('ascii')
|
|
|
|
match = re.search("MAVProxy Version: ([0-9]+)[.]([0-9]+)[.]([0-9]+)", output)
|
|
|
|
if match is None:
|
|
|
|
raise ValueError("Unable to determine MAVProxy version from (%s)" % output)
|
2023-02-03 17:30:14 -04:00
|
|
|
return int(match.group(1)), int(match.group(2)), int(match.group(3))
|
2019-08-05 02:53:40 -03:00
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2021-02-16 21:58:21 -04:00
|
|
|
def start_MAVProxy_SITL(atype,
|
|
|
|
aircraft=None,
|
|
|
|
setup=False,
|
2023-02-16 01:37:55 -04:00
|
|
|
master=None,
|
2021-02-16 21:58:21 -04:00
|
|
|
options=[],
|
2023-02-16 01:37:55 -04:00
|
|
|
sitl_rcin_port=5501,
|
2021-02-16 21:58:21 -04:00
|
|
|
pexpect_timeout=60,
|
|
|
|
logfile=sys.stdout):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""Launch mavproxy connected to a SITL instance."""
|
2023-02-16 01:37:55 -04:00
|
|
|
if master is None:
|
|
|
|
raise ValueError("Expected a master")
|
|
|
|
|
2019-12-31 19:42:45 -04:00
|
|
|
local_mp_modules_dir = os.path.abspath(
|
|
|
|
os.path.join(__file__, '..', '..', '..', 'mavproxy_modules'))
|
|
|
|
env = dict(os.environ)
|
2020-03-04 02:00:37 -04:00
|
|
|
old = env.get('PYTHONPATH', None)
|
|
|
|
env['PYTHONPATH'] = local_mp_modules_dir
|
|
|
|
if old is not None:
|
|
|
|
env['PYTHONPATH'] += os.path.pathsep + old
|
|
|
|
|
2011-11-09 00:45:18 -04:00
|
|
|
global close_list
|
2020-03-03 03:55:04 -04:00
|
|
|
cmd = []
|
|
|
|
cmd.append(mavproxy_cmd())
|
|
|
|
cmd.extend(['--master', master])
|
2023-02-16 01:37:55 -04:00
|
|
|
cmd.extend(['--sitl', "localhost:%u" % sitl_rcin_port])
|
2011-11-07 07:55:21 -04:00
|
|
|
if setup:
|
2020-03-03 03:55:04 -04:00
|
|
|
cmd.append('--setup')
|
2011-11-07 07:55:21 -04:00
|
|
|
if aircraft is None:
|
|
|
|
aircraft = 'test.%s' % atype
|
2020-03-03 03:55:04 -04:00
|
|
|
cmd.extend(['--aircraft', aircraft])
|
|
|
|
cmd.extend(options)
|
2022-08-01 02:00:22 -03:00
|
|
|
cmd.extend(['--default-modules', 'misc,wp,rally,fence,param,arm,mode,rc,cmdlong,output'])
|
2020-03-03 03:55:04 -04:00
|
|
|
|
2020-03-04 02:00:37 -04:00
|
|
|
print("PYTHONPATH: %s" % str(env['PYTHONPATH']))
|
2020-03-03 03:55:04 -04:00
|
|
|
print("Running: %s" % cmd_as_shell(cmd))
|
|
|
|
|
2021-02-16 21:58:21 -04:00
|
|
|
ret = pexpect.spawn(cmd[0], cmd[1:], logfile=logfile, encoding=ENCODING, timeout=pexpect_timeout, env=env)
|
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
|
|
|
|
|
|
|
|
|
2023-12-25 22:21:43 -04:00
|
|
|
def start_PPP_daemon(ips, sockaddr):
|
|
|
|
"""Start pppd for networking"""
|
|
|
|
|
|
|
|
global close_list
|
|
|
|
cmd = "sudo pppd socket %s debug noauth nodetach %s" % (sockaddr, ips)
|
|
|
|
cmd = cmd.split()
|
|
|
|
print("Running: %s" % cmd_as_shell(cmd))
|
|
|
|
|
|
|
|
ret = pexpect.spawn(cmd[0], cmd[1:], logfile=sys.stdout, encoding=ENCODING, timeout=30)
|
|
|
|
ret.delaybeforesend = 0
|
|
|
|
pexpect_autoclose(ret)
|
|
|
|
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."""
|
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)
|
2023-02-03 17:30:14 -04:00
|
|
|
except OSError:
|
2011-11-09 08:53:09 -04:00
|
|
|
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()
|
2023-02-03 17:30:14 -04:00
|
|
|
except OSError:
|
2013-03-25 18:20:40 -03:00
|
|
|
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)
|
2023-02-03 17:30:14 -04:00
|
|
|
except OSError:
|
2011-12-02 00:14:15 -04:00
|
|
|
print("Parent had finished - exiting")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
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))
|
2023-02-03 17:30:14 -04:00
|
|
|
return degrees(lat2), degrees(lon2)
|
2012-06-04 22:18:45 -03:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2022-08-22 05:36:21 -03:00
|
|
|
|
2020-11-08 20:30:11 -04:00
|
|
|
def load_local_module(fname):
|
2023-02-03 17:30:14 -04:00
|
|
|
"""load a python module from within the ardupilot tree"""
|
2020-11-08 20:30:11 -04:00
|
|
|
fname = os.path.join(topdir(), fname)
|
2020-11-08 17:58:40 -04:00
|
|
|
if sys.version_info.major >= 3:
|
|
|
|
import importlib.util
|
2020-11-08 20:30:11 -04:00
|
|
|
spec = importlib.util.spec_from_file_location("local_module", fname)
|
2020-11-08 17:58:40 -04:00
|
|
|
ret = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(ret)
|
|
|
|
else:
|
|
|
|
import imp
|
2020-11-08 20:30:11 -04:00
|
|
|
ret = imp.load_source("local_module", fname)
|
2020-11-08 17:58:40 -04:00
|
|
|
return ret
|
|
|
|
|
2018-07-31 06:50:02 -03:00
|
|
|
|
2023-11-16 10:09:45 -04:00
|
|
|
def get_git_hash(short=False):
|
|
|
|
short_v = "--short=8 " if short else ""
|
|
|
|
githash = run_cmd(f'git rev-parse {short_v}HEAD', output=True, directory=reltopdir('.')).strip()
|
|
|
|
if sys.version_info.major >= 3:
|
|
|
|
githash = githash.decode('utf-8')
|
|
|
|
return githash
|
|
|
|
|
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|