Merged r4671:4680 from jcorgan/est into trunk. Pulls in gr-wxgui updates for new...
[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 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, 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
50         self.define_component("usrp", self._u)
51         self.connect("self", 0, "usrp", 0)
52
53
54     def set_subdev(self, subdev_spec):
55         if subdev_spec is None:
56             subdev_spec = self.pick_subdevice()
57         self._subdev = usrp.selected_subdev(self._u, subdev_spec)
58         self._u.set_mux(usrp.determine_tx_mux_value(self._u, subdev_spec))
59         if self._verbose:
60             print 'TX using', self._subdev.name(), 'daughterboard'
61
62     def pick_subdevice(self):
63         """
64         The user didn't specify a subdevice.
65         If there's a daughterboard on A, select A.
66         If there's a daughterboard on B, select B.
67         Otherwise, select A.
68         """
69         if self._u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
70             return (0, 0)
71         if self._u.db[1][0].dbid() >= 0:
72             return (1, 0)
73         return (0, 0)
74
75     def set_if_rate(self, if_rate):
76         # If no IF rate specified, set to maximum interpolation
77         if if_rate is None:
78             self._interp = 512
79         else:
80             self._interp = 4*int(self._u.dac_rate()/(4.0*if_rate)+0.5)
81             
82
83         self._if_rate = self._u.dac_rate()/self._interp
84         self._u.set_interp_rate(self._interp)
85
86         if self._verbose:
87             print "USRP interpolation rate is", self._interp
88             print "USRP IF rate is", n2s(self._if_rate), "sps"
89
90     def set_calibration(self, calibration):
91         self._cal = calibration
92         if self._verbose:
93             print "Using frequency calibration offset of", n2s(calibration), "Hz"
94
95     def tune(self, freq):
96         """
97         Set the center frequency we're interested in.
98
99         @param target_freq: frequency in Hz
100         @type: bool
101
102         Tuning is a two step process.  First we ask the front-end to
103         tune as close to the desired frequency as it can.  Then we use
104         the result of that operation and our target_frequency to
105         determine the value for the digital down converter.
106         """
107         self._tune_result = self._u.tune(self._subdev._which, self._subdev, freq+self._cal)
108         if self._tune_result:
109             if self._verbose:
110                 print "Baseband frequency is", n2s(self._tune_result.baseband_freq), "Hz"
111                 print "DXC frequency is", n2s(self._tune_result.dxc_freq), "Hz"
112                 print "Center frequency is", n2s(freq), "Hz"
113                 print "Residual frequency is", n2s(self._tune_result.residual_freq), "Hz"
114             return True
115         
116         return False
117
118 if __name__ == '__main__':
119     sink = usrp_sink_c(verbose=True)
120