Reorganized things quite a bit. Much cleaner output interface.

Made a single registry of output plugins which gets populated on init with classes to output on a socket, print to screen, database, etc. Next step is to implement a SQLite database class.
This commit is contained in:
Nick Foster 2010-10-17 19:45:19 -07:00
parent 7727b7c902
commit 33b04c292c
9 changed files with 442 additions and 357 deletions

View File

@ -146,7 +146,14 @@ int air_modes_slicer::work(int noutput_items,
//the limitation on short packets means in practice a short packet has to be at least 6dB above the noise floor in order to be output. long packets can theoretically
//be decoded at the 3dB SNR point. below that and the preamble detector won't fire.
//in practice, this limitation causes you to see a HUGE number of type 11 packets which pass CRC through random luck.
//these packets necessarily have large numbers of low-confidence bits, so we toss them with an arbitrary limit of 10.
//that's a pretty dang low threshold so i don't think we'll drop many legit packets
if(rx_packet.type == Short_Packet && rx_packet.message_type != 11 && rx_packet.numlowconf != 0) continue;
if(rx_packet.type == Short_Packet && rx_packet.message_type == 11 && rx_packet.numlowconf >= 10) continue;
//if(rx_packet.numlowconf >= 24) continue; //don't even try, this is the maximum number of errors ECC could possibly correct
//the above line should be part of ECC, and only checked if the message has parity errors

View File

@ -6,11 +6,11 @@ import math, time
latz = 15
nbits = 17
#my_lat = 37.76225 #update these later!
#my_lon = -122.44254
my_lat = 37.76225 #update these later!
my_lon = -122.44254
#ER
my_lat = 37.40889176297184
my_lon = -122.07765340805054
#my_lat = 37.40889176297184
#my_lon = -122.07765340805054
def nz(ctype):

Binary file not shown.

View File

@ -6,7 +6,8 @@ from altitude import decode_alt
from cpr import cpr_decode
import math
def parse0(shortdata, parity, ecc):
class modes_parse:
def parse0(self, shortdata, parity, ecc):
# shortdata = long(shortdata, 16)
#parity = long(parity)
@ -20,7 +21,7 @@ def parse0(shortdata, parity, ecc):
return [vs, cc, sl, ri, altitude]
def parse4(shortdata, parity, ecc):
def parse4(self, shortdata, parity, ecc):
# shortdata = long(shortdata, 16)
fs = shortdata >> 24 & 0x07 #flight status: 0 is airborne normal, 1 is ground normal, 2 is airborne alert, 3 is ground alert, 4 is alert SPI, 5 is normal SPI
dr = shortdata >> 19 & 0x1F #downlink request: 0 means no req, bit 0 is Comm-B msg rdy bit, bit 1 is TCAS info msg rdy, bit 2 is Comm-B bcast #1 msg rdy, bit2+bit0 is Comm-B bcast #2 msg rdy,
@ -33,7 +34,7 @@ def parse4(shortdata, parity, ecc):
def parse5(shortdata, parity, ecc):
def parse5(self, shortdata, parity, ecc):
# shortdata = long(shortdata, 16)
fs = shortdata >> 24 & 0x07 #flight status: 0 is airborne normal, 1 is ground normal, 2 is airborne alert, 3 is ground alert, 4 is alert SPI, 5 is normal SPI
dr = shortdata >> 19 & 0x1F #downlink request: 0 means no req, bit 0 is Comm-B msg rdy bit, bit 1 is TCAS info msg rdy, bit 2 is Comm-B bcast #1 msg rdy, bit2+bit0 is Comm-B bcast #2 msg rdy,
@ -42,7 +43,7 @@ def parse5(shortdata, parity, ecc):
return [fs, dr, um]
def parse11(shortdata, parity, ecc):
def parse11(self, shortdata, parity, ecc):
# shortdata = long(shortdata, 16)
interrogator = ecc & 0x0F
@ -51,7 +52,7 @@ def parse11(shortdata, parity, ecc):
return [icao24, interrogator, ca]
#def parse17(shortdata, longdata, parity, ecc):
#def parse17(self, shortdata, longdata, parity, ecc):
# shortdata = long(shortdata, 16)
# longdata = long(longdata, 16)
# parity = long(parity, 16)
@ -101,18 +102,18 @@ def parse11(shortdata, parity, ecc):
# return retstr
def parseBDS08(shortdata, longdata, parity, ecc):
def parseBDS08(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
msg = ""
for i in range(0, 8):
msg += charmap( longdata >> (42-6*i) & 0x3F)
msg += self.charmap( longdata >> (42-6*i) & 0x3F)
#retstr = "Type 17 subtype 04 (ident) from " + "%x" % icao24 + " with data " + msg
return msg
def charmap(d):
def charmap(self, d):
if d > 0 and d < 27:
retval = chr(ord("A")+d-1)
elif d == 32:
@ -124,18 +125,17 @@ def charmap(d):
return retval
#lkplist is the last known position, for emitter-centered decoding. evenlist and oddlist are the last
#received encoded position data for each reporting type. all dictionaries indexed by ICAO number.
lkplist = {}
evenlist = {}
oddlist = {}
evenlist_ground = {}
oddlist_ground = {}
_lkplist = {}
_evenlist = {}
_oddlist = {}
_evenlist_ground = {}
_oddlist_ground = {}
#the above dictionaries are all in the format [lat, lon, time].
def parseBDS05(shortdata, longdata, parity, ecc):
def parseBDS05(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
encoded_lon = longdata & 0x1FFFF
@ -146,13 +146,13 @@ def parseBDS05(shortdata, longdata, parity, ecc):
altitude = decode_alt(enc_alt, False)
[decoded_lat, decoded_lon, rnge, bearing] = cpr_decode(icao24, encoded_lat, encoded_lon, cpr_format, evenlist, oddlist, lkplist, 0, longdata)
[decoded_lat, decoded_lon, rnge, bearing] = cpr_decode(icao24, encoded_lat, encoded_lon, cpr_format, self._evenlist, self._oddlist, self._lkplist, 0, longdata)
return [altitude, decoded_lat, decoded_lon, rnge, bearing]
#welp turns out it looks like there's only 17 bits in the BDS0,6 ground packet after all. fuck.
def parseBDS06(shortdata, longdata, parity, ecc):
def parseBDS06(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
encoded_lon = longdata & 0x1FFFF
@ -163,12 +163,11 @@ def parseBDS06(shortdata, longdata, parity, ecc):
altitude = 0
[decoded_lat, decoded_lon, rnge, bearing] = cpr_decode(icao24, encoded_lat, encoded_lon, cpr_format, evenlist_ground, oddlist_ground, lkplist, 1, longdata)
[decoded_lat, decoded_lon, rnge, bearing] = cpr_decode(icao24, encoded_lat, encoded_lon, cpr_format, self._evenlist_ground, self._oddlist_ground, self._lkplist, 1, longdata)
return [altitude, decoded_lat, decoded_lon, rnge, bearing]
def parseBDS09_0(shortdata, longdata, parity, ecc):
def parseBDS09_0(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
vert_spd = ((longdata >> 6) & 0x1FF) * 32
ud = bool((longdata >> 15) & 1)
@ -198,7 +197,7 @@ def parseBDS09_0(shortdata, longdata, parity, ecc):
return [velocity, heading, vert_spd]
def parseBDS09_1(shortdata, longdata, parity, ecc):
def parseBDS09_1(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
alt_geo_diff = longdata & 0x7F - 1
above_below = bool((longdata >> 7) & 1)
@ -240,8 +239,3 @@ def parseBDS09_1(shortdata, longdata, parity, ecc):
#retstr = "Type 17 subtype 09-1 (track report) from " + "%x" % icao24 + " with velocity " + "%.0f" % velocity + "kt heading " + "%.0f" % heading + " VS " + "%.0f" % vert_spd
return [velocity, heading, vert_spd]
def parse20(shortdata, longdata, parity, ecc):
return "Message 20 not yet implemented"

Binary file not shown.

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python
import time, os, sys
from string import split, join
from modes_parse import *
import modes_parse
import math
def modes_print(message):
#a mode S parser for all message types. first, split the input into data fields
class modes_output_print(modes_parse.modes_parse):
def parse(self, message):
[msgtype, shortdata, longdata, parity, ecc, reference] = message.split()
shortdata = long(shortdata, 16)
@ -19,26 +19,24 @@ def modes_print(message):
output = str("")
if msgtype == 0:
output = print0(shortdata, parity, ecc)
output = self.print0(shortdata, parity, ecc)
elif msgtype == 4:
output = print4(shortdata, parity, ecc)
output = self.print4(shortdata, parity, ecc)
elif msgtype == 5:
output = print5(shortdata, parity, ecc)
output = self.print5(shortdata, parity, ecc)
elif msgtype == 11:
output = print11(shortdata, parity, ecc)
output = self.print11(shortdata, parity, ecc)
elif msgtype == 17:
output = print17(shortdata, longdata, parity, ecc)
elif msgtype == 20:
output = parse20(shortdata, longdata, parity, ecc)
output = self.print17(shortdata, longdata, parity, ecc)
else:
output = "No handler for message type " + str(msgtype) + " from " + str(ecc)
output = "(%.0f) " % (10.0*math.log10(float(reference))) + output
return output
print output
def print0(shortdata, parity, ecc):
[vs, cc, sl, ri, altitude] = parse0(shortdata, parity, ecc)
def print0(self, shortdata, parity, ecc):
[vs, cc, sl, ri, altitude] = self.parse0(shortdata, parity, ecc)
retstr = "Type 0 (short A-A surveillance) from " + "%x" % ecc + " at " + str(altitude) + "ft"
# the ri values below 9 are used for other things. might want to print those someday.
@ -52,9 +50,9 @@ def print0(shortdata, parity, ecc):
return retstr
def print4(shortdata, parity, ecc):
def print4(self, shortdata, parity, ecc):
[fs, dr, um, altitude] = parse4(shortdata, parity, ecc)
[fs, dr, um, altitude] = self.parse4(shortdata, parity, ecc)
retstr = "Type 4 (short surveillance altitude reply) from " + "%x" % ecc + " at " + str(altitude) + "ft"
@ -71,8 +69,8 @@ def print4(shortdata, parity, ecc):
return retstr
def print5(shortdata, parity, ecc):
[fs, dr, um] = parse5(shortdata, parity, ecc)
def print5(self, shortdata, parity, ecc):
[fs, dr, um] = self.parse5(shortdata, parity, ecc)
retstr = "Type 5 (short surveillance ident reply) from " + "%x" % ecc + " with ident " + str(shortdata & 0x1FFF)
@ -89,30 +87,29 @@ def print5(shortdata, parity, ecc):
return retstr
def print11(shortdata, parity, ecc):
[icao24, interrogator, ca] = parse11(shortdata, parity, ecc)
def print11(self, shortdata, parity, ecc):
[icao24, interrogator, ca] = self.parse11(shortdata, parity, ecc)
retstr = "Type 11 (all call reply) from " + "%x" % icao24 + " in reply to interrogator " + str(interrogator)
return retstr
def print17(shortdata, longdata, parity, ecc):
def print17(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
subtype = (longdata >> 51) & 0x1F;
if subtype == 4:
msg = parseBDS08(shortdata, longdata, parity, ecc)
msg = self.parseBDS08(shortdata, longdata, parity, ecc)
retstr = "Type 17 subtype 04 (ident) from " + "%x" % icao24 + " with data " + msg
elif subtype >= 5 and subtype <= 8:
[altitude, decoded_lat, decoded_lon, rnge, bearing] = parseBDS06(shortdata, longdata, parity, ecc)
[altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(shortdata, longdata, parity, ecc)
if decoded_lat==0: #no unambiguously valid position available
retstr = ""
else:
#retstr = "INSERT INTO plane_positions (icao, seen, alt, lat, lon) VALUES ('" + "%x" % icao24 + "', now(), " + str(altitude) + ", " + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ")"
retstr = "Type 17 subtype 06 (surface report) from " + "%x" % icao24 + " at (" + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ") (" + "%.2f" % rnge + " @ " + "%.0f" % bearing + ")"
elif subtype >= 9 and subtype <= 18:
[altitude, decoded_lat, decoded_lon, rnge, bearing] = parseBDS05(shortdata, longdata, parity, ecc)
[altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(shortdata, longdata, parity, ecc)
retstr = "Type 17 subtype 05 (position report) from " + "%x" % icao24 + " at (" + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ") (" + "%.2f" % rnge + " @ " + "%.0f" % bearing + ") at " + str(altitude) + "ft"
@ -123,11 +120,11 @@ def print17(shortdata, longdata, parity, ecc):
elif subtype == 19:
subsubtype = (longdata >> 48) & 0x07
if subsubtype == 0:
[velocity, heading, vert_spd] = parseBDS09_0(shortdata, longdata, parity, ecc)
[velocity, heading, vert_spd] = self.parseBDS09_0(shortdata, longdata, parity, ecc)
retstr = "Type 17 subtype 09-0 (track report) from " + "%x" % icao24 + " with velocity " + "%.0f" % velocity + "kt heading " + "%.0f" % heading + " VS " + "%.0f" % vert_spd
elif subsubtype == 1:
[velocity, heading, vert_spd] = parseBDS09_1(shortdata, longdata, parity, ecc)
[velocity, heading, vert_spd] = self.parseBDS09_1(shortdata, longdata, parity, ecc)
retstr = "Type 17 subtype 09-1 (track report) from " + "%x" % icao24 + " with velocity " + "%.0f" % velocity + "kt heading " + "%.0f" % heading + " VS " + "%.0f" % vert_spd
else:
@ -136,7 +133,3 @@ def print17(shortdata, longdata, parity, ecc):
retstr = "Type 17, subtype " + str(subtype) + " not implemented"
return retstr

96
src/python/modes_sbs1.py Normal file
View File

@ -0,0 +1,96 @@
import time, os, sys, socket
from string import split, join
import modes_parse
from datetime import *
class modes_output_sbs1(modes_parse.modes_parse):
def __init__(self):
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.bind(('', 30003))
self._s.listen(1)
self._s.setblocking(0) #nonblocking
self._conns = [] #list of active connections
def output(self, msg):
sbs1_msg = self.parse(msg)
if sbs1_msg is not None:
for conn in self._conns[:]: #iterate over a copy of the list
try:
conn.send(sbs1_msg)
except socket.error:
self._conns.remove(conn)
print "Connections: ", len(self._conns)
def add_pending_conns(self):
try:
conn, addr = self._s.accept()
self._conns.append(conn)
print "Connections: ", len(self._conns)
except socket.error:
pass
def __del__(self):
self._s.close()
def parse(self, message):
#assembles a SBS-1-style output string from the received message
#this version ignores anything that isn't Type 17 for now, because we just don't care
[msgtype, shortdata, longdata, parity, ecc, reference] = message.split()
shortdata = long(shortdata, 16)
longdata = long(longdata, 16)
parity = long(parity, 16)
ecc = long(ecc, 16)
# reference = float(reference)
msgtype = int(msgtype)
outmsg = None
if msgtype == 17:
outmsg = self.pp17(shortdata, longdata, parity, ecc)
return outmsg
def pp17(self, shortdata, longdata, parity, ecc):
icao24 = shortdata & 0xFFFFFF
subtype = (longdata >> 51) & 0x1F
retstr = None
timenow = datetime.now()
datestr = timenow.strftime("%Y/%m/%d")
timestr = timenow.strftime("%H:%M:%S.000") #we'll get better timestamps later, hopefully with actual VRT time in them
if subtype == 4:
msg = self.parseBDS08(shortdata, longdata, parity, ecc)
retstr = "MSG,1,0,0,%X,0,%s,%s,%s,%s,%s,,,,,,,,,,,\n" % (icao24, datestr, timestr, datestr, timestr, msg)
elif subtype >= 5 and subtype <= 8:
[altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(shortdata, longdata, parity, ecc)
if decoded_lat is None: #no unambiguously valid position available
retstr = None
else:
retstr = "MSG,3,0,0,%X,0,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\n" % (icao24, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon)
elif subtype >= 9 and subtype <= 18 and subtype != 15: #i'm eliminating type 15 records because they don't appear to be valid position reports.
[altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(shortdata, longdata, parity, ecc)
if decoded_lat is None: #no unambiguously valid position available
retstr = None
else:
retstr = "MSG,3,0,0,%X,0,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\n" % (icao24, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon)
elif subtype == 19:
subsubtype = (longdata >> 48) & 0x07
if subsubtype == 0:
[velocity, heading, vert_spd] = self.parseBDS09_0(shortdata, longdata, parity, ecc)
retstr = "MSG,4,0,0,%X,0,%s,%s,%s,%s,,,%.1f,%.1f,,,%i,,,,,\n" % (icao24, datestr, timestr, datestr, timestr, velocity, heading, vert_spd)
elif subsubtype == 1:
[velocity, heading, vert_spd] = self.parseBDS09_1(shortdata, longdata, parity, ecc)
retstr = "MSG,4,0,0,%X,0,%s,%s,%s,%s,,,%.1f,%.1f,,,%i,,,,,\n" % (icao24, datestr, timestr, datestr, timestr, velocity, heading, vert_spd)
#else:
#print "debug (modes_sbs1): unknown subtype %i with data %x %x %x\n" % (subtype, shortdata, longdata, parity,)
return retstr

View File

@ -7,11 +7,10 @@ from optparse import OptionParser
import time, os, sys
from string import split, join
from usrpm import usrp_dbid
from modes_print import modes_print
from modes_print import modes_output_print
from modes_sql import modes_sql
from modes_sbs1 import modes_output_sbs1
import gnuradio.gr.gr_threading as _threading
import MySQLdb
class top_block_runner(_threading.Thread):
def __init__(self, tb):
@ -26,20 +25,6 @@ class top_block_runner(_threading.Thread):
self.done = True
"""
The following are optional command line parameters:
-R SUBDEV Daughter board specification, defaults to first found
-f FREQ USRP receive frequency (1090 MHz Default)
-g GAIN Daughterboard gain setting. Defaults to mid-range.
-r RATE USRP sample rate
-t THRESH Receiver valid pulse threshold
-a Output all frames. Defaults only output frames
Once the program is running, ctrl-break (Ctrl-C) stops operation.
"""
class adsb_rx_block (gr.top_block):
def __init__(self, options, args, queue):
@ -61,14 +46,13 @@ class adsb_rx_block (gr.top_block):
if options.gain is None: #set to halfway
g = self.u.get_gain_range()
options.gain = (g[0]+g[1]) / 2.0
options.gain = (g.min+g.max) / 2.0
if not(self.tune(options.freq)):
print "Failed to set initial frequency"
print "Setting gain to %i" % (options.gain,)
self.u.set_gain(options.gain)
# self.subdev.set_bw(self.options.bandwidth) #only for DBSRX
else:
rate = options.rate
@ -86,15 +70,7 @@ class adsb_rx_block (gr.top_block):
self.framer = air.modes_framer(rate)
self.slicer = air.modes_slicer(rate, queue)
# if options.rate < 16:
#there's a really nasty spur at 1088 caused by a multiple of the USRP xtal. if you use a decimation of 16, it gets filtered out by the CIC. if not, it really fucks with you unless you filter it out.
# filter_coeffs = gr.firdes.band_reject(1.0, rate, 1.7e6, 2.3e6, 0.5e6, gr.firdes.WIN_HAMMING)
# self.filt = gr.fir_filter_ccf(1, filter_coeffs)
# self.connect(self.u, self.filt)
# else:
self.filt = self.u
self.connect(self.filt, self.demod)
self.connect(self.u, self.demod)
self.connect(self.demod, self.avg)
self.connect(self.demod, (self.preamble, 0))
self.connect(self.avg, (self.preamble, 1))
@ -107,11 +83,15 @@ class adsb_rx_block (gr.top_block):
result = self.u.set_center_freq(freq)
return result
def post_to_sql(db, query):
if query is not None:
c = db.cursor()
c.execute(query)
if __name__ == '__main__':
usage = "%prog: [options] output filename"
parser = OptionParser(option_class=eng_option, usage=usage)
parser.add_option("-R", "--rx-subdev-spec", type="subdev",
parser.add_option("-R", "--rx-subdev-spec", type="string",
help="select USRP Rx side A or B", metavar="SUBDEV")
parser.add_option("-f", "--freq", type="eng_float", default=1090e6,
help="set receive frequency in Hz [default=%default]", metavar="FREQ")
@ -124,41 +104,57 @@ if __name__ == '__main__':
parser.add_option("-a","--output-all", action="store_true", default=False,
help="output all frames")
parser.add_option("-b","--bandwidth", type="eng_float", default=5e6,
help="set DBSRX front-end bandwidth in Hz [default=5e6]")
help="set DBSRX baseband bandwidth in Hz [default=%default]")
parser.add_option("-F","--filename", type="string", default=None,
help="read data from file instead of USRP")
parser.add_option("-D","--database", action="store_true", default=False,
help="send to database instead of printing to screen")
parser.add_option("-P","--sbs1", action="store_true", default=False,
help="open an SBS-1-compatible server on port 30003")
parser.add_option("-n","--no-print", action="store_true", default=False,
help="disable printing decoded packets to stdout")
(options, args) = parser.parse_args()
# if len(args) != 1:
# parser.print_help()
# sys.exit(1)
# filename = args[0]
queue = gr.msg_queue()
outputs = [] #registry of plugin output functions
updates = [] #registry of plugin update functions
if options.database is True:
db = MySQLdb.connect(host="localhost", user="planes", passwd="planes", db="planes")
import pysqlite3
#db = pysqlite3.connect(:memory:)
#here we have to initialize the database with the correct tables and relations
if options.sbs1 is True:
sbs1port = modes_output_sbs1()
outputs.append(sbs1port.output)
updates.append(sbs1port.add_pending_conns)
if options.no_print is not True:
outputs.append(modes_output_print().parse)
fg = adsb_rx_block(options, args, queue)
runner = top_block_runner(fg)
while 1:
try:
#the update registry is really for the SBS1 plugin -- we're looking for new TCP connections.
#i think we have to do this here rather than in the output handler because otherwise connections will stack up
#until the next output
for update in updates:
update()
#main message handler
if queue.empty_p() == 0 :
while queue.empty_p() == 0 :
msg = queue.delete_head() #blocking read
if options.database is False:
print modes_print(msg.to_string())
else:
query = modes_sql(msg.to_string())
if query is not None:
c = db.cursor()
c.execute(query)
for out in outputs:
out(msg.to_string())
elif runner.done:
break
raise KeyboardInterrupt
else:
time.sleep(0.1)
@ -166,4 +162,3 @@ if __name__ == '__main__':
fg.stop()
runner = None
break

View File

@ -79,7 +79,7 @@ class adsb_rx_block (gr.top_block):
print "Setting gain to %i" % (options.gain,)
self.subdev.set_gain(options.gain)
self.subdev.set_bw(self.options.bandwidth) #only for DBSRX
#self.subdev.set_bw(self.options.bandwidth) #only for DBSRX
rate = self.u.adc_rate() / options.decim