Merge branch 'dfsg-orig'
[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
62         #setup usrp
63         self._modulator_class = modulator_class
64         self._setup_usrp_sink(options)
65
66         tx_path = transmit_path.transmit_path(modulator_class, options)
67         for attr in dir(tx_path): #forward the methods
68             if not attr.startswith('_') and not hasattr(self, attr):
69                 setattr(self, attr, getattr(tx_path, attr))
70
71         #connect
72         self.connect(tx_path, self.u)
73         
74     def _setup_usrp_sink(self, options):
75         """
76         Creates a USRP sink, determines the settings for best bitrate,
77         and attaches to the transmitter's subdevice.
78         """
79         self.u = usrp_options.create_usrp_sink(options)
80         dac_rate = self.u.dac_rate()
81         self.rs_rate = options.bitrate    # Store requested bit rate
82             
83         (self._bitrate, self._samples_per_symbol, self._interp) = \
84                         pick_tx_bitrate(options.bitrate, self._modulator_class.bits_per_symbol(),
85                                         options.samples_per_symbol, options.interp,
86                                         dac_rate, self.u.get_interp_rates())
87
88         options.interp = self._interp
89         options.samples_per_symbol = self._samples_per_symbol
90         options.bitrate = self._bitrate
91
92         if options.verbose:
93             print 'USRP Sink:', self.u
94             print "Interpolation Rate: ", self._interp
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)