3 # Copyright 2006, 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, blks2
24 from gnuradio import msdd
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
32 from receive_path import receive_path
34 class my_top_block(gr.top_block):
35 def __init__(self, address, callback, options):
36 gr.top_block.__init__(self)
38 self._address = address
39 self._rx_freq = options.rx_freq # receiver's center frequency
40 self._rx_gain = options.rx_gain # receiver's gain
41 self._decim = options.decim # Decimating rate for the USRP (prelim)
43 if self._rx_freq is None:
44 sys.stderr.write("-f FREQ or --freq FREQ or --rx-freq FREQ must be specified\n")
50 #taps = gr.firdes.low_pass(1, 1, 0.4, 0.2)
51 #self.resample = gr.rational_resampler_base_ccf(5, 8, taps)
52 self.resample = blks2.rational_resampler_ccf(5, 8)
55 self.rxpath = receive_path(callback, options)
57 self.connect(self.src, self.resample, self.rxpath)
58 #self.connect(self.src, gr.file_sink(gr.sizeof_gr_complex, "receive.dat"))
59 #self.connect(self.resample, gr.file_sink(gr.sizeof_gr_complex, "resampled.dat"))
61 def _setup_source(self):
64 self.src = msdd.source_c(0, 1, self._address, self._port)
65 self.src.set_decim_rate(self._decim)
66 self.src.set_desired_packet_size(0, 1460)
68 self.set_gain(self._rx_gain)
69 self.set_freq(self._rx_freq)
71 def set_freq(self, target_freq):
73 Set the center frequency we're interested in.
75 @param target_freq: frequency in Hz
78 r = self.src.set_rx_freq(0, target_freq)
83 def set_gain(self, gain):
85 Sets the analog gain in the USRP
87 return self.src.set_pga(0, gain)
92 def add_options(normal, expert):
94 Adds usrp-specific options to the Options Parser
96 add_freq_option(normal)
97 normal.add_option("", "--rx-gain", type="eng_float", default=32, metavar="GAIN",
98 help="set receiver gain in dB [default=%default].")
99 normal.add_option("-v", "--verbose", action="store_true", default=False)
101 expert.add_option("", "--rx-freq", type="eng_float", default=None,
102 help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
103 expert.add_option("-d", "--decim", type="intx", default=128,
104 help="set fpga decimation rate to DECIM [default=%default]")
105 expert.add_option("", "--snr", type="eng_float", default=30,
106 help="set the SNR of the channel in dB [default=%default]")
108 # Make a static method to call before instantiation
109 add_options = staticmethod(add_options)
111 def add_freq_option(parser):
113 Hackery that has the -f / --freq option set both tx_freq and rx_freq
115 def freq_callback(option, opt_str, value, parser):
116 parser.values.rx_freq = value
117 parser.values.tx_freq = value
119 if not parser.has_option('--freq'):
120 parser.add_option('-f', '--freq', type="eng_float",
121 action="callback", callback=freq_callback,
122 help="set Tx and/or Rx frequency to FREQ [default=%default]",
125 # /////////////////////////////////////////////////////////////////////////////
127 # /////////////////////////////////////////////////////////////////////////////
131 global n_rcvd, n_right
136 def rx_callback(ok, payload):
137 global n_rcvd, n_right
139 (pktno,) = struct.unpack('!H', payload[0:2])
142 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
146 for x in payload[2:]:
147 t = hex(ord(x)).replace('0x', '')
151 printable = ''.join(printlst)
156 usage = "usage: %prog [options] host"
157 parser = OptionParser(usage=usage, option_class=eng_option, conflict_handler="resolve")
158 expert_grp = parser.add_option_group("Expert")
159 parser.add_option("","--discontinuous", action="store_true", default=False,
160 help="enable discontinuous")
162 my_top_block.add_options(parser, expert_grp)
163 receive_path.add_options(parser, expert_grp)
164 blks2.ofdm_mod.add_options(parser, expert_grp)
165 blks2.ofdm_demod.add_options(parser, expert_grp)
167 (options, args) = parser.parse_args ()
171 tb = my_top_block(address, rx_callback, options)
173 #r = gr.enable_realtime_scheduling()
175 # print "Warning: failed to enable realtime scheduling"
177 tb.start() # start flow graph
178 tb.wait() # wait for it to finish
180 if __name__ == '__main__':
183 except KeyboardInterrupt: