2012-03-22 08:50:47 -03:00
|
|
|
import math
|
2011-12-12 19:10:30 -04:00
|
|
|
import os, pexpect, sys, time, random
|
2012-03-22 08:50:47 -03:00
|
|
|
from rotmat import Vector3, Matrix3
|
2011-10-30 23:50:34 -03:00
|
|
|
from subprocess import call, check_call,Popen, PIPE
|
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def m2ft(x):
|
|
|
|
'''meters to feet'''
|
|
|
|
return float(x) / 0.3048
|
|
|
|
|
|
|
|
def ft2m(x):
|
|
|
|
'''feet to meters'''
|
|
|
|
return float(x) * 0.3048
|
|
|
|
|
|
|
|
def kt2mps(x):
|
|
|
|
return x * 0.514444444
|
|
|
|
|
|
|
|
def mps2kt(x):
|
|
|
|
return x / 0.514444444
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def topdir():
|
|
|
|
'''return top of git tree where autotest is running from'''
|
|
|
|
d = os.path.dirname(os.path.realpath(__file__))
|
2011-12-02 00:14:15 -04:00
|
|
|
assert(os.path.basename(d)=='pysim')
|
|
|
|
d = os.path.dirname(d)
|
2011-11-01 08:31:01 -03:00
|
|
|
assert(os.path.basename(d)=='autotest')
|
|
|
|
d = os.path.dirname(d)
|
|
|
|
assert(os.path.basename(d)=='Tools')
|
|
|
|
d = os.path.dirname(d)
|
|
|
|
return d
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2011-11-01 08:31:01 -03:00
|
|
|
def reltopdir(path):
|
|
|
|
'''return a path relative to topdir()'''
|
|
|
|
return os.path.normpath(os.path.join(topdir(), path))
|
2011-10-30 23:50:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def run_cmd(cmd, dir=".", show=False, output=False, checkfail=True):
|
|
|
|
'''run a shell command'''
|
|
|
|
if show:
|
|
|
|
print("Running: '%s' in '%s'" % (cmd, dir))
|
|
|
|
if output:
|
|
|
|
return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
|
|
|
|
elif checkfail:
|
|
|
|
return check_call(cmd, shell=True, cwd=dir)
|
|
|
|
else:
|
|
|
|
return call(cmd, shell=True, cwd=dir)
|
|
|
|
|
|
|
|
def rmfile(path):
|
|
|
|
'''remove a file if it exists'''
|
|
|
|
try:
|
2011-11-07 07:55:21 -04:00
|
|
|
os.unlink(path)
|
2011-10-30 23:50:34 -03:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def deltree(path):
|
|
|
|
'''delete a tree of files'''
|
|
|
|
run_cmd('rm -rf %s' % path)
|
2011-11-12 19:12:57 -04:00
|
|
|
|
|
|
|
|
2011-10-30 23:50:34 -03:00
|
|
|
|
2012-01-04 07:03:37 -04:00
|
|
|
def build_SIL(atype, target='sitl'):
|
2011-10-30 23:50:34 -03:00
|
|
|
'''build desktop SIL'''
|
2012-01-04 07:03:37 -04:00
|
|
|
run_cmd("make clean %s" % target,
|
2011-11-01 08:31:01 -03:00
|
|
|
dir=reltopdir(atype),
|
2011-10-30 23:50:34 -03:00
|
|
|
checkfail=True)
|
2011-11-09 01:15:53 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
def build_AVR(atype, board='mega2560'):
|
|
|
|
'''build AVR binaries'''
|
|
|
|
config = open(reltopdir('config.mk'), mode='w')
|
|
|
|
config.write('''
|
|
|
|
BOARD=%s
|
|
|
|
PORT=/dev/null
|
|
|
|
''' % board)
|
|
|
|
config.close()
|
|
|
|
run_cmd("make clean", dir=reltopdir(atype), checkfail=True)
|
|
|
|
run_cmd("make", dir=reltopdir(atype), checkfail=True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# list of pexpect children to close on exit
|
2011-11-09 00:45:18 -04:00
|
|
|
close_list = []
|
|
|
|
|
2011-11-09 01:15:53 -04:00
|
|
|
def pexpect_autoclose(p):
|
|
|
|
'''mark for autoclosing'''
|
|
|
|
global close_list
|
|
|
|
close_list.append(p)
|
|
|
|
|
2011-11-09 00:45:18 -04:00
|
|
|
def pexpect_close(p):
|
|
|
|
'''close a pexpect child'''
|
|
|
|
global close_list
|
2011-11-13 20:17:57 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
def pexpect_close_all():
|
|
|
|
'''close all pexpect children'''
|
|
|
|
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
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def pexpect_drain(p):
|
|
|
|
'''drain any pending input'''
|
|
|
|
try:
|
|
|
|
p.read_nonblocking(1000, timeout=0)
|
|
|
|
except pexpect.TIMEOUT:
|
|
|
|
pass
|
|
|
|
|
2011-11-25 23:45:18 -04:00
|
|
|
def start_SIL(atype, valgrind=False, wipe=False, CLI=False, height=None):
|
2011-10-30 23:50:34 -03:00
|
|
|
'''launch a SIL instance'''
|
2011-11-07 07:55:21 -04:00
|
|
|
cmd=""
|
2011-11-08 03:07:01 -04:00
|
|
|
if valgrind and os.path.exists('/usr/bin/valgrind'):
|
2011-11-07 07:55:21 -04:00
|
|
|
cmd += 'valgrind -q --log-file=%s-valgrind.log ' % atype
|
|
|
|
cmd += reltopdir('tmp/%s.build/%s.elf' % (atype, atype))
|
|
|
|
if wipe:
|
|
|
|
cmd += ' -w'
|
|
|
|
if CLI:
|
|
|
|
cmd += ' -s'
|
2011-11-25 23:45:18 -04:00
|
|
|
if height is not None:
|
|
|
|
cmd += ' -H %u' % height
|
2011-11-07 07:55:21 -04:00
|
|
|
ret = pexpect.spawn(cmd, logfile=sys.stdout, timeout=5)
|
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
|
|
|
ret.expect('Waiting for connection')
|
|
|
|
return ret
|
|
|
|
|
2011-11-07 07:55:21 -04:00
|
|
|
def start_MAVProxy_SIL(atype, aircraft=None, setup=False, master='tcp:127.0.0.1:5760',
|
|
|
|
options=None, logfile=sys.stdout):
|
2011-10-30 23:50:34 -03:00
|
|
|
'''launch mavproxy connected to a SIL instance'''
|
2011-11-09 00:45:18 -04:00
|
|
|
global close_list
|
2012-07-24 01:29:24 -03:00
|
|
|
MAVPROXY = os.getenv('MAVPROXY', reltopdir('../MAVProxy/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
|
|
|
|
if options is not None:
|
|
|
|
cmd += ' ' + options
|
|
|
|
ret = pexpect.spawn(cmd, logfile=logfile, 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):
|
|
|
|
'''setup a callback that is called once a second while waiting for
|
|
|
|
patterns'''
|
|
|
|
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)
|
|
|
|
pass
|
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
|
|
|
|
|
|
|
def mkdir_p(dir):
|
|
|
|
'''like mkdir -p'''
|
|
|
|
if not dir:
|
|
|
|
return
|
|
|
|
if dir.endswith("/"):
|
|
|
|
mkdir_p(dir[:-1])
|
|
|
|
return
|
|
|
|
if os.path.isdir(dir):
|
|
|
|
return
|
|
|
|
mkdir_p(os.path.dirname(dir))
|
|
|
|
os.mkdir(dir)
|
2011-11-09 08:43:25 -04:00
|
|
|
|
|
|
|
def loadfile(fname):
|
|
|
|
'''load a file as a string'''
|
|
|
|
f = open(fname, mode='r')
|
|
|
|
r = f.read()
|
|
|
|
f.close()
|
|
|
|
return r
|
2011-11-09 08:53:09 -04:00
|
|
|
|
|
|
|
def lock_file(fname):
|
|
|
|
'''lock a file'''
|
|
|
|
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
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
def check_parent(parent_pid=os.getppid()):
|
|
|
|
'''check our parent process is still alive'''
|
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):
|
2011-12-05 02:44:04 -04:00
|
|
|
'''convert the angular velocities from earth frame to
|
|
|
|
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
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
returns a gyro vector in body frame, in rad/s
|
2011-12-05 02:44:04 -04:00
|
|
|
'''
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
def BodyRatesToEarthRates(dcm, gyro):
|
2011-12-05 02:44:04 -04:00
|
|
|
'''convert the angular velocities from body frame to
|
|
|
|
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
|
|
|
|
2012-03-22 08:50:47 -03:00
|
|
|
returns a earth rate vector
|
2011-12-05 02:44:04 -04:00
|
|
|
'''
|
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
|
|
|
|
|
|
|
phiDot = p + tan(theta)*(q*sin(phi) + r*cos(phi))
|
|
|
|
thetaDot = q*cos(phi) - r*sin(phi)
|
|
|
|
if fabs(cos(theta)) < 1.0e-20:
|
|
|
|
theta += 1.0e-10
|
|
|
|
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
|
|
|
|
2012-06-04 22:18:45 -03:00
|
|
|
def gps_newpos(lat, lon, bearing, distance):
|
|
|
|
'''extrapolate latitude/longitude given a heading and distance
|
|
|
|
thanks to http://www.movable-type.co.uk/scripts/latlong.html
|
|
|
|
'''
|
|
|
|
from math import sin, asin, cos, atan2, radians, degrees
|
|
|
|
radius_of_earth = 6378100.0 # in meters
|
|
|
|
|
|
|
|
lat1 = radians(lat)
|
|
|
|
lon1 = radians(lon)
|
|
|
|
brng = radians(bearing)
|
|
|
|
dr = distance/radius_of_earth
|
|
|
|
|
|
|
|
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))
|
|
|
|
return (degrees(lat2), degrees(lon2))
|
|
|
|
|
2011-12-05 02:44:04 -04:00
|
|
|
|
2011-12-12 19:10:30 -04:00
|
|
|
class Wind(object):
|
|
|
|
'''a wind generation object'''
|
|
|
|
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)
|
|
|
|
self.speed = float(a[0]) # m/s
|
2011-12-13 00:28:03 -04:00
|
|
|
self.direction = float(a[1]) # direction the wind is going in
|
2011-12-12 19:10:30 -04:00
|
|
|
self.turbulance= float(a[2]) # turbulance factor (standard deviation)
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
'''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
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
|
|
|
|
def accel(self, velocity, deltat=None):
|
|
|
|
'''return current wind acceleration in ground frame. The
|
|
|
|
velocity 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
|
|
|
|
|
|
|
(speed, direction) = self.current(deltat=deltat)
|
|
|
|
|
|
|
|
# wind vector
|
2012-03-22 08:50:47 -03:00
|
|
|
v = Vector3(speed, 0, 0)
|
|
|
|
m = Matrix3()
|
|
|
|
m.from_euler(0, 0, radians(direction))
|
|
|
|
wind = m.transposed() * v
|
2011-12-12 19:10:30 -04:00
|
|
|
|
|
|
|
# relative wind vector
|
|
|
|
relwind = wind - velocity
|
|
|
|
|
2011-12-13 00:28:03 -04:00
|
|
|
# add in cross-section effect
|
2011-12-12 19:10:30 -04:00
|
|
|
a = relwind * self.cross_section
|
|
|
|
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
2011-12-02 00:14:15 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|