2012-06-13 22:53:19 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
#flightgear interface to uhd_modes.py
|
|
|
|
#outputs UDP data to add traffic to FGFS
|
|
|
|
|
|
|
|
import struct
|
|
|
|
import socket
|
2012-07-17 10:27:09 +08:00
|
|
|
import air_modes
|
|
|
|
from air_modes import mlat
|
2012-06-13 22:53:19 +08:00
|
|
|
import sqlite3
|
|
|
|
import string, threading, math, time
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.sql import output_sql
|
2012-06-13 22:53:19 +08:00
|
|
|
from Quaternion import Quat
|
|
|
|
import numpy
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.exceptions import *
|
2012-06-13 22:53:19 +08:00
|
|
|
|
2013-06-19 10:06:30 +08:00
|
|
|
class output_flightgear:
|
|
|
|
def __init__(self, cprdec, hostname, port, pub):
|
2012-06-13 22:53:19 +08:00
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
self.positions = {}
|
|
|
|
self.velocities = {}
|
|
|
|
self.callsigns = {}
|
2013-06-19 10:06:30 +08:00
|
|
|
self._cpr = cprdec
|
2012-06-13 22:53:19 +08:00
|
|
|
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2019-09-18 05:13:51 +08:00
|
|
|
# self.sock.connect((self.hostname, self.port))
|
2014-02-08 08:59:34 +08:00
|
|
|
pub.subscribe("type17_dl", self.output)
|
2012-06-13 22:53:19 +08:00
|
|
|
|
2013-06-19 10:06:30 +08:00
|
|
|
def output(self, msg):
|
2012-06-20 06:43:43 +08:00
|
|
|
try:
|
2013-06-19 10:06:30 +08:00
|
|
|
msgtype = msg.data["df"]
|
2012-06-20 06:43:43 +08:00
|
|
|
if msgtype == 17: #ADS-B report
|
2013-06-19 10:06:30 +08:00
|
|
|
icao24 = msg.data["aa"]
|
|
|
|
bdsreg = msg.data["me"].get_type()
|
2012-07-17 00:16:48 +08:00
|
|
|
if bdsreg == 0x08: #ident packet
|
2014-02-08 08:59:34 +08:00
|
|
|
(ident, actype) = air_modes.parseBDS08(msg.data)
|
2012-06-20 06:43:43 +08:00
|
|
|
#select model based on actype
|
|
|
|
self.callsigns[icao24] = [ident, actype]
|
2014-02-08 08:59:34 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x06: #BDS0,6 pos
|
2014-02-08 08:59:34 +08:00
|
|
|
[ground_track, decoded_lat, decoded_lon, rnge, bearing] = air_modes.parseBDS06(msg.data, self._cpr)
|
2012-06-27 14:27:58 +08:00
|
|
|
self.positions[icao24] = [decoded_lat, decoded_lon, 0]
|
2012-06-13 22:53:19 +08:00
|
|
|
self.update(icao24)
|
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x05: #BDS0,5 pos
|
2014-02-08 08:59:34 +08:00
|
|
|
[altitude, decoded_lat, decoded_lon, rnge, bearing] = air_modes.parseBDS05(msg.data, self._cpr)
|
2012-06-13 22:53:19 +08:00
|
|
|
self.positions[icao24] = [decoded_lat, decoded_lon, altitude]
|
|
|
|
self.update(icao24)
|
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x09: #velocity
|
2014-02-08 08:59:34 +08:00
|
|
|
subtype = msg.data["bds09"].get_type()
|
2012-07-17 00:16:48 +08:00
|
|
|
if subtype == 0:
|
2014-02-08 08:59:34 +08:00
|
|
|
[velocity, heading, vert_spd, turnrate] = air_modes.parseBDS09_0(msg.data)
|
2012-07-17 00:16:48 +08:00
|
|
|
elif subtype == 1:
|
2014-02-08 08:59:34 +08:00
|
|
|
[velocity, heading, vert_spd] = air_modes.parseBDS09_1(msg.data)
|
2012-07-17 00:16:48 +08:00
|
|
|
turnrate = 0
|
2012-06-27 06:37:20 +08:00
|
|
|
else:
|
|
|
|
return
|
2012-07-17 00:16:48 +08:00
|
|
|
|
2012-06-27 06:37:20 +08:00
|
|
|
self.velocities[icao24] = [velocity, heading, vert_spd, turnrate]
|
2014-02-08 08:59:34 +08:00
|
|
|
|
2012-06-20 06:43:43 +08:00
|
|
|
except ADSBError:
|
|
|
|
pass
|
2012-06-13 22:53:19 +08:00
|
|
|
|
|
|
|
def update(self, icao24):
|
|
|
|
#check to see that ICAO24 appears in all three records and that the data looks valid
|
|
|
|
complete = (icao24 in self.positions)\
|
|
|
|
and (icao24 in self.velocities)\
|
|
|
|
and (icao24 in self.callsigns)
|
|
|
|
if complete:
|
2019-09-18 05:13:51 +08:00
|
|
|
print("FG update: %s" % (self.callsigns[icao24][0]))
|
2012-06-13 22:53:19 +08:00
|
|
|
msg = fg_posmsg(self.callsigns[icao24][0],
|
2012-06-27 10:25:31 +08:00
|
|
|
self.callsigns[icao24][1],
|
2012-06-13 22:53:19 +08:00
|
|
|
self.positions[icao24][0],
|
|
|
|
self.positions[icao24][1],
|
|
|
|
self.positions[icao24][2],
|
|
|
|
self.velocities[icao24][1],
|
|
|
|
self.velocities[icao24][0],
|
|
|
|
self.velocities[icao24][2],
|
|
|
|
self.velocities[icao24][3]).pack()
|
|
|
|
|
2019-09-18 05:13:51 +08:00
|
|
|
self.sock.sendto(msg, (self.hostname, self.port))
|
2012-06-13 22:53:19 +08:00
|
|
|
|
|
|
|
class fg_header:
|
|
|
|
def __init__(self):
|
|
|
|
self.magic = "FGFS"
|
|
|
|
self.proto = 0x00010001
|
|
|
|
self.msgid = 0
|
|
|
|
self.msglen = 0 #in bytes, though they swear it isn't
|
|
|
|
self.replyaddr = 0 #unused
|
|
|
|
self.replyport = 0 #unused
|
|
|
|
self.callsign = "UNKNOWN"
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
hdrfmt = '!4sLLLLL8s0L'
|
|
|
|
|
|
|
|
def pack(self):
|
|
|
|
self.msglen = 32 + len(self.data)
|
|
|
|
packed = struct.pack(self.hdrfmt, self.magic, self.proto, self.msgid, self.msglen, self.replyaddr, self.replyport, self.callsign)
|
|
|
|
return packed
|
|
|
|
|
|
|
|
#so this appears to work, but FGFS doesn't display it in flight for some reason. not in the chat window either. oh well.
|
|
|
|
class fg_chatmsg(fg_header):
|
|
|
|
def __init__(self, msg):
|
|
|
|
fg_header.__init__(self)
|
|
|
|
self.chatmsg = msg
|
|
|
|
self.msgid = 1
|
|
|
|
|
|
|
|
def pack(self):
|
|
|
|
self.chatfmt = '!' + str(len(self.chatmsg)) + 's'
|
|
|
|
#print "Packing with strlen %i " % len(self.chatmsg)
|
|
|
|
self.data = struct.pack(self.chatfmt, self.chatmsg)
|
|
|
|
return fg_header.pack(self) + self.data
|
|
|
|
|
2012-06-16 06:53:03 +08:00
|
|
|
modelmap = { None: 'Aircraft/777-200/Models/777-200ER.xml',
|
2012-06-16 07:58:11 +08:00
|
|
|
"NO INFO": 'Aircraft/777-200/Models/777-200ER.xml',
|
2012-06-16 06:53:03 +08:00
|
|
|
"LIGHT": 'Aircraft/c172p/Models/c172p.xml',
|
|
|
|
"SMALL": 'Aircraft/CitationX/Models/Citation-X.xml',
|
|
|
|
"LARGE": 'Aircraft/CRJ700-family/Models/CRJ700.xml',
|
|
|
|
"LARGE HIGH VORTEX": 'Aircraft/757-200/Models/757-200.xml',
|
2019-09-18 05:13:51 +08:00
|
|
|
"HEAVY": 'Aircraft/IDG-A32X/Models/A320neo-CFM.xml',
|
2012-06-16 06:53:03 +08:00
|
|
|
"HIGH PERFORMANCE": 'Aircraft/SR71-BlackBird/Models/Blackbird-SR71B.xml', #yeah i know
|
|
|
|
"ROTORCRAFT": 'Aircraft/ec130/Models/ec130b4.xml',
|
|
|
|
"GLIDER": 'Aircraft/ASK21-MI/Models/ask21mi.xml',
|
|
|
|
"BALLOON/BLIMP": 'Aircraft/ZLT-NT/Models/ZLT-NT.xml',
|
|
|
|
"ULTRALIGHT": 'Aircraft/cri-cri/Models/MC-15.xml',
|
|
|
|
"UAV": 'Aircraft/YardStik/Models/yardstik.xml', #hahahaha
|
2012-06-16 07:55:04 +08:00
|
|
|
"SPACECRAFT": 'Aircraft/SpaceShip-One/Models/spaceshipone.xml',
|
2012-06-16 06:53:03 +08:00
|
|
|
"SURFACE EMERGENCY VEHICLE": 'Aircraft/followme/Models/follow_me.xml', #not the best
|
|
|
|
"SURFACE SERVICE VEHICLE": 'Aircraft/pushback/Models/Pushback.xml'
|
|
|
|
}
|
|
|
|
|
2012-06-13 22:53:19 +08:00
|
|
|
class fg_posmsg(fg_header):
|
2012-06-27 10:25:31 +08:00
|
|
|
def __init__(self, callsign, modelname, lat, lon, alt, hdg, vel, vs, turnrate):
|
2012-06-13 22:53:19 +08:00
|
|
|
#from the above, calculate valid FGFS mp vals
|
|
|
|
#this is the translation layer between ADS-B and FGFS
|
|
|
|
fg_header.__init__(self)
|
|
|
|
self.callsign = callsign
|
|
|
|
if self.callsign is None:
|
|
|
|
self.callsign = "UNKNOWN"
|
2012-06-27 10:25:31 +08:00
|
|
|
self.modelname = modelname
|
2012-06-16 06:53:03 +08:00
|
|
|
if self.modelname not in modelmap:
|
|
|
|
#this should keep people on their toes when strange aircraft types are seen
|
|
|
|
self.model = 'Aircraft/santa/Models/santa.xml'
|
|
|
|
else:
|
|
|
|
self.model = modelmap[self.modelname]
|
|
|
|
|
2012-06-13 22:53:19 +08:00
|
|
|
self.lat = lat
|
|
|
|
self.lon = lon
|
|
|
|
self.alt = alt
|
|
|
|
self.hdg = hdg
|
|
|
|
self.vel = vel
|
|
|
|
self.vs = vs
|
|
|
|
self.turnrate = turnrate
|
|
|
|
self.msgid = 7
|
|
|
|
self.time = time.time()
|
|
|
|
self.lag = 0
|
|
|
|
|
|
|
|
def pack(self):
|
|
|
|
#this is, in order:
|
|
|
|
#model, time (time.time() is fine), lag, position, orientation, linear vel, angular vel, linear accel, angular accel (accels unused), 0
|
|
|
|
#position is in ECEF format -- same as mlat uses. what luck!
|
|
|
|
pos = mlat.llh2ecef([self.lat, self.lon, self.alt * 0.3048]) #alt is in meters!
|
|
|
|
|
|
|
|
#get the rotation quaternion to rotate to local reference frame from lat/lon
|
|
|
|
rotquat = Quat([self.lat, self.lon])
|
|
|
|
#get the quaternion corresponding to aircraft orientation
|
|
|
|
acquat = Quat([self.hdg, 0, 0])
|
|
|
|
#rotate aircraft into ECEF frame
|
|
|
|
ecefquat = rotquat * acquat
|
|
|
|
#get it in angle/axis representation
|
|
|
|
(angle, axis) = ecefquat._get_angle_axis()
|
|
|
|
orientation = angle * axis
|
|
|
|
|
|
|
|
kts_to_ms = 0.514444444 #convert kts to m/s
|
|
|
|
vel_ms = self.vel * kts_to_ms
|
|
|
|
velvec = (vel_ms,0,0) #velocity vector in m/s -- is this in the local frame? looks like [0] is fwd vel,
|
|
|
|
#we'll pretend the a/c is always moving the dir it's pointing
|
|
|
|
turnvec = (0,0,self.turnrate * (math.pi / 180.) ) #turn rates in rad/s [roll, pitch, yaw]
|
|
|
|
accelvec = (0,0,0)
|
|
|
|
turnaccelvec = (0,0,0)
|
|
|
|
self.posfmt = '!96s' + 'd' + 'd' + '3d' + '3f' + '3f' + '3f' + '3f' + '3f' + 'I'
|
|
|
|
self.data = struct.pack(self.posfmt,
|
|
|
|
self.model,
|
|
|
|
self.time,
|
|
|
|
self.lag,
|
|
|
|
pos[0], pos[1], pos[2],
|
|
|
|
orientation[0], orientation[1], orientation[2],
|
|
|
|
velvec[0], velvec[1], velvec[2],
|
|
|
|
turnvec[0], turnvec[1], turnvec[2],
|
|
|
|
accelvec[0], accelvec[1], accelvec[2],
|
|
|
|
turnaccelvec[0], turnaccelvec[1], turnaccelvec[2],
|
|
|
|
0)
|
|
|
|
|
|
|
|
return fg_header.pack(self) + self.data
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
timeoffset = time.time()
|
|
|
|
iof = open('27augrudi3.txt')
|
|
|
|
localpos = [37.409066,-122.077836]
|
|
|
|
hostname = "localhost"
|
|
|
|
port = 5000
|
2012-07-17 10:27:09 +08:00
|
|
|
fgout = output_flightgear(localpos, hostname, port)
|
2012-06-13 22:53:19 +08:00
|
|
|
|
|
|
|
for line in iof:
|
|
|
|
timetosend = float(line.split()[6])
|
|
|
|
while (time.time() - timeoffset) < timetosend:
|
|
|
|
time.sleep(0.02)
|
|
|
|
fgout.output(line)
|