Merged r4767:4859 from developer branch jcorgan/channel, passes distcheck.
[debian/gnuradio] / gnuradio-examples / python / hier / sounder / usrp_sounder_tx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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 2, or (at your option)
10 # any later version.
11
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.
16
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.
21
22
23 from gnuradio import gr, eng_notation
24 from gnuradio.eng_option import eng_option
25 from optparse import OptionParser
26 from usrp_sink import usrp_sink_c
27 from sounder_tx import sounder_tx
28
29 n2s = eng_notation.num_to_str
30
31 class usrp_sounder_tx(gr.hier_block2):
32     def __init__(self, subdev_spec, freq, cal, verbose, degree, chip_rate, amplitude):
33
34         # Call hierarchical block constructor
35         # Top-level blocks have no inputs or outputs
36         gr.hier_block2.__init__(self,
37                                 "usrp_sounder_tx",      # Block typename
38                                 gr.io_signature(0,0,0), # Input signature
39                                 gr.io_signature(0,0,0)) # Output signature
40
41         self._freq = freq
42         self._cal = cal
43         self._verbose = verbose
44         self._degree = degree
45         self._length = 2**degree-1
46         self._amplitude = amplitude
47         
48         self._u = usrp_sink_c(0, subdev_spec, chip_rate, self._freq, self._cal, self._verbose)
49         self.define_component("usrp", self._u)
50         self._chip_rate = self._u._if_rate
51         self._max_time = float(self._length)/self._chip_rate
52         self.define_component("pn", sounder_tx(self._degree, self._chip_rate, self._verbose))
53         self.define_component("gain", gr.multiply_const_ff(amplitude));
54         self.define_component("f2c", gr.float_to_complex())
55         
56         if self._verbose:
57             print "Chip rate is", n2s(self._chip_rate), "chips/sec"
58             print "Resolution is", n2s(1.0/self._chip_rate), "sec"
59             print "Using PN code of degree", self._degree, "length", 2**self._degree-1
60             print "Maximum measurable impulse response is", n2s(self._max_time), "sec"
61             print "Output amplitude is", amplitude
62
63         # Ultimately this will be
64         # self.connect("pn gain f2c usrp")
65         self.connect("pn", 0, "gain", 0)
66         self.connect("gain", 0, "f2c", 0)
67         self.connect("f2c", 0, "usrp", 0)
68                               
69 def main():
70         parser = OptionParser(option_class=eng_option)
71
72         # Transmit path options
73         parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
74                           help="select USRP Rx side A or B (default=first found)")
75         parser.add_option("-f", "--freq", type="eng_float", default=0.0,
76                           help="set center frequency (default=%default)")
77         parser.add_option("-c", "--cal", type="eng_float", default=0.0,
78                           help="set frequency calibration offset (default=%default)")
79         parser.add_option("-v", "--verbose", action="store_true", default=False,
80                           help="print extra debugging info")
81         parser.add_option("-d", "--degree", type="int", default=10,
82                           help="set PN code degree (length=2**degree-1, default=%default)")
83         parser.add_option("-r", "--chip-rate", type="eng_float", default=8e6,
84                           help="set sounder chip rate (default=%default)")
85         parser.add_option("-g", "--amplitude", type="eng_float", default=8000.0,
86                           help="set output amplitude (default=%default)")
87         parser.add_option("", "--real-time", action="store_true", default=False,
88                           help="Attempt to enable real-time scheduling")
89         (options, args) = parser.parse_args()
90
91         if len(args) != 0:
92             parser.print_help()
93             sys.exit(1)
94
95         if not options.real_time:
96             realtime = False
97         else:
98             # Attempt to enable realtime scheduling
99             r = gr.enable_realtime_scheduling()
100             if r == gr.RT_OK:
101                 realtime = True
102             else:
103                 realtime = False
104                 print "Note: failed to enable realtime scheduling"
105
106         # Create an instance of a hierarchical block
107         top_block = usrp_sounder_tx(options.tx_subdev_spec, options.freq, options.cal,
108                                     options.verbose, options.degree, options.chip_rate,
109                                     options.amplitude)
110                               
111         # Create an instance of a runtime, passing it the top block
112         # to process
113         runtime = gr.runtime(top_block)
114
115         try:    
116             # Run forever
117             runtime.run()
118         except KeyboardInterrupt:
119             # Ctrl-C exits
120             pass
121
122 if __name__ == '__main__':
123     main ()