Merge r6461:6464 from jcorgan/t162-staging into trunk.
[debian/gnuradio] / gnuradio-examples / python / limbo / 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 3, 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.top_block):
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.top_block.__init__(self, "usrp_sounder_tx")
37         self._freq = freq
38         self._cal = cal
39         self._verbose = verbose
40         self._degree = degree
41         self._length = 2**degree-1
42         self._amplitude = amplitude
43         
44         self._u = usrp_sink_c(0, subdev_spec, chip_rate, self._freq, self._cal, self._verbose)
45         self._chip_rate = self._u._if_rate
46         self._max_time = float(self._length)/self._chip_rate
47         self._pn = sounder_tx(self._degree, self._chip_rate, self._verbose)
48         self._gain = gr.multiply_const_ff(amplitude)
49         self._f2c = gr.float_to_complex()
50         self.connect(self._pn, self._gain, self._f2c, self._u)
51         
52         if self._verbose:
53             print "Chip rate is", n2s(self._chip_rate), "chips/sec"
54             print "Resolution is", n2s(1.0/self._chip_rate), "sec"
55             print "Using PN code of degree", self._degree, "length", 2**self._degree-1
56             print "Maximum measurable impulse response is", n2s(self._max_time), "sec"
57             print "Output amplitude is", amplitude
58
59                               
60 def main():
61         parser = OptionParser(option_class=eng_option)
62
63         # Transmit path options
64         parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
65                           help="select USRP Rx side A or B (default=first found)")
66         parser.add_option("-f", "--freq", type="eng_float", default=0.0,
67                           help="set center frequency (default=%default)")
68         parser.add_option("-c", "--cal", type="eng_float", default=0.0,
69                           help="set frequency calibration offset (default=%default)")
70         parser.add_option("-v", "--verbose", action="store_true", default=False,
71                           help="print extra debugging info")
72         parser.add_option("-d", "--degree", type="int", default=10,
73                           help="set PN code degree (length=2**degree-1, default=%default)")
74         parser.add_option("-r", "--chip-rate", type="eng_float", default=8e6,
75                           help="set sounder chip rate (default=%default)")
76         parser.add_option("-g", "--amplitude", type="eng_float", default=8000.0,
77                           help="set output amplitude (default=%default)")
78         parser.add_option("", "--real-time", action="store_true", default=False,
79                           help="Attempt to enable real-time scheduling")
80         (options, args) = parser.parse_args()
81
82         if len(args) != 0:
83             parser.print_help()
84             sys.exit(1)
85
86         if not options.real_time:
87             realtime = False
88         else:
89             # Attempt to enable realtime scheduling
90             r = gr.enable_realtime_scheduling()
91             if r == gr.RT_OK:
92                 realtime = True
93             else:
94                 realtime = False
95                 print "Note: failed to enable realtime scheduling"
96
97         # Create an instance of a hierarchical block
98         top_block = usrp_sounder_tx(options.tx_subdev_spec, options.freq, options.cal,
99                                     options.verbose, options.degree, options.chip_rate,
100                                     options.amplitude)
101                               
102         try:    
103             # Run forever
104             top_block.run()
105         except KeyboardInterrupt:
106             # Ctrl-C exits
107             pass
108
109 if __name__ == '__main__':
110     main ()