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