Fixed base class name.
[debian/gnuradio] / gnuradio-examples / python / hier / sounder / usrp_sink.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, usrp, eng_notation
24 n2s = eng_notation.num_to_str
25
26 # Hierarchical block implementing a USRP sink for complex floats,
27 # with convenience functions for tuning, interpolation, etc.
28 #
29 class usrp_sink_c(gr.hier_block2):
30     """
31     Create a USRP sink object accepting complex floats.
32     """
33     def __init__(self, which=0, subdev_spec=None, if_rate=None,
34                  freq=0.0, calibration=0.0, verbose=False):
35         # Call hierarchical block constructor
36         gr.hier_block2.__init__(self, 
37                                 "usrp_sink_c",                             # Block typename
38                                 gr.io_signature(1,1,gr.sizeof_gr_complex), # Input signature
39                                 gr.io_signature(0,0,0))                    # Output signature
40
41         self._verbose = verbose
42         self._u = usrp.sink_c(which)
43         if self._verbose:
44             print 'DAC sample rate is', n2s(self._u.dac_rate()), "sps"
45         self.set_subdev(subdev_spec)
46         self.set_if_rate(if_rate)
47         self.set_calibration(calibration)
48         self.tune(freq)
49         self.connect(self, self._u)
50
51     def set_subdev(self, subdev_spec):
52         if subdev_spec is None:
53             subdev_spec = self.pick_subdevice()
54         self._subdev = usrp.selected_subdev(self._u, subdev_spec)
55         self._u.set_mux(usrp.determine_tx_mux_value(self._u, subdev_spec))
56         if self._verbose:
57             print 'TX using', self._subdev.name(), 'daughterboard'
58
59     def pick_subdevice(self):
60         """
61         The user didn't specify a subdevice.
62         If there's a daughterboard on A, select A.
63         If there's a daughterboard on B, select B.
64         Otherwise, select A.
65         """
66         if self._u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
67             return (0, 0)
68         if self._u.db[1][0].dbid() >= 0:
69             return (1, 0)
70         return (0, 0)
71
72     def set_if_rate(self, if_rate):
73         # If no IF rate specified, set to maximum interpolation
74         if if_rate is None:
75             self._interp = 512
76         else:
77             self._interp = 4*int(self._u.dac_rate()/(4.0*if_rate)+0.5)
78             
79
80         self._if_rate = self._u.dac_rate()/self._interp
81         self._u.set_interp_rate(self._interp)
82
83         if self._verbose:
84             print "USRP interpolation rate is", self._interp
85             print "USRP IF rate is", n2s(self._if_rate), "sps"
86
87     def set_calibration(self, calibration):
88         self._cal = calibration
89         if self._verbose:
90             print "Using frequency calibration offset of", n2s(calibration), "Hz"
91
92     def tune(self, freq):
93         """
94         Set the center frequency we're interested in.
95
96         @param target_freq: frequency in Hz
97         @type: bool
98
99         Tuning is a two step process.  First we ask the front-end to
100         tune as close to the desired frequency as it can.  Then we use
101         the result of that operation and our target_frequency to
102         determine the value for the digital down converter.
103         """
104         self._tune_result = self._u.tune(self._subdev._which, self._subdev, freq+self._cal)
105         if self._tune_result:
106             if self._verbose:
107                 print "Baseband frequency is", n2s(self._tune_result.baseband_freq), "Hz"
108                 print "DXC frequency is", n2s(self._tune_result.dxc_freq), "Hz"
109                 print "Center frequency is", n2s(freq), "Hz"
110                 print "Residual frequency is", n2s(self._tune_result.residual_freq), "Hz"
111             return True
112         
113         return False
114
115 if __name__ == '__main__':
116     sink = usrp_sink_c(verbose=True)
117