Imported Upstream version 3.2.2
[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 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         #setup usrp
66         self._modulator_class = modulator_class
67         self._setup_usrp_sink(options)
68         #connect
69         self.connect(tx_path, self.u)
70
71     def _setup_usrp_sink(self, options):
72         """
73         Creates a USRP sink, determines the settings for best bitrate,
74         and attaches to the transmitter's subdevice.
75         """
76         self.u = usrp_options.create_usrp_sink(options)
77         dac_rate = self.u.dac_rate()
78         if options.verbose:
79             print 'USRP Sink:', self.u
80         (self._bitrate, self._samples_per_symbol, self._interp) = \
81                         pick_tx_bitrate(options.bitrate, self._modulator_class.bits_per_symbol(), \
82                                         options.samples_per_symbol, options.interp, dac_rate, \
83                                         self.u.get_interp_rates())
84
85         self.u.set_interp(self._interp)
86         self.u.set_auto_tr(True)
87
88         if not self.u.set_center_freq(options.tx_freq):
89             print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.tx_freq))
90             raise ValueError, eng_notation.num_to_str(options.tx_freq)