2013-05-28 10:50:42 +08:00
|
|
|
# Copyright 2013 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Radio interface for Mode S RX.
|
2013-06-09 03:32:24 +08:00
|
|
|
# Handles all hardware- and source-related functionality
|
2013-05-28 10:50:42 +08:00
|
|
|
# You pass it options, it gives you data.
|
|
|
|
# It uses the pubsub interface to allow clients to subscribe to its data feeds.
|
|
|
|
|
2013-08-05 11:10:35 +08:00
|
|
|
from gnuradio import gr, gru, eng_notation, filter, blocks
|
2013-07-18 09:03:45 +08:00
|
|
|
from gnuradio.filter import optfir
|
2013-05-28 10:50:42 +08:00
|
|
|
from gnuradio.eng_option import eng_option
|
2013-06-06 03:56:09 +08:00
|
|
|
from gnuradio.gr.pubsub import pubsub
|
2014-09-22 10:03:16 +08:00
|
|
|
from gnuradio.filter import pfb
|
2013-06-09 04:12:38 +08:00
|
|
|
from optparse import OptionParser, OptionGroup
|
2013-05-28 10:50:42 +08:00
|
|
|
import air_modes
|
2013-05-30 05:18:15 +08:00
|
|
|
import zmq
|
|
|
|
import threading
|
|
|
|
import time
|
2013-06-09 03:32:24 +08:00
|
|
|
import re
|
2013-05-28 10:50:42 +08:00
|
|
|
|
2013-06-06 03:56:09 +08:00
|
|
|
class modes_radio (gr.top_block, pubsub):
|
2013-05-30 05:18:15 +08:00
|
|
|
def __init__(self, options, context):
|
2013-05-28 10:50:42 +08:00
|
|
|
gr.top_block.__init__(self)
|
2013-06-06 03:56:09 +08:00
|
|
|
pubsub.__init__(self)
|
2013-05-28 10:50:42 +08:00
|
|
|
self._options = options
|
|
|
|
self._queue = gr.msg_queue()
|
2013-06-09 03:24:23 +08:00
|
|
|
self._rate = int(options.rate)
|
2013-05-28 10:50:42 +08:00
|
|
|
|
2013-06-09 03:24:23 +08:00
|
|
|
self._resample = None
|
2013-05-28 10:50:42 +08:00
|
|
|
self._setup_source(options)
|
|
|
|
|
2014-09-22 10:03:16 +08:00
|
|
|
if self._rate < 4e6:
|
|
|
|
self._resample = pfb.arb_resampler_ccf(4.e6/self._rate)
|
|
|
|
self._rx_rate = 4e6
|
|
|
|
else:
|
|
|
|
self._rx_rate = self._rate
|
|
|
|
|
|
|
|
self._rx_path = air_modes.rx_path(self._rx_rate, options.threshold,
|
2013-08-10 06:41:26 +08:00
|
|
|
self._queue, options.pmf, options.dcblock)
|
2013-05-28 10:50:42 +08:00
|
|
|
|
2014-09-22 10:03:16 +08:00
|
|
|
|
2013-06-06 03:56:09 +08:00
|
|
|
#now subscribe to set various options via pubsub
|
2013-06-09 03:24:23 +08:00
|
|
|
self.subscribe("freq", self.set_freq)
|
|
|
|
self.subscribe("gain", self.set_gain)
|
|
|
|
self.subscribe("rate", self.set_rate)
|
|
|
|
self.subscribe("rate", self._rx_path.set_rate)
|
|
|
|
self.subscribe("threshold", self._rx_path.set_threshold)
|
|
|
|
self.subscribe("pmf", self._rx_path.set_pmf)
|
|
|
|
|
|
|
|
self.publish("freq", self.get_freq)
|
|
|
|
self.publish("gain", self.get_gain)
|
|
|
|
self.publish("rate", self.get_rate)
|
|
|
|
self.publish("threshold", self._rx_path.get_threshold)
|
|
|
|
self.publish("pmf", self._rx_path.get_pmf)
|
|
|
|
|
|
|
|
if self._resample is not None:
|
|
|
|
self.connect(self._u, self._resample, self._rx_path)
|
2013-05-28 10:50:42 +08:00
|
|
|
else:
|
2013-06-09 03:24:23 +08:00
|
|
|
self.connect(self._u, self._rx_path)
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
#Publish messages when they come back off the queue
|
2013-06-06 03:56:09 +08:00
|
|
|
server_addr = ["inproc://modes-radio-pub"]
|
|
|
|
if options.tcp is not None:
|
2013-06-10 23:57:12 +08:00
|
|
|
server_addr += ["tcp://*:%i" % options.tcp]
|
2013-06-06 03:56:09 +08:00
|
|
|
|
|
|
|
self._sender = air_modes.zmq_pubsub_iface(context, subaddr=None, pubaddr=server_addr)
|
2013-06-03 20:38:26 +08:00
|
|
|
self._async_sender = gru.msgq_runner(self._queue, self.send)
|
|
|
|
|
2013-06-03 21:29:08 +08:00
|
|
|
def send(self, msg):
|
|
|
|
self._sender["dl_data"] = msg.to_string()
|
2013-05-30 15:58:03 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_radio_options(parser):
|
2013-06-09 04:12:38 +08:00
|
|
|
group = OptionGroup(parser, "Receiver setup options")
|
2013-10-27 12:53:49 +08:00
|
|
|
|
2013-06-09 03:24:23 +08:00
|
|
|
#Choose source
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-s","--source", type="string", default="uhd",
|
2013-06-09 04:48:14 +08:00
|
|
|
help="Choose source: uhd, osmocom, <filename>, or <ip:port> [default=%default]")
|
2013-06-10 23:57:12 +08:00
|
|
|
group.add_option("-t","--tcp", type="int", default=None, metavar="PORT",
|
2013-06-10 20:52:11 +08:00
|
|
|
help="Open a TCP server on this port to publish reports")
|
2013-06-09 03:24:23 +08:00
|
|
|
|
|
|
|
#UHD/Osmocom args
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-R", "--subdev", type="string",
|
2013-05-30 15:58:03 +08:00
|
|
|
help="select USRP Rx side A or B", metavar="SUBDEV")
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-A", "--antenna", type="string",
|
2013-05-30 15:58:03 +08:00
|
|
|
help="select which antenna to use on daughterboard")
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-D", "--args", type="string",
|
2013-05-30 15:58:03 +08:00
|
|
|
help="arguments to pass to radio constructor", default="")
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-f", "--freq", type="eng_float", default=1090e6,
|
2013-05-30 15:58:03 +08:00
|
|
|
help="set receive frequency in Hz [default=%default]", metavar="FREQ")
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-g", "--gain", type="int", default=None,
|
2013-05-30 15:58:03 +08:00
|
|
|
help="set RF gain", metavar="dB")
|
2013-06-09 03:24:23 +08:00
|
|
|
|
|
|
|
#RX path args
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-r", "--rate", type="eng_float", default=4e6,
|
2013-06-09 03:24:23 +08:00
|
|
|
help="set sample rate [default=%default]")
|
2013-08-09 13:15:24 +08:00
|
|
|
group.add_option("-T", "--threshold", type="eng_float", default=7.0,
|
2013-05-30 15:58:03 +08:00
|
|
|
help="set pulse detection threshold above noise in dB [default=%default]")
|
2013-06-09 04:12:38 +08:00
|
|
|
group.add_option("-p","--pmf", action="store_true", default=False,
|
|
|
|
help="Use pulse matched filtering [default=%default]")
|
2013-08-10 06:41:26 +08:00
|
|
|
group.add_option("-d","--dcblock", action="store_true", default=False,
|
|
|
|
help="Use a DC blocking filter (best for HackRF Jawbreaker) [default=%default]")
|
2013-06-09 04:12:38 +08:00
|
|
|
|
|
|
|
parser.add_option_group(group)
|
2013-05-30 15:58:03 +08:00
|
|
|
|
2013-06-09 03:24:23 +08:00
|
|
|
def live_source(self):
|
2013-08-06 08:06:22 +08:00
|
|
|
return self._options.source=="uhd" or self._options.source=="osmocom"
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
def set_freq(self, freq):
|
2013-08-06 07:26:22 +08:00
|
|
|
return self._u.set_center_freq(freq, 0) if self.live_source() else 0
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
def set_gain(self, gain):
|
2013-08-06 08:06:22 +08:00
|
|
|
if self.live_source():
|
|
|
|
self._u.set_gain(gain)
|
|
|
|
print "Gain is %f" % self.get_gain()
|
|
|
|
return self.get_gain()
|
2013-06-06 03:56:09 +08:00
|
|
|
|
|
|
|
def set_rate(self, rate):
|
2014-09-22 10:03:16 +08:00
|
|
|
if(rate < 4e6 and self._rate > 4e6):
|
|
|
|
raise NotImplementedError("Lowering rate <4e6Msps not currently supported.")
|
|
|
|
if(rate < 4e6):
|
|
|
|
self._resample.set_rate(4e6/rate)
|
|
|
|
self._rx_rate = 4e6
|
|
|
|
else:
|
|
|
|
self._rx_rate = rate
|
|
|
|
self._rx_path.set_rate(self._rx_rate)
|
2014-09-22 10:28:44 +08:00
|
|
|
if self._options.source in ("osmocom"):
|
|
|
|
return self._u.set_sample_rate(rate)
|
|
|
|
if self._options.source in ("uhd"):
|
|
|
|
return self._u.set_rate(rate)
|
|
|
|
else:
|
|
|
|
return 0
|
2013-08-06 07:26:22 +08:00
|
|
|
|
|
|
|
def set_threshold(self, threshold):
|
|
|
|
self._rx_path.set_threshold(threshold)
|
2013-06-06 03:56:09 +08:00
|
|
|
|
|
|
|
def get_freq(self, freq):
|
2013-08-06 08:06:22 +08:00
|
|
|
return self._u.get_center_freq(freq, 0) if self.live_source() else 1090e6
|
2013-08-09 13:15:24 +08:00
|
|
|
|
2013-08-06 08:06:22 +08:00
|
|
|
def get_gain(self):
|
|
|
|
return self._u.get_gain() if self.live_source() else 0
|
2013-06-06 03:56:09 +08:00
|
|
|
|
2013-08-06 08:06:22 +08:00
|
|
|
def get_rate(self):
|
|
|
|
return self._u.get_rate() if self.live_source() else self._rate
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
def _setup_source(self, options):
|
2013-06-09 03:24:23 +08:00
|
|
|
if options.source == "uhd":
|
2013-05-28 10:50:42 +08:00
|
|
|
#UHD source by default
|
|
|
|
from gnuradio import uhd
|
|
|
|
self._u = uhd.single_usrp_source(options.args, uhd.io_type_t.COMPLEX_FLOAT32, 1)
|
|
|
|
|
|
|
|
if(options.subdev):
|
|
|
|
self._u.set_subdev_spec(options.subdev, 0)
|
|
|
|
|
2013-05-30 15:58:03 +08:00
|
|
|
if not self._u.set_center_freq(options.freq):
|
|
|
|
print "Failed to set initial frequency"
|
|
|
|
|
2013-05-28 10:50:42 +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.
|
2013-06-09 03:24:23 +08:00
|
|
|
if self._u.get_time_source(0) != 'gpsdo':
|
2013-05-28 10:50:42 +08:00
|
|
|
self._u.set_time_now(uhd.time_spec(0.0))
|
|
|
|
|
|
|
|
if options.antenna is not None:
|
|
|
|
self._u.set_antenna(options.antenna)
|
|
|
|
|
2013-05-30 05:18:15 +08:00
|
|
|
self._u.set_samp_rate(options.rate)
|
|
|
|
options.rate = int(self._u.get_samp_rate()) #retrieve actual
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
if options.gain is None: #set to halfway
|
|
|
|
g = self._u.get_gain_range()
|
|
|
|
options.gain = (g.start()+g.stop()) / 2.0
|
|
|
|
|
|
|
|
print "Setting gain to %i" % options.gain
|
|
|
|
self._u.set_gain(options.gain)
|
|
|
|
print "Gain is %i" % self._u.get_gain()
|
2013-05-30 05:18:15 +08:00
|
|
|
|
|
|
|
#TODO: detect if you're using an RTLSDR or Jawbreaker
|
|
|
|
#and set up accordingly.
|
2013-06-09 03:24:23 +08:00
|
|
|
elif options.source == "osmocom": #RTLSDR dongle or HackRF Jawbreaker
|
2013-05-28 10:50:42 +08:00
|
|
|
import osmosdr
|
2013-08-09 13:15:24 +08:00
|
|
|
self._u = osmosdr.source(options.args)
|
2013-05-30 05:18:15 +08:00
|
|
|
# self._u.set_sample_rate(3.2e6) #fixed for RTL dongles
|
|
|
|
self._u.set_sample_rate(options.rate)
|
2013-05-28 10:50:42 +08:00
|
|
|
if not self._u.set_center_freq(options.freq):
|
|
|
|
print "Failed to set initial frequency"
|
|
|
|
|
2013-08-10 07:21:58 +08:00
|
|
|
# self._u.set_gain_mode(0) #manual gain mode
|
2013-05-28 10:50:42 +08:00
|
|
|
if options.gain is None:
|
|
|
|
options.gain = 34
|
2013-08-10 07:21:58 +08:00
|
|
|
self._u.set_gain(options.gain)
|
2013-05-28 10:50:42 +08:00
|
|
|
print "Gain is %i" % self._u.get_gain()
|
|
|
|
|
2013-06-09 03:24:23 +08:00
|
|
|
#Note: this should only come into play if using an RTLSDR.
|
2013-06-10 23:24:42 +08:00
|
|
|
# lpfiltcoeffs = gr.firdes.low_pass(1, 5*3.2e6, 1.6e6, 300e3)
|
2013-07-18 09:03:45 +08:00
|
|
|
# self._resample = filter.rational_resampler_ccf(interpolation=5, decimation=4, taps=lpfiltcoeffs)
|
2013-08-10 07:21:58 +08:00
|
|
|
|
2013-05-28 10:50:42 +08:00
|
|
|
else:
|
2013-06-09 03:24:23 +08:00
|
|
|
#semantically detect whether it's ip.ip.ip.ip:port or filename
|
|
|
|
if ':' in options.source:
|
|
|
|
try:
|
|
|
|
ip, port = re.search("(.*)\:(\d{1,5})", options.source).groups()
|
|
|
|
except:
|
|
|
|
raise Exception("Please input UDP source e.g. 192.168.10.1:12345")
|
|
|
|
self._u = gr.udp_source(gr.sizeof_gr_complex, ip, int(port))
|
|
|
|
print "Using UDP source %s:%s" % (ip, port)
|
2013-05-28 10:50:42 +08:00
|
|
|
else:
|
2013-08-05 11:10:35 +08:00
|
|
|
self._u = blocks.file_source(gr.sizeof_gr_complex, options.source)
|
2013-06-09 03:24:23 +08:00
|
|
|
print "Using file source %s" % options.source
|
2013-05-28 10:50:42 +08:00
|
|
|
|
|
|
|
print "Rate is %i" % (options.rate,)
|
2013-05-30 16:06:53 +08:00
|
|
|
|
2013-06-10 23:24:42 +08:00
|
|
|
def close(self):
|
2014-09-29 01:43:54 +08:00
|
|
|
self.stop()
|
|
|
|
self.wait()
|
2013-06-03 20:38:26 +08:00
|
|
|
self._sender.close()
|
2013-08-09 13:15:24 +08:00
|
|
|
self._u = None
|