3 # Copyright 2005,2006 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, gru, blks
24 from gnuradio import usrp
25 from gnuradio import eng_notation
30 from pick_bitrate import pick_rx_bitrate
32 # /////////////////////////////////////////////////////////////////////////////
34 # /////////////////////////////////////////////////////////////////////////////
36 class receive_path(gr.hier_block):
37 def __init__(self, fg, demod_class, rx_callback, options):
39 options = copy.copy(options) # make a copy so we can destructively modify
41 self._verbose = options.verbose
42 self._rx_freq = options.rx_freq # receiver's center frequency
43 self._rx_gain = options.rx_gain # receiver's gain
44 self._rx_subdev_spec = options.rx_subdev_spec # daughterboard to use
45 self._bitrate = options.bitrate # desired bit rate
46 self._decim = options.decim # Decimating rate for the USRP (prelim)
47 self._samples_per_symbol = options.samples_per_symbol # desired samples/symbol
48 self._fusb_block_size = options.fusb_block_size # usb info for USRP
49 self._fusb_nblocks = options.fusb_nblocks # usb info for USRP
51 self._rx_callback = rx_callback # this callback is fired when there's a packet available
52 self._demod_class = demod_class # the demodulator_class we're using
54 if self._rx_freq is None:
55 sys.stderr.write("-f FREQ or --freq FREQ or --rx-freq FREQ must be specified\n")
58 # Set up USRP source; also adjusts decim, samples_per_symbol, and bitrate
59 self._setup_usrp_source()
61 # copy the final answers back into options for use by demodulator
62 options.samples_per_symbol = self._samples_per_symbol
63 options.bitrate = self._bitrate
64 options.decim = self._decim
67 demod_kwargs = self._demod_class.extract_kwargs_from_options(options)
69 # Design filter to get actual channel we want
71 chan_coeffs = gr.firdes.low_pass (1.0, # gain
72 sw_decim * self._samples_per_symbol, # sampling rate
73 1.0, # midpoint of trans. band
74 0.1, # width of trans. band
75 gr.firdes.WIN_HANN) # filter type
77 # Decimating channel filter
78 # complex in and out, float taps
79 self.chan_filt = gr.fft_filter_ccc(sw_decim, chan_coeffs)
80 #self.chan_filt = gr.fir_filter_ccf(sw_decim, chan_coeffs)
83 self.packet_receiver = \
85 self._demod_class(fg, **demod_kwargs),
87 callback=self._rx_callback,
90 ok = self.set_freq(self._rx_freq)
92 print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(self._rx_freq))
93 raise ValueError, eng_notation.num_to_str(self._rx_freq)
95 g = self.subdev.gain_range()
96 if options.show_rx_gain_range:
97 print "Rx Gain Range: minimum = %g, maximum = %g, step size = %g" \
100 self.set_gain(options.rx_gain)
102 self.set_auto_tr(True) # enable Auto Transmit/Receive switching
104 # Carrier Sensing Blocks
106 thresh = 30 # in dB, will have to adjust
107 self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
108 fg.connect(self.chan_filt, self.probe)
110 # Display some information about the setup
112 self._print_verbage()
114 fg.connect(self.u, self.chan_filt, self.packet_receiver)
115 gr.hier_block.__init__(self, fg, None, None)
117 def _setup_usrp_source(self):
118 self.u = usrp.source_c (fusb_block_size=self._fusb_block_size,
119 fusb_nblocks=self._fusb_nblocks)
120 adc_rate = self.u.adc_rate()
122 # derive values of bitrate, samples_per_symbol, and decim from desired info
123 (self._bitrate, self._samples_per_symbol, self._decim) = \
124 pick_rx_bitrate(self._bitrate, self._demod_class.bits_per_symbol(), \
125 self._samples_per_symbol, self._decim, adc_rate)
127 self.u.set_decim_rate(self._decim)
129 # determine the daughterboard subdevice we're using
130 if self._rx_subdev_spec is None:
131 self._rx_subdev_spec = usrp.pick_rx_subdevice(self.u)
132 self.subdev = usrp.selected_subdev(self.u, self._rx_subdev_spec)
134 self.u.set_mux(usrp.determine_rx_mux_value(self.u, self._rx_subdev_spec))
136 def set_freq(self, target_freq):
138 Set the center frequency we're interested in.
140 @param target_freq: frequency in Hz
143 Tuning is a two step process. First we ask the front-end to
144 tune as close to the desired frequency as it can. Then we use
145 the result of that operation and our target_frequency to
146 determine the value for the digital up converter.
148 r = self.u.tune(0, self.subdev, target_freq)
154 def set_gain(self, gain):
156 Sets the analog gain in the USRP
159 r = self.subdev.gain_range()
160 gain = (r[0] + r[1])/2 # set gain to midpoint
162 return self.subdev.set_gain(gain)
164 def set_auto_tr(self, enable):
165 return self.subdev.set_auto_tr(enable)
170 def samples_per_symbol(self):
171 return self._samples_per_symbol
176 def carrier_sensed(self):
178 Return True if we think carrier is present.
180 #return self.probe.level() > X
181 return self.probe.unmuted()
183 def carrier_threshold(self):
185 Return current setting in dB.
187 return self.probe.threshold()
189 def set_carrier_threshold(self, threshold_in_db):
191 Set carrier threshold.
193 @param threshold_in_db: set detection threshold
194 @type threshold_in_db: float (dB)
196 self.probe.set_threshold(threshold_in_db)
199 def add_options(normal, expert):
201 Adds receiver-specific options to the Options Parser
203 add_freq_option(normal)
204 if not normal.has_option("--bitrate"):
205 normal.add_option("-r", "--bitrate", type="eng_float", default=None,
206 help="specify bitrate. samples-per-symbol and interp/decim will be derived.")
207 normal.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
208 help="select USRP Rx side A or B")
209 normal.add_option("", "--rx-gain", type="eng_float", default=None, metavar="GAIN",
210 help="set receiver gain in dB [default=midpoint]. See also --show-rx-gain-range")
211 normal.add_option("", "--show-rx-gain-range", action="store_true", default=False,
212 help="print min and max Rx gain available on selected daughterboard")
213 normal.add_option("-v", "--verbose", action="store_true", default=False)
214 expert.add_option("-S", "--samples-per-symbol", type="int", default=None,
215 help="set samples/symbol [default=%default]")
216 expert.add_option("", "--rx-freq", type="eng_float", default=None,
217 help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
218 expert.add_option("-d", "--decim", type="intx", default=None,
219 help="set fpga decimation rate to DECIM [default=%default]")
220 expert.add_option("", "--log", action="store_true", default=False,
221 help="Log all parts of flow graph to files (CAUTION: lots of data)")
223 # Make a static method to call before instantiation
224 add_options = staticmethod(add_options)
227 def _print_verbage(self):
229 Prints information about the receive path
231 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
232 print "Rx gain: %g" % (self.gain,)
233 print "modulation: %s" % (self._demod_class.__name__)
234 print "bitrate: %sb/s" % (eng_notation.num_to_str(self._bitrate))
235 print "samples/symbol: %3d" % (self._samples_per_symbol)
236 print "decim: %3d" % (self._decim)
237 print "Rx Frequency: %s" % (eng_notation.num_to_str(self._rx_freq))
238 # print "Rx Frequency: %f" % (self._rx_freq)
240 def add_freq_option(parser):
242 Hackery that has the -f / --freq option set both tx_freq and rx_freq
244 def freq_callback(option, opt_str, value, parser):
245 parser.values.rx_freq = value
246 parser.values.tx_freq = value
248 if not parser.has_option('--freq'):
249 parser.add_option('-f', '--freq', type="eng_float",
250 action="callback", callback=freq_callback,
251 help="set Tx and/or Rx frequency to FREQ [default=%default]",