Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / usrp / fm_tx_2_daughterboards.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,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 """
24 Transmit 2 signals, one out each daughterboard.
25
26 Outputs SSB (USB) signals on side A and side B at frequencies
27 specified on command line.
28
29 Side A is 600 Hz tone.
30 Side B is 350 + 440 Hz tones.
31 """
32
33 from gnuradio import gr
34 from gnuradio.eng_notation import num_to_str, str_to_num
35 from gnuradio import usrp
36 from gnuradio import audio
37 from gnuradio import blks2
38 from gnuradio.eng_option import eng_option
39 from optparse import OptionParser
40 from usrpm import usrp_dbid
41 import math
42 import sys
43
44
45 class example_signal_0(gr.hier_block2):
46     """
47     Sinusoid at 600 Hz.
48     """
49     def __init__(self, sample_rate):
50         gr.hier_block2.__init__(self, "example_signal_0",
51                                 gr.io_signature(0, 0, 0),                    # Input signature
52                                 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
53
54         src = gr.sig_source_c (sample_rate,    # sample rate
55                                gr.GR_SIN_WAVE, # waveform type
56                                600,            # frequency
57                                1.0,            # amplitude
58                                0)              # DC Offset
59     
60         self.connect(src, self)
61
62
63 class example_signal_1(gr.hier_block2):
64     """
65     North American dial tone (350 + 440 Hz).
66     """
67     def __init__(self, sample_rate):
68         gr.hier_block2.__init__(self, "example_signal_1",
69                                 gr.io_signature(0, 0, 0),                    # Input signature
70                                 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
71
72         src0 = gr.sig_source_c (sample_rate,    # sample rate
73                                 gr.GR_SIN_WAVE, # waveform type
74                                 350,            # frequency
75                                 1.0,            # amplitude
76                                 0)              # DC Offset
77
78         src1 = gr.sig_source_c (sample_rate,    # sample rate
79                                 gr.GR_SIN_WAVE, # waveform type
80                                 440,            # frequency
81                                 1.0,            # amplitude
82                                 0)              # DC Offset
83         sum = gr.add_cc()
84         self.connect(src0, (sum, 0))
85         self.connect(src1, (sum, 1))
86         self.connect(sum, self)
87
88 class my_top_block(gr.top_block):
89
90     def __init__(self):
91         gr.top_block.__init__(self)
92
93         usage="%prog: [options] side-A-tx-freq side-B-tx-freq"
94         parser = OptionParser (option_class=eng_option, usage=usage)
95         (options, args) = parser.parse_args ()
96
97         if len(args) != 2:
98             parser.print_help()
99             raise SystemExit
100         else:
101             freq0 = str_to_num(args[0])
102             freq1 = str_to_num(args[1])
103
104         # ----------------------------------------------------------------
105         # Set up USRP to transmit on both daughterboards
106
107         self.u = usrp.sink_c(nchan=2)          # say we want two channels
108
109         self.dac_rate = self.u.dac_rate()                    # 128 MS/s
110         self.usrp_interp = 400
111         self.u.set_interp_rate(self.usrp_interp)
112         self.usrp_rate = self.dac_rate / self.usrp_interp    # 320 kS/s
113
114         # we're using both daughterboard slots, thus subdev is a 2-tuple
115         self.subdev = (self.u.db(0, 0), self.u.db(1, 0))
116         print "Using TX d'board %s" % (self.subdev[0].side_and_name(),)
117         print "Using TX d'board %s" % (self.subdev[1].side_and_name(),)
118         
119         # set up the Tx mux so that
120         #  channel 0 goes to Slot A I&Q and channel 1 to Slot B I&Q
121         self.u.set_mux(0xba98)
122
123         self.subdev[0].set_gain(self.subdev[0].gain_range()[1])    # set max Tx gain
124         self.subdev[1].set_gain(self.subdev[1].gain_range()[1])    # set max Tx gain
125
126         self.set_freq(0, freq0)
127         self.set_freq(1, freq1)
128         self.subdev[0].set_enable(True)             # enable transmitter
129         self.subdev[1].set_enable(True)             # enable transmitter
130
131         # ----------------------------------------------------------------
132         # build two signal sources, interleave them, amplify and connect them to usrp
133
134         sig0 = example_signal_0(self.usrp_rate)
135         sig1 = example_signal_1(self.usrp_rate)
136
137         intl = gr.interleave(gr.sizeof_gr_complex)
138         self.connect(sig0, (intl, 0))
139         self.connect(sig1, (intl, 1))
140
141         # apply some gain
142         if_gain = 10000
143         ifamp = gr.multiply_const_cc(if_gain)
144         
145         # and wire them up
146         self.connect(intl, ifamp, self.u)
147         
148
149     def set_freq(self, side, target_freq):
150         """
151         Set the center frequency we're interested in.
152
153         @param side: 0 = side A, 1 = side B
154         @param target_freq: frequency in Hz
155         @rtype: bool
156
157         Tuning is a two step process.  First we ask the front-end to
158         tune as close to the desired frequency as it can.  Then we use
159         the result of that operation and our target_frequency to
160         determine the value for the digital up converter.
161         """
162
163         print "Tuning side %s to %sHz" % (("A", "B")[side], num_to_str(target_freq))
164         r = self.u.tune(self.subdev[side].which(), self.subdev[side], target_freq)
165         if r:
166             print "  r.baseband_freq =", num_to_str(r.baseband_freq)
167             print "  r.dxc_freq      =", num_to_str(r.dxc_freq)
168             print "  r.residual_freq =", num_to_str(r.residual_freq)
169             print "  r.inverted      =", r.inverted
170             print "  OK"
171             return True
172
173         else:
174             print "  Failed!"
175             
176         return False
177
178
179 if __name__ == '__main__':
180     try:
181         my_top_block().run()
182     except KeyboardInterrupt:
183         pass