2011-12-02 00:16:06 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
import errno
|
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
from math import cos, fabs, radians, sin, sqrt
|
|
|
|
|
2011-12-02 00:16:06 -04:00
|
|
|
|
|
|
|
class udp_out(object):
|
2016-07-31 07:22:06 -03:00
|
|
|
"""A UDP output socket."""
|
2011-12-02 00:16:06 -04:00
|
|
|
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
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
def recv(self, n=None):
|
2011-12-02 00:16:06 -04:00
|
|
|
try:
|
|
|
|
data, self.last_address = self.port.recvfrom(300)
|
|
|
|
except socket.error as e:
|
2016-07-31 07:22:06 -03: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
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:16:06 -04:00
|
|
|
def m2ft(x):
|
|
|
|
return x / 0.3048
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:16:06 -04:00
|
|
|
def kt2mps(x):
|
|
|
|
return x * 0.514444444
|
|
|
|
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2011-12-02 00:16:06 -04:00
|
|
|
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))
|
2016-07-31 07:22:06 -03:00
|
|
|
scale = 9.81 / sqrt((xAccel*xAccel) + (yAccel*yAccel) + (zAccel*zAccel))
|
|
|
|
xAccel *= scale
|
|
|
|
yAccel *= scale
|
|
|
|
zAccel *= scale
|
|
|
|
|
|
|
|
struc_buf = struct.pack('<17dI',
|
|
|
|
latitude, longitude, altitude, heading,
|
|
|
|
speedN, speedE, speedD,
|
|
|
|
xAccel, yAccel, zAccel,
|
|
|
|
rollRate, pitchRate, yawRate,
|
|
|
|
rollDeg, pitchDeg, yawDeg,
|
|
|
|
airspeed, magic)
|
|
|
|
udp.write(struc_buf)
|
2011-12-02 00:16:06 -04:00
|
|
|
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
|