merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-examples / python / hier / usrp / usrp_siggen.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, gru
4 from gnuradio import usrp
5 from gnuradio.eng_option import eng_option
6 from gnuradio import eng_notation
7 from optparse import OptionParser
8 import sys
9
10
11 class my_graph(gr.hier_block2):
12     def __init__ (self, type, ampl, wfreq, offset, subdev_spec, interp, rf_freq):
13         # Call hierarchical block constructor
14         # Top-level blocks have no inputs or outputs
15         gr.hier_block2.__init__(self, 
16                                 "usrp_siggen",          # Block type 
17                                 gr.io_signature(0,0,0), # Input signature
18                                 gr.io_signature(0,0,0)) # Output signature
19         
20         # controllable values
21         self.interp = interp
22         self.waveform_type = type
23         self.waveform_ampl = ampl
24         self.waveform_freq = wfreq
25         self.waveform_offset = offset
26
27         self.u = usrp.sink_c (0, self.interp)
28
29         # determine the daughterboard subdevice we're using
30         if subdev_spec is None:
31             ubdev_spec = usrp.pick_tx_subdevice(self.u)
32         m = usrp.determine_tx_mux_value(self.u, subdev_spec)
33         self.u.set_mux(m)
34         self.subdev = usrp.selected_subdev(self.u, subdev_spec)
35         self.subdev.set_gain(self.subdev.gain_range()[1])    # set max Tx gain
36         self.subdev.set_enable(True)                         # enable transmitter
37         print "Using TX d'board %s" % (self.subdev.side_and_name(),)
38
39         if not self.set_freq(rf_freq):
40             sys.stderr.write('Failed to set RF frequency\n')
41             raise SystemExit
42
43         self.define_component("usrp", self.u)
44
45         if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
46             self.src = gr.sig_source_c (self.usb_freq (),
47                                         gr.GR_SIN_WAVE,
48                                         self.waveform_freq,
49                                         self.waveform_ampl,
50                                         self.waveform_offset)
51             self.define_component("src", self.src)
52
53         elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
54             self.src = gr.noise_source_c (gr.GR_UNIFORM,
55                                           self.waveform_ampl)
56             self.define_component("src", self.src)
57         
58         else:
59             raise ValueError, type
60
61         # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
62         # self.define_component("file_sink", self.file_sink)
63
64         self.connect ("src", 0, "usrp", 0)
65
66
67     def usb_freq (self):
68         return self.u.dac_freq() / self.interp
69
70     def usb_throughput (self):
71         return self.usb_freq () * 4
72         
73     def set_freq(self, target_freq):
74         """
75         Set the center frequency we're interested in.
76
77         @param target_freq: frequency in Hz
78         @rypte: bool
79
80         Tuning is a two step process.  First we ask the front-end to
81         tune as close to the desired frequency as it can.  Then we use
82         the result of that operation and our target_frequency to
83         determine the value for the digital up converter.
84         """
85         r = self.u.tune(self.subdev._which, self.subdev, target_freq)
86         if r:
87             #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
88             #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
89             #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
90             #print "r.inverted      =", r.inverted
91             return True
92
93         return False
94
95
96
97 def main ():
98     parser = OptionParser (option_class=eng_option)
99     parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
100                        help="select USRP Tx side A or B")
101     parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
102                        help="set RF center frequency to FREQ")
103     parser.add_option ("-i", "--interp", type="int", default=64,
104                        help="set fgpa interpolation rate to INTERP [default=%default]")
105
106     parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
107                        help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
108     parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
109                        help="generate a constant output")
110     parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
111                        help="generate Gaussian random output")
112     parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
113                        help="generate Uniform random output")
114
115     parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
116                        help="set waveform frequency to FREQ [default=%default]")
117     parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
118                        help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
119     parser.add_option ("-o", "--offset", type="eng_float", default=0,
120                        help="set waveform offset to OFFSET [default=%default]")
121     (options, args) = parser.parse_args ()
122
123     if len(args) != 0:
124         parser.print_help()
125         raise SystemExit
126
127     if options.rf_freq is None:
128         sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
129         parser.print_help()
130         raise SystemExit
131
132     top_block = my_graph(options.type, options.amplitude, options.waveform_freq, options.offset,
133                          options.tx_subdev_spec, options.interp, options.rf_freq)
134
135     runtime = gr.runtime(top_block)
136     
137     try:    
138         # Run forever
139         runtime.run()
140     except KeyboardInterrupt:
141         # Ctrl-C exits
142         pass
143
144 if __name__ == '__main__':
145     main ()