2011-12-02 00:16:06 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2011-12-22 21:07:32 -04:00
|
|
|
import socket, struct, time, errno
|
2011-12-02 00:16:06 -04:00
|
|
|
from math import *
|
|
|
|
|
|
|
|
class udp_out(object):
|
|
|
|
'''a UDP output socket'''
|
|
|
|
def __init__(self, device):
|
|
|
|
a = device.split(':')
|
|
|
|
if len(a) != 2:
|
|
|
|
print("UDP ports must be specified as host:port")
|
|
|
|
sys.exit(1)
|
|
|
|
self.port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.destination_addr = (a[0], int(a[1]))
|
|
|
|
self.port.setblocking(0)
|
|
|
|
self.last_address = None
|
|
|
|
|
|
|
|
def recv(self,n=None):
|
|
|
|
try:
|
|
|
|
data, self.last_address = self.port.recvfrom(300)
|
|
|
|
except socket.error as e:
|
2011-12-22 21:07:32 -04:00
|
|
|
if e.errno in [ errno.EAGAIN, errno.EWOULDBLOCK ]:
|
2011-12-02 00:16:06 -04:00
|
|
|
return ""
|
|
|
|
raise
|
|
|
|
return data
|
|
|
|
|
|
|
|
def write(self, buf):
|
|
|
|
try:
|
|
|
|
self.port.sendto(buf, self.destination_addr)
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def ft2m(x):
|
|
|
|
return x * 0.3048
|
|
|
|
|
|
|
|
def m2ft(x):
|
|
|
|
return x / 0.3048
|
|
|
|
|
|
|
|
def kt2mps(x):
|
|
|
|
return x * 0.514444444
|
|
|
|
|
|
|
|
def mps2kt(x):
|
|
|
|
return x / 0.514444444
|
|
|
|
|
|
|
|
udp = udp_out("127.0.0.1:5501")
|
|
|
|
|
|
|
|
latitude = -35
|
|
|
|
longitude = 149
|
|
|
|
altitude = 600.0
|
|
|
|
heading = 0.0
|
|
|
|
speedN = 0
|
|
|
|
speedE = 0.0
|
2013-03-27 20:28:08 -03:00
|
|
|
speedD = 0.0
|
2011-12-02 00:16:06 -04:00
|
|
|
xAccel = 0.0
|
|
|
|
yAccel = 0.0
|
|
|
|
zAccel = 0.0
|
|
|
|
rollRate = 0.0
|
|
|
|
pitchRate = 0.0
|
|
|
|
yawRate = 0.0
|
|
|
|
rollDeg = 0.0
|
|
|
|
pitchDeg = 0.0
|
|
|
|
yawDeg = 0.0
|
|
|
|
airspeed = 0
|
2013-03-27 20:28:08 -03:00
|
|
|
magic = 0x4c56414f
|
2011-12-02 00:16:06 -04:00
|
|
|
|
|
|
|
deltaT = 0.005
|
|
|
|
rollDeg = 45
|
|
|
|
pitchDeg = 0
|
|
|
|
|
|
|
|
pitchMax = None
|
|
|
|
rollMax = None
|
|
|
|
|
|
|
|
if True:
|
|
|
|
pitchRate = 1
|
|
|
|
pitchMax = 45
|
|
|
|
rollMax = 45
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
2012-02-23 23:14:58 -04:00
|
|
|
xAccel = sin(radians(pitchDeg))
|
2011-12-02 00:16:06 -04:00
|
|
|
yAccel = -sin(radians(rollDeg)) * cos(radians(pitchDeg))
|
|
|
|
zAccel = -cos(radians(rollDeg)) * cos(radians(pitchDeg))
|
2011-12-02 08:43:03 -04:00
|
|
|
scale = 9.81 / sqrt((xAccel*xAccel)+(yAccel*yAccel)+(zAccel*zAccel))
|
2011-12-02 00:16:06 -04:00
|
|
|
xAccel *= scale;
|
|
|
|
yAccel *= scale;
|
|
|
|
zAccel *= scale;
|
|
|
|
|
2013-03-27 20:28:08 -03:00
|
|
|
buf = struct.pack('<17dI',
|
2011-12-02 00:16:06 -04:00
|
|
|
latitude, longitude, altitude, heading,
|
2013-03-27 20:28:08 -03:00
|
|
|
speedN, speedE, speedD,
|
2011-12-02 00:16:06 -04:00
|
|
|
xAccel, yAccel, zAccel,
|
|
|
|
rollRate, pitchRate, yawRate,
|
|
|
|
rollDeg, pitchDeg, yawDeg,
|
|
|
|
airspeed, magic)
|
|
|
|
udp.write(buf)
|
|
|
|
time.sleep(deltaT)
|
|
|
|
|
|
|
|
yawDeg += yawRate * deltaT
|
|
|
|
if yawDeg > 180:
|
|
|
|
yawDeg -= 360
|
|
|
|
if yawDeg < -180:
|
|
|
|
yawDeg += 360
|
|
|
|
heading = yawDeg
|
|
|
|
|
|
|
|
if pitchMax is not None and fabs(pitchDeg) > pitchMax:
|
|
|
|
pitchRate = -pitchRate
|
|
|
|
|
|
|
|
if rollMax is not None and fabs(rollDeg) > rollMax:
|
|
|
|
rollRate = -rollRate
|
|
|
|
|
|
|
|
pitchDeg += pitchRate * deltaT
|
|
|
|
if pitchDeg > 180:
|
|
|
|
pitchDeg -= 360
|
|
|
|
if pitchDeg < -180:
|
|
|
|
pitchDeg += 360
|
|
|
|
|
|
|
|
rollDeg += rollRate * deltaT
|
|
|
|
if rollDeg > 180:
|
|
|
|
rollDeg -= 360
|
|
|
|
if rollDeg < -180:
|
|
|
|
rollDeg += 360
|