Merge branch 'fll'
[debian/gnuradio] / gnuradio-examples / python / digital / usrp_transmit_path.py
1 #
2 # Copyright 2009 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio import gr
23 from gnuradio import usrp_options
24 import transmit_path
25 from pick_bitrate import pick_tx_bitrate
26 from gnuradio import eng_notation
27
28 def add_freq_option(parser):
29     """
30     Hackery that has the -f / --freq option set both tx_freq and rx_freq
31     """
32     def freq_callback(option, opt_str, value, parser):
33         parser.values.rx_freq = value
34         parser.values.tx_freq = value
35
36     if not parser.has_option('--freq'):
37         parser.add_option('-f', '--freq', type="eng_float",
38                           action="callback", callback=freq_callback,
39                           help="set Tx and/or Rx frequency to FREQ [default=%default]",
40                           metavar="FREQ")
41
42 def add_options(parser, expert):
43     add_freq_option(parser)
44     usrp_options.add_tx_options(parser)
45     transmit_path.transmit_path.add_options(parser, expert)
46     expert.add_option("", "--tx-freq", type="eng_float", default=None,
47                           help="set transmit frequency to FREQ [default=%default]", metavar="FREQ")
48     parser.add_option("-v", "--verbose", action="store_true", default=False)
49
50 class usrp_transmit_path(gr.hier_block2):
51     def __init__(self, modulator_class, options):
52         '''
53         See below for what options should hold
54         '''
55         gr.hier_block2.__init__(self, "usrp_transmit_path",
56                 gr.io_signature(0, 0, 0),                    # Input signature
57                 gr.io_signature(0, 0, 0)) # Output signature
58         if options.tx_freq is None:
59             sys.stderr.write("-f FREQ or --freq FREQ or --tx-freq FREQ must be specified\n")
60             raise SystemExit
61         tx_path = transmit_path.transmit_path(modulator_class, options)
62         for attr in dir(tx_path): #forward the methods
63             if not attr.startswith('_') and not hasattr(self, attr):
64                 setattr(self, attr, getattr(tx_path, attr))
65
66         #setup usrp
67         self._modulator_class = modulator_class
68         self._setup_usrp_sink(options)
69
70         # Set up resampler based on rate determined by _setup_usrp_sink
71         rs_taps = gr.firdes.low_pass_2(32, 32, 0.45, 0.1, 60)
72         self.resampler = gr.pfb_arb_resampler_ccf(self.rs_rate, rs_taps)
73
74         #connect
75         self.connect(tx_path, self.resampler, self.u)
76         
77     def _setup_usrp_sink(self, options):
78         """
79         Creates a USRP sink, determines the settings for best bitrate,
80         and attaches to the transmitter's subdevice.
81         """
82         self.u = usrp_options.create_usrp_sink(options)
83         dac_rate = self.u.dac_rate()
84         self.rs_rate = options.bitrate    # Store requested bit rate
85         if options.verbose:
86             print 'USRP Sink:', self.u
87         (self._bitrate, self._samples_per_symbol, self._interp) = \
88                         pick_tx_bitrate(options.bitrate, self._modulator_class.bits_per_symbol(), \
89                                         options.samples_per_symbol, options.interp, dac_rate, \
90                                         self.u.get_interp_rates())
91
92         # Calculate resampler rate based on requested and actual rates
93         self.rs_rate = self._bitrate / self.rs_rate
94         print "Resampling by %f to get bitrate of %ssps" % (self.rs_rate, eng_notation.num_to_str(self._bitrate/self.rs_rate))
95
96         self.u.set_interp(self._interp)
97         self.u.set_auto_tr(True)
98
99         if not self.u.set_center_freq(options.tx_freq):
100             print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.tx_freq))
101             raise ValueError, eng_notation.num_to_str(options.tx_freq)