Merged r10071:10164 from features/cppdb-test into trunk. Implements the fully native...
[debian/gnuradio] / gr-utils / src / python / usrp_siggen.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2007,2008 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, gru
24 from gnuradio import usrp
25 from gnuradio.eng_option import eng_option
26 from gnuradio import eng_notation
27 from optparse import OptionParser
28 import sys
29
30
31 class my_top_block(gr.top_block):
32     def __init__ (self):
33         gr.top_block.__init__(self)
34         
35         # controllable values
36         self.interp = 64
37         self.waveform_type = gr.GR_SIN_WAVE
38         self.waveform_ampl = 16000
39         self.waveform_freq = 100.12345e3
40         self.waveform_offset = 0
41         self._instantiate_blocks ()
42         self.set_waveform_type (self.waveform_type)
43
44     def usb_freq (self):
45         return self.u.dac_freq() / self.interp
46
47     def usb_throughput (self):
48         return self.usb_freq () * 4
49         
50     def set_waveform_type (self, type):
51         '''
52         valid waveform types are: gr.GR_SIN_WAVE, gr.GR_CONST_WAVE,
53         gr.GR_UNIFORM and gr.GR_GAUSSIAN
54         '''
55         self._configure_graph (type)
56         self.waveform_type = type
57
58     def set_waveform_ampl (self, ampl):
59         self.waveform_ampl = ampl
60         self.siggen.set_amplitude (ampl)
61         self.noisegen.set_amplitude (ampl)
62
63     def set_waveform_freq (self, freq):
64         self.waveform_freq = freq
65         self.siggen.set_frequency (freq)
66         
67     def set_waveform_offset (self, offset):
68         self.waveform_offset = offset
69         self.siggen.set_offset (offset)
70
71     def set_interpolator (self, interp):
72         self.interp = interp
73         self.siggen.set_sampling_freq (self.usb_freq ())
74         self.u.set_interp_rate (interp)
75
76     def _instantiate_blocks (self):
77         self.src = None
78         self.u = usrp.sink_c (0, self.interp)
79         
80         self.siggen = gr.sig_source_c (self.usb_freq (),
81                                        gr.GR_SIN_WAVE,
82                                        self.waveform_freq,
83                                        self.waveform_ampl,
84                                        self.waveform_offset)
85
86         self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
87                                            self.waveform_ampl)
88
89         # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
90
91     def _configure_graph (self, type):
92         try:
93             self.lock()
94             self.disconnect_all ()
95             if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
96                 self.connect (self.siggen, self.u)
97                 # self.connect (self.siggen, self.file_sink)
98                 self.siggen.set_waveform (type)
99                 self.src = self.siggen
100             elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
101                 self.connect (self.noisegen, self.u)
102                 self.noisegen.set_type (type)
103                 self.src = self.noisegen
104             else:
105                 raise ValueError, type
106         finally:
107             self.unlock()
108
109     def set_freq(self, target_freq):
110         """
111         Set the center frequency we're interested in.
112
113         @param target_freq: frequency in Hz
114         @rypte: bool
115
116         Tuning is a two step process.  First we ask the front-end to
117         tune as close to the desired frequency as it can.  Then we use
118         the result of that operation and our target_frequency to
119         determine the value for the digital up converter.
120         """
121         r = self.u.tune(self.subdev.which(), self.subdev, target_freq)
122         if r:
123             #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
124             #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
125             #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
126             #print "r.inverted      =", r.inverted
127             return True
128
129         return False
130
131
132
133 def main ():
134     parser = OptionParser (option_class=eng_option)
135     parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
136                        help="select USRP Tx side A or B")
137     parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
138                        help="set RF center frequency to FREQ")
139     parser.add_option ("-i", "--interp", type="int", default=64,
140                        help="set fgpa interpolation rate to INTERP [default=%default]")
141
142     parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
143                        help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
144     parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
145                        help="generate a constant output")
146     parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
147                        help="generate Gaussian random output")
148     parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
149                        help="generate Uniform random output")
150
151     parser.add_option ("-w", "--waveform-freq", type="eng_float", default=0,
152                        help="set waveform frequency to FREQ [default=%default]")
153     parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
154                        help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
155     parser.add_option ("-g", "--gain", type="eng_float", default=None,
156                        help="set output gain to GAIN [default=%default]")
157     parser.add_option ("-o", "--offset", type="eng_float", default=0,
158                        help="set waveform offset to OFFSET [default=%default]")
159     (options, args) = parser.parse_args ()
160
161     if len(args) != 0:
162         parser.print_help()
163         raise SystemExit
164
165     if options.rf_freq is None:
166         sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
167         parser.print_help()
168         raise SystemExit
169
170     tb = my_top_block()
171     tb.set_interpolator (options.interp)
172     tb.set_waveform_type (options.type)
173     tb.set_waveform_freq (options.waveform_freq)
174     tb.set_waveform_ampl (options.amplitude)
175     tb.set_waveform_offset (options.offset)
176
177     # determine the daughterboard subdevice we're using
178     if options.tx_subdev_spec is None:
179         options.tx_subdev_spec = usrp.pick_tx_subdevice(tb.u)
180
181     m = usrp.determine_tx_mux_value(tb.u, options.tx_subdev_spec)
182     #print "mux = %#04x" % (m,)
183     tb.u.set_mux(m)
184     tb.subdev = usrp.selected_subdev(tb.u, options.tx_subdev_spec)
185     print "Using TX d'board %s" % (tb.subdev.side_and_name(),)
186     
187     if options.gain is None:
188         tb.subdev.set_gain(tb.subdev.gain_range()[1])    # set max Tx gain
189     else:
190         tb.subdev.set_gain(options.gain)    # set max Tx gain
191
192     if not tb.set_freq(options.rf_freq):
193         sys.stderr.write('Failed to set RF frequency\n')
194         raise SystemExit
195     
196     tb.subdev.set_enable(True)                       # enable transmitter
197
198     try:
199         tb.run()
200     except KeyboardInterrupt:
201         pass
202
203
204 if __name__ == '__main__':
205     main ()