2010-10-20 01:05:33 +08:00
|
|
|
#!/usr/bin/env python
|
2010-10-19 00:59:08 +08:00
|
|
|
# Copyright 2010 Nick Foster
|
|
|
|
#
|
|
|
|
# This file is part of gr-air-modes
|
|
|
|
#
|
|
|
|
# gr-air-modes is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# gr-air-modes is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gr-air-modes; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
2012-06-14 11:14:57 +08:00
|
|
|
my_position = None
|
2010-10-24 09:00:58 +08:00
|
|
|
|
2011-12-15 02:17:16 +08:00
|
|
|
from gnuradio import gr, gru, optfir, eng_notation, blks2
|
2010-09-16 06:25:09 +08:00
|
|
|
from gnuradio.eng_option import eng_option
|
|
|
|
from optparse import OptionParser
|
2010-10-18 15:14:28 +08:00
|
|
|
import time, os, sys, threading
|
2010-09-16 06:25:09 +08:00
|
|
|
from string import split, join
|
2011-12-15 02:17:16 +08:00
|
|
|
import air_modes
|
2010-09-16 06:25:09 +08:00
|
|
|
import gnuradio.gr.gr_threading as _threading
|
2011-06-09 12:44:53 +08:00
|
|
|
import csv
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.exceptions import *
|
2010-09-16 06:25:09 +08:00
|
|
|
|
|
|
|
class top_block_runner(_threading.Thread):
|
|
|
|
def __init__(self, tb):
|
|
|
|
_threading.Thread.__init__(self)
|
|
|
|
self.setDaemon(1)
|
|
|
|
self.tb = tb
|
|
|
|
self.done = False
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.tb.run()
|
|
|
|
self.done = True
|
|
|
|
|
|
|
|
class adsb_rx_block (gr.top_block):
|
2010-10-18 10:45:19 +08:00
|
|
|
def __init__(self, options, args, queue):
|
|
|
|
gr.top_block.__init__(self)
|
|
|
|
|
|
|
|
self.options = options
|
|
|
|
self.args = args
|
2011-06-02 07:36:00 +08:00
|
|
|
rate = int(options.rate)
|
2012-06-14 07:04:59 +08:00
|
|
|
use_resampler = False
|
2012-12-19 11:23:52 +08:00
|
|
|
self.time_source = None
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-06-14 00:24:23 +08:00
|
|
|
if options.filename is None and options.udp is None and not options.rtlsdr:
|
|
|
|
#UHD source by default
|
|
|
|
from gnuradio import uhd
|
2012-07-18 05:30:40 +08:00
|
|
|
self.u = uhd.single_usrp_source(options.args, uhd.io_type_t.COMPLEX_FLOAT32, 1)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-12-19 11:23:52 +08:00
|
|
|
#check for GPSDO
|
|
|
|
#if you have a GPSDO, UHD will automatically set the timestamp to UTC time
|
|
|
|
#as well as automatically set the clock to lock to GPSDO.
|
|
|
|
if self.u.get_time_source(0) == 'gpsdo':
|
|
|
|
self.time_source = 'gpsdo'
|
|
|
|
else:
|
|
|
|
self.time_source = None
|
|
|
|
self.u.set_time_now(uhd.time_spec(0.0))
|
|
|
|
|
2011-07-03 07:44:38 +08:00
|
|
|
if not options.antenna is None:
|
|
|
|
self.u.set_antenna(options.antenna)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
self.u.set_samp_rate(rate)
|
|
|
|
rate = int(self.u.get_samp_rate()) #retrieve actual
|
|
|
|
|
|
|
|
if options.gain is None: #set to halfway
|
|
|
|
g = self.u.get_gain_range()
|
2010-11-19 06:25:07 +08:00
|
|
|
options.gain = (g.start()+g.stop()) / 2.0
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
if not(self.tune(options.freq)):
|
|
|
|
print "Failed to set initial frequency"
|
|
|
|
|
2012-06-14 00:24:23 +08:00
|
|
|
print "Setting gain to %i" % options.gain
|
2010-10-18 10:45:19 +08:00
|
|
|
self.u.set_gain(options.gain)
|
2012-06-14 00:24:23 +08:00
|
|
|
print "Gain is %i" % self.u.get_gain()
|
|
|
|
|
|
|
|
elif options.rtlsdr: #RTLSDR dongle
|
|
|
|
import osmosdr
|
2012-07-18 05:30:40 +08:00
|
|
|
self.u = osmosdr.source_c(options.args)
|
2012-08-24 10:21:09 +08:00
|
|
|
self.u.set_sample_rate(3.2e6) #fixed for RTL dongles
|
2012-06-14 00:24:23 +08:00
|
|
|
if not self.u.set_center_freq(options.freq):
|
|
|
|
print "Failed to set initial frequency"
|
|
|
|
|
|
|
|
self.u.set_gain_mode(0) #manual gain mode
|
2012-06-14 10:45:20 +08:00
|
|
|
if options.gain is None:
|
2012-07-17 00:36:15 +08:00
|
|
|
options.gain = 34
|
2012-06-14 10:45:20 +08:00
|
|
|
|
2012-06-14 00:24:23 +08:00
|
|
|
self.u.set_gain(options.gain)
|
|
|
|
print "Gain is %i" % self.u.get_gain()
|
2012-06-14 05:06:08 +08:00
|
|
|
|
2012-06-14 05:17:50 +08:00
|
|
|
use_resampler = True
|
2012-06-14 05:06:08 +08:00
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
else:
|
2012-06-13 22:49:22 +08:00
|
|
|
if options.filename is not None:
|
|
|
|
self.u = gr.file_source(gr.sizeof_gr_complex, options.filename)
|
2012-06-14 00:24:23 +08:00
|
|
|
elif options.udp is not None:
|
2012-06-13 22:49:22 +08:00
|
|
|
self.u = gr.udp_source(gr.sizeof_gr_complex, "localhost", options.udp)
|
2012-06-14 00:24:23 +08:00
|
|
|
else:
|
|
|
|
raise Exception("No valid source selected")
|
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
print "Rate is %i" % (rate,)
|
2010-11-21 08:34:44 +08:00
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
pass_all = 0
|
|
|
|
if options.output_all :
|
|
|
|
pass_all = 1
|
|
|
|
|
2012-10-28 02:28:06 +08:00
|
|
|
self.rx_path = air_modes.rx_path(rate, options.threshold, queue, options.pmf)
|
2012-06-14 05:06:08 +08:00
|
|
|
|
2012-06-14 05:17:50 +08:00
|
|
|
if use_resampler:
|
2012-08-24 10:21:09 +08:00
|
|
|
self.lpfiltcoeffs = gr.firdes.low_pass(1, 5*3.2e6, 1.6e6, 300e3)
|
|
|
|
self.resample = blks2.rational_resampler_ccf(interpolation=5, decimation=4, taps=self.lpfiltcoeffs)
|
2012-10-16 00:27:22 +08:00
|
|
|
self.connect(self.u, self.resample, self.rx_path)
|
2012-06-14 05:06:08 +08:00
|
|
|
else:
|
2012-10-16 00:27:22 +08:00
|
|
|
self.connect(self.u, self.rx_path)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
def tune(self, freq):
|
2010-11-19 06:25:07 +08:00
|
|
|
result = self.u.set_center_freq(freq, 0)
|
2010-10-18 10:45:19 +08:00
|
|
|
return result
|
2010-09-16 06:25:09 +08:00
|
|
|
|
2011-08-28 05:40:01 +08:00
|
|
|
def printraw(msg):
|
|
|
|
print msg
|
|
|
|
|
2012-12-19 11:23:52 +08:00
|
|
|
def printmlat(msg):
|
|
|
|
msglist = pickle.loads(msg)
|
|
|
|
for msg in msglist:
|
|
|
|
print "mlat message: %x at [%f, %f]" % (msg.data, msg.lat, msg.lon)
|
|
|
|
|
2010-09-16 06:25:09 +08:00
|
|
|
if __name__ == '__main__':
|
2010-10-18 10:45:19 +08:00
|
|
|
usage = "%prog: [options] output filename"
|
|
|
|
parser = OptionParser(option_class=eng_option, usage=usage)
|
|
|
|
parser.add_option("-R", "--rx-subdev-spec", type="string",
|
|
|
|
help="select USRP Rx side A or B", metavar="SUBDEV")
|
2011-07-03 07:44:38 +08:00
|
|
|
parser.add_option("-A", "--antenna", type="string",
|
|
|
|
help="select which antenna to use on daughterboard")
|
2012-07-18 05:30:40 +08:00
|
|
|
parser.add_option("-D", "--args", type="string",
|
|
|
|
help="arguments to pass to UHD/RTL constructor", default="")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-f", "--freq", type="eng_float", default=1090e6,
|
2010-09-16 06:25:09 +08:00
|
|
|
help="set receive frequency in Hz [default=%default]", metavar="FREQ")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-g", "--gain", type="int", default=None,
|
2010-09-16 06:25:09 +08:00
|
|
|
help="set RF gain", metavar="dB")
|
2011-06-02 07:36:00 +08:00
|
|
|
parser.add_option("-r", "--rate", type="eng_float", default=4000000,
|
2010-09-16 06:25:09 +08:00
|
|
|
help="set ADC sample rate [default=%default]")
|
2012-10-14 11:52:38 +08:00
|
|
|
parser.add_option("-T", "--threshold", type="eng_float", default=5.0,
|
2010-09-16 06:25:09 +08:00
|
|
|
help="set pulse detection threshold above noise in dB [default=%default]")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-a","--output-all", action="store_true", default=False,
|
2010-09-16 06:25:09 +08:00
|
|
|
help="output all frames")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-F","--filename", type="string", default=None,
|
|
|
|
help="read data from file instead of USRP")
|
2010-10-18 15:14:28 +08:00
|
|
|
parser.add_option("-K","--kml", type="string", default=None,
|
|
|
|
help="filename for Google Earth KML output")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-P","--sbs1", action="store_true", default=False,
|
|
|
|
help="open an SBS-1-compatible server on port 30003")
|
2011-07-28 01:59:04 +08:00
|
|
|
parser.add_option("-w","--raw", action="store_true", default=False,
|
|
|
|
help="open a server outputting raw timestamped data on port 9988")
|
2010-10-18 10:45:19 +08:00
|
|
|
parser.add_option("-n","--no-print", action="store_true", default=False,
|
|
|
|
help="disable printing decoded packets to stdout")
|
2011-06-09 12:44:53 +08:00
|
|
|
parser.add_option("-l","--location", type="string", default=None,
|
|
|
|
help="GPS coordinates of receiving station in format xx.xxxxx,xx.xxxxx")
|
2012-06-13 22:49:22 +08:00
|
|
|
parser.add_option("-u","--udp", type="int", default=None,
|
|
|
|
help="Use UDP source on specified port")
|
|
|
|
parser.add_option("-m","--multiplayer", type="string", default=None,
|
|
|
|
help="FlightGear server to send aircraft data, in format host:port")
|
2012-06-14 00:24:23 +08:00
|
|
|
parser.add_option("-d","--rtlsdr", action="store_true", default=False,
|
|
|
|
help="Use RTLSDR dongle instead of UHD source")
|
2012-10-28 02:28:06 +08:00
|
|
|
parser.add_option("-p","--pmf", action="store_true", default=False,
|
|
|
|
help="Use pulse matched filtering")
|
2012-12-19 11:23:52 +08:00
|
|
|
parser.add_option("-s","--mlat-server", type="string", default=None,
|
|
|
|
help="Use a multilateration server to track non-ADS-B aircraft")
|
2012-06-14 00:24:23 +08:00
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2011-06-09 12:44:53 +08:00
|
|
|
if options.location is not None:
|
2011-12-13 01:38:34 +08:00
|
|
|
reader = csv.reader([options.location], quoting=csv.QUOTE_NONNUMERIC)
|
2011-06-09 12:44:53 +08:00
|
|
|
my_position = reader.next()
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
queue = gr.msg_queue()
|
2012-12-19 11:23:52 +08:00
|
|
|
mlat_queue = None
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
outputs = [] #registry of plugin output functions
|
2012-12-19 11:23:52 +08:00
|
|
|
mlat_outputs = [] #registry of plugin mlat handling functions
|
2010-10-18 10:45:19 +08:00
|
|
|
updates = [] #registry of plugin update functions
|
|
|
|
|
2012-06-30 00:32:58 +08:00
|
|
|
if options.raw is True:
|
2012-07-17 10:27:09 +08:00
|
|
|
rawport = air_modes.raw_server(9988) #port
|
2012-06-30 00:32:58 +08:00
|
|
|
outputs.append(rawport.output)
|
|
|
|
outputs.append(printraw)
|
|
|
|
updates.append(rawport.add_pending_conns)
|
|
|
|
|
2010-10-18 15:14:28 +08:00
|
|
|
if options.kml is not None:
|
2012-01-31 10:50:04 +08:00
|
|
|
#we spawn a thread to run every 30 seconds (or whatever) to generate KML
|
2012-07-06 14:52:00 +08:00
|
|
|
dbname = 'adsb.db'
|
2012-10-10 00:26:16 +08:00
|
|
|
lock = threading.Lock()
|
|
|
|
sqldb = air_modes.output_sql(my_position, dbname, lock) #input into the db
|
|
|
|
kmlgen = air_modes.output_kml(options.kml, dbname, my_position, lock) #create a KML generating thread to read from the db
|
2012-07-06 14:52:00 +08:00
|
|
|
outputs.append(sqldb.output)
|
2010-10-20 01:05:33 +08:00
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
if options.sbs1 is True:
|
2012-07-17 10:27:09 +08:00
|
|
|
sbs1port = air_modes.output_sbs1(my_position, 30003)
|
2010-10-18 10:45:19 +08:00
|
|
|
outputs.append(sbs1port.output)
|
|
|
|
updates.append(sbs1port.add_pending_conns)
|
|
|
|
|
|
|
|
if options.no_print is not True:
|
2012-07-17 10:27:09 +08:00
|
|
|
outputs.append(air_modes.output_print(my_position).output)
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-06-13 22:49:22 +08:00
|
|
|
if options.multiplayer is not None:
|
|
|
|
[fghost, fgport] = options.multiplayer.split(':')
|
2012-07-17 10:27:09 +08:00
|
|
|
fgout = air_modes.output_flightgear(my_position, fghost, int(fgport))
|
2012-06-13 22:49:22 +08:00
|
|
|
outputs.append(fgout.output)
|
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
fg = adsb_rx_block(options, args, queue)
|
2012-12-19 11:23:52 +08:00
|
|
|
|
|
|
|
if options.mlat_server is not None:
|
|
|
|
mlat_queue = gr.msg_queue()
|
|
|
|
mlat_client = air_modes.mlat_client(mlat_queue, my_position, options.mlat_server, fg.time_source)
|
|
|
|
outputs.append(mlat_client.output) #output to server when we get a report
|
|
|
|
updates.append(mlat_client.get_mlat_positions) #check for replies from the server
|
|
|
|
#note: this means that get_mlat_positions and printmlat execute in the same thread.
|
|
|
|
#you could just have mlat_client spawn a thread to check its socket. might make more sense to do this.
|
|
|
|
mlat_outputs.append(printmlat)
|
|
|
|
|
2010-10-18 10:45:19 +08:00
|
|
|
runner = top_block_runner(fg)
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
try:
|
2012-12-19 11:23:52 +08:00
|
|
|
#handle the once-per-loop updates (check for mlat responses, add TCP conns to SBS-1 plugin, etc.)
|
2010-10-18 10:45:19 +08:00
|
|
|
for update in updates:
|
|
|
|
update()
|
|
|
|
|
|
|
|
#main message handler
|
2012-06-13 22:49:22 +08:00
|
|
|
if not queue.empty_p() :
|
|
|
|
while not queue.empty_p() :
|
2010-10-18 10:45:19 +08:00
|
|
|
msg = queue.delete_head() #blocking read
|
|
|
|
|
2010-10-25 07:13:44 +08:00
|
|
|
for out in outputs:
|
2012-07-02 22:17:21 +08:00
|
|
|
try:
|
|
|
|
out(msg.to_string())
|
2012-07-08 02:04:49 +08:00
|
|
|
except air_modes.ADSBError:
|
2012-07-02 22:17:21 +08:00
|
|
|
pass
|
2010-10-18 10:45:19 +08:00
|
|
|
|
|
|
|
elif runner.done:
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
else:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
fg.stop()
|
|
|
|
runner = None
|
2010-10-20 01:05:33 +08:00
|
|
|
if options.kml is not None:
|
|
|
|
kmlgen.done = True
|
2010-10-18 10:45:19 +08:00
|
|
|
break
|