From 4750d2004462b4907177913d27f1eb7a37e10d17 Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Mon, 16 Jul 2012 09:16:48 -0700 Subject: [PATCH 1/2] Parser returns objects for subfields instead of flat data. Lets you use get_type to resolve BDS registers, etc. Printer takes advantage of this. Now printing BDS reg instead of "subtype". --- python/modes_flightgear.py | 23 ++++++++++++----------- python/modes_parse.py | 6 ++++-- python/modes_print.py | 34 ++++++++++++++++------------------ python/modes_sbs1.py | 21 +++++++++------------ python/modes_sql.py | 23 ++++++++++------------- 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/python/modes_flightgear.py b/python/modes_flightgear.py index a1d51bc..2a50e95 100755 --- a/python/modes_flightgear.py +++ b/python/modes_flightgear.py @@ -34,31 +34,32 @@ class modes_flightgear(modes_parse.modes_parse): msgtype = data["df"] if msgtype == 17: #ADS-B report icao24 = data["aa"] - subtype = data["sub"] - if subtype == 4: #ident packet + bdsreg = data["me"].get_type() + if bdsreg == 0x08: #ident packet (ident, actype) = self.parseBDS08(data) #select model based on actype self.callsigns[icao24] = [ident, actype] - elif 5 <= subtype <= 8: #BDS0,6 pos + elif bdsreg == 0x06: #BDS0,6 pos [ground_track, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(data) self.positions[icao24] = [decoded_lat, decoded_lon, 0] self.update(icao24) - elif 9 <= subtype <= 18: #BDS0,5 pos + elif bdsreg == 0x05: #BDS0,5 pos [altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(data) self.positions[icao24] = [decoded_lat, decoded_lon, altitude] self.update(icao24) - elif subtype == 19: #velocity - subsubtype = data["sub"] - if subsubtype == 0: - [velocity, heading, vert_spd, turnrate] = self.parseBDS09_0(data) - elif 1 <= subsubtype <= 2: - [velocity, heading, vert_spd] = self.parseBDS09_1(data) - turnrate = 0 + elif bdsreg == 0x09: #velocity + subtype = data["bds09"].get_type() + if subtype == 0: + [velocity, heading, vert_spd, turnrate] = self.parseBDS09_0(data) + elif subtype == 1: + [velocity, heading, vert_spd] = self.parseBDS09_1(data) + turnrate = 0 else: return + self.velocities[icao24] = [velocity, heading, vert_spd, turnrate] except ADSBError: diff --git a/python/modes_parse.py b/python/modes_parse.py index f628af2..f1c3b61 100644 --- a/python/modes_parse.py +++ b/python/modes_parse.py @@ -58,7 +58,9 @@ class data_field: if len(bits) == 3: obj = bits[2](self.get_bits(bits[0], bits[1])) fields.update(obj.parse()) - fields.update({field: self.get_bits(bits[0], bits[1])}) + fields.update({field: obj}) + else: + fields.update({field: self.get_bits(bits[0], bits[1])}) else: raise NoHandlerError(mytype) return fields @@ -131,7 +133,7 @@ class me_reply(data_field): return 0x08 elif 5 <= ftc <= 8: return 0x06 - elif 9 <= ftc <= 18: + elif 9 <= ftc <= 18 and ftc != 15: #FTC 15 does not appear to be valid return 0x05 elif ftc == 19: return 0x09 diff --git a/python/modes_print.py b/python/modes_print.py index 4af3b9b..b8064e0 100644 --- a/python/modes_print.py +++ b/python/modes_print.py @@ -135,41 +135,39 @@ class modes_output_print(modes_parse.modes_parse): def print17(self, data): icao24 = data["aa"] - subtype = data["ftc"] + bdsreg = data["me"].get_type() retstr = None - if 1 <= subtype <= 4: + if bdsreg == 0x08: (msg, typestring) = self.parseBDS08(data) - retstr = "Type 17 subtype 04 (ident) from %x type %s ident %s" % (icao24, typestring, msg) + retstr = "Type 17 BDS0,8 (ident) from %x type %s ident %s" % (icao24, typestring, msg) - elif subtype >= 5 and subtype <= 8: + elif bdsreg == 0x06: [ground_track, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(data) - retstr = "Type 17 subtype 06 (surface report) from %x at (%.6f, %.6f) ground track %i" % (icao24, decoded_lat, decoded_lon, ground_track) + retstr = "Type 17 BDS0,6 (surface report) from %x at (%.6f, %.6f) ground track %i" % (icao24, decoded_lat, decoded_lon, ground_track) if rnge is not None and bearing is not None: retstr += " (%.2f @ %.0f)" % (rnge, bearing) - elif subtype >= 9 and subtype <= 18: + elif bdsreg == 0x05: [altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(data) - retstr = "Type 17 subtype 05 (position report) from %x at (%.6f, %.6f)" % (icao24, decoded_lat, decoded_lon) + retstr = "Type 17 BDS0,5 (position report) from %x at (%.6f, %.6f)" % (icao24, decoded_lat, decoded_lon) if rnge is not None and bearing is not None: retstr += " (" + "%.2f" % rnge + " @ " + "%.0f" % bearing + ")" retstr += " at " + str(altitude) + "ft" - elif subtype == 19: - subsubtype = data["sub"] - if subsubtype == 0: - [velocity, heading, vert_spd] = self.parseBDS09_0(data) - retstr = "Type 17 subtype 09-0 (track report) from %x with velocity %.0fkt heading %.0f VS %.0f" % (icao24, velocity, heading, vert_spd) - elif 1 <= subsubtype <= 2: - [velocity, heading, vert_spd] = self.parseBDS09_1(data) - retstr = "Type 17 subtype 09-%i (track report) from %x with velocity %.0fkt heading %.0f VS %.0f" % (subsubtype, icao24, velocity, heading, vert_spd) + elif bdsreg == 0x09: + subtype = data["bds09"].get_type() + if subtype == 0 or subtype == 1: + parser = self.parseBDS09_0 if subtype == 0 else self.parseBDS09_1 + [velocity, heading, vert_spd] = parser(data) + retstr = "Type 17 BDS0,9-%i (track report) from %x with velocity %.0fkt heading %.0f VS %.0f" % (subtype, icao24, velocity, heading, vert_spd) else: - retstr = "Type 17 subtype 09-%i from %x not implemented" % (subsubtype, icao24) + retstr = "Type 17 BDS0,9-%i from %x not implemented" % (subtype, icao24) - elif subtype == 28: + elif bdsreg == 0x62: emerg_str = self.parseBDS62(data) - retstr = "Type 17 subtype 28 (emergency) from %x type %s" % (icao24, emerg_str) + retstr = "Type 17 BDS6,2 (emergency) from %x type %s" % (icao24, emerg_str) else: retstr = "Type 17 subtype %i from %x not implemented" % (subtype, icao24) diff --git a/python/modes_sbs1.py b/python/modes_sbs1.py index 28e1430..3f16b13 100644 --- a/python/modes_sbs1.py +++ b/python/modes_sbs1.py @@ -159,19 +159,19 @@ class modes_output_sbs1(modes_parse.modes_parse): def pp17(self, data): icao24 = data["aa"] aircraft_id = self.get_aircraft_id(icao24) - subtype = data["ftc"] + bdsreg = data["me"].get_type() retstr = None #we'll get better timestamps later, hopefully with actual VRT time #in them [datestr, timestr] = self.current_time() - if subtype >= 1 and subtype <= 4: + if bdsreg == 0x08: # Aircraft Identification (msg, typestring) = self.parseBDS08(data) retstr = "MSG,1,0,%i,%06X,%i,%s,%s,%s,%s,%s,,,,,,,,,,,\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, msg) - elif subtype >= 5 and subtype <= 8: + elif bdsreg == 0x06: # Surface position measurement [ground_track, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(data) altitude = 0 @@ -180,7 +180,7 @@ class modes_output_sbs1(modes_parse.modes_parse): else: retstr = "MSG,2,0,%i,%06X,%i,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon) - elif subtype >= 9 and subtype <= 18: + elif bdsreg == 0x05: # Airborne position measurements # WRONG (rnge, bearing), is this still true? [altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(data) @@ -189,16 +189,13 @@ class modes_output_sbs1(modes_parse.modes_parse): else: retstr = "MSG,3,0,%i,%06X,%i,%s,%s,%s,%s,,%i,,,%.5f,%.5f,,,,0,0,0\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, altitude, decoded_lat, decoded_lon) - elif subtype == 19: + elif bdsreg == 0x09: # Airborne velocity measurements # WRONG (heading, vert_spd), Is this still true? - subsubtype = data["sub"] - if subsubtype == 0: - [velocity, heading, vert_spd] = self.parseBDS09_0(data) - retstr = "MSG,4,0,%i,%06X,%i,%s,%s,%s,%s,,,%.1f,%.1f,,,%i,,,,,\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, velocity, heading, vert_spd) - - elif 1 <= subsubtype <= 2: - [velocity, heading, vert_spd] = self.parseBDS09_1(data) + subtype = data["bds09"].get_type() + if subtype == 0 or subtype == 1: + parser = self.parseBDS09_0 if subtype == 0 else self.parseBDS09_1 + [velocity, heading, vert_spd] = parser(data) retstr = "MSG,4,0,%i,%06X,%i,%s,%s,%s,%s,,,%.1f,%.1f,,,%i,,,,,\n" % (aircraft_id, icao24, aircraft_id+100, datestr, timestr, datestr, timestr, velocity, heading, vert_spd) return retstr diff --git a/python/modes_sql.py b/python/modes_sql.py index 944bcbf..9cf272b 100644 --- a/python/modes_sql.py +++ b/python/modes_sql.py @@ -88,15 +88,15 @@ class modes_output_sql(modes_parse.modes_parse): def sql17(self, data): icao24 = data["aa"] - subtype = data["ftc"] + bdsreg = data["me"].get_type() retstr = None - if subtype == 4: + if bdsreg == 0x08: (msg, typename) = self.parseBDS08(data) retstr = "INSERT OR REPLACE INTO ident (icao, ident) VALUES (" + "%i" % icao24 + ", '" + msg + "')" - elif subtype >= 5 and subtype <= 8: + elif bdsreg == 0x06: [ground_track, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS06(data) altitude = 0 if decoded_lat is None: #no unambiguously valid position available @@ -104,21 +104,18 @@ class modes_output_sql(modes_parse.modes_parse): else: retstr = "INSERT INTO positions (icao, seen, alt, lat, lon) VALUES (" + "%i" % icao24 + ", datetime('now'), " + str(altitude) + ", " + "%.6f" % decoded_lat + ", " + "%.6f" % 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. + elif bdsreg == 0x05: [altitude, decoded_lat, decoded_lon, rnge, bearing] = self.parseBDS05(data) if decoded_lat is None: #no unambiguously valid position available retstr = None else: retstr = "INSERT INTO positions (icao, seen, alt, lat, lon) VALUES (" + "%i" % icao24 + ", datetime('now'), " + str(altitude) + ", " + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ")" - elif subtype == 19: - subsubtype = data["sub"] - if subsubtype == 0: - [velocity, heading, vert_spd] = self.parseBDS09_0(data) - retstr = "INSERT INTO vectors (icao, seen, speed, heading, vertical) VALUES (" + "%i" % icao24 + ", datetime('now'), " + "%.0f" % velocity + ", " + "%.0f" % heading + ", " + "%.0f" % vert_spd + ")"; - - elif 1 <= subsubtype <= 2: - [velocity, heading, vert_spd] = self.parseBDS09_1(data) - retstr = "INSERT INTO vectors (icao, seen, speed, heading, vertical) VALUES (" + "%i" % icao24 + ", datetime('now'), " + "%.0f" % velocity + ", " + "%.0f" % heading + ", " + "%.0f" % vert_spd + ")"; + elif bdsreg == 0x09: + subtype = data["bds09"].get_type() + if subtype == 0 or subtype == 1: + parser = self.parseBDS09_0 if subtype == 0 else self.parseBDS09_1 + [velocity, heading, vert_spd] = parser(data) + retstr = "INSERT INTO vectors (icao, seen, speed, heading, vertical) VALUES (" + "%i" % icao24 + ", datetime('now'), " + "%.0f" % velocity + ", " + "%.0f" % heading + ", " + "%.0f" % vert_spd + ")" return retstr From c2129e0eab7af92cbc2e6177bcc25742412ed8ef Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Mon, 16 Jul 2012 09:36:15 -0700 Subject: [PATCH 2/2] Default RTL gain of 35 and parser catches negative shifts generated by invalid packets. --- apps/uhd_modes.py | 3 ++- python/modes_parse.py | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/uhd_modes.py b/apps/uhd_modes.py index 6f271c7..2650bf1 100755 --- a/apps/uhd_modes.py +++ b/apps/uhd_modes.py @@ -29,6 +29,7 @@ from string import split, join import air_modes import gnuradio.gr.gr_threading as _threading import csv +from air_modes.modes_exceptions import * class top_block_runner(_threading.Thread): def __init__(self, tb): @@ -87,7 +88,7 @@ class adsb_rx_block (gr.top_block): self.u.set_gain_mode(0) #manual gain mode if options.gain is None: - options.gain = 49 + options.gain = 34 self.u.set_gain(options.gain) print "Gain is %i" % self.u.get_gain() diff --git a/python/modes_parse.py b/python/modes_parse.py index f1c3b61..3af9486 100644 --- a/python/modes_parse.py +++ b/python/modes_parse.py @@ -77,9 +77,16 @@ class data_field: def get_bits(self, *args): startbit = args[0] num = args[1] - bits = (self.data \ + try: + bits = (self.data \ >> (self.get_numbits() - startbit - num + self.offset)) \ & ((1 << num) - 1) + #the exception handler catches instances when you try to shift more than + #the number of bits. this can happen when a garbage packet gets through + #which reports itself as a short packet but of type long. + #TODO: should find more productive way to throw this out + except ValueError: + bits = 0 return bits class bds09_reply(data_field):