summaryrefslogtreecommitdiff
path: root/gr-utils/src/python/usrp2_siggen.py
blob: 40506213e38ee9389c79579058f8ec0cfe1b8815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
#
# Copyright 2008 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 

from gnuradio import gr, eng_notation
from gnuradio import usrp2
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import sys

n2s = eng_notation.num_to_str

class siggen_top_block(gr.top_block):
    def __init__(self, options):
        gr.top_block.__init__(self)

        # Create a USRP2 sink with the requested interpolation rate
        self._u = usrp2.sink_32fc(options.interface, options.mac_addr)
        self._u.set_interp(options.interp)

        # Set the Tx daughterboard gain as requested
        if options.gain is None:
            g = self._u.gain_range()
            options.gain = float(g[0]+g[1])/2
        self._u.set_gain(options.gain)

        # Tune the USRP2 FPGA and daughterboard to the requested center frequency
        tr = self._u.set_center_freq(options.tx_freq)
        if tr == None:
            sys.stderr.write('Failed to set center frequency\n')
            raise SystemExit, 1

        eth_rate = self._u.dac_rate()/self._u.interp()
        
        # Create a source for the requested waveform type
        if options.type == gr.GR_SIN_WAVE or options.type == gr.GR_CONST_WAVE:
            self._src = gr.sig_source_c(eth_rate,              # Sample rate
                                        options.type,          # Waveform type
                                        options.waveform_freq, # Waveform frequency
                                        options.amplitude,     # Waveform amplitude
                                        options.offset)        # Waveform offset
            
        elif options.type == gr.GR_GAUSSIAN or options.type == gr.GR_UNIFORM:
            self._src = gr.noise_source_c(options.type, options.amplitude)
        else:
            sys.stderr.write('Unknown waveform type\n')
            raise SystemExit, 1

        if options.verbose:
            print "Network interface:", options.interface
            print "USRP2 address:", self._u.mac_addr()
            #print "Using TX d'board %s" % (self._u.tx_name(),)
            print "Tx gain:", options.gain
            print "Tx baseband frequency:", n2s(tr.baseband_freq), "Hz"
            print "Tx DDC frequency:", n2s(tr.dxc_freq), "Hz"
            print "Tx residual frequency:", n2s(tr.residual_freq), "Hz"
            print "Tx interpolation rate:", options.interp
            print "Tx GbE sample rate:", n2s(eth_rate), "samples/sec"
	    if options.type == gr.GR_SIN_WAVE:
		print "Baseband waveform type: Sine wave"
		print "Baseband waveform frequency:", n2s(options.waveform_freq), "Hz"
	    elif options.type == gr.GR_CONST_WAVE:
		print "Baseband waveform type: Constant"
	    elif options.type == gr.GR_GAUSSIAN:
		print "Baseband waveform type: Gaussian noise"
	    elif options.type == gr.GR_UNIFORM:
		print "Baseband waveform type: Uniform noise"		
			    
        # Wire the flowgraph
        self.connect(self._src, self._u)
            
def get_options():
    usage="%prog: [options]"

    parser = OptionParser(option_class=eng_option, usage=usage)
    parser.add_option("-e", "--interface", type="string", default="eth0",
                      help="use specified Ethernet interface [default=%default]")
    parser.add_option("-m", "--mac-addr", type="string", default="",
                      help="use USRP2 at specified MAC address [default=None]")  
    parser.add_option("-i", "--interp", type="int", default=16,
                      help="set fgpa decimation rate to DECIM [default=%default]")
    parser.add_option("-g", "--gain", type="eng_float", default=None,
                      help="set output gain to GAIN [default=%default]")
    parser.add_option("-f", "--tx-freq", type="eng_float", default=None,
                      help="set frequency to FREQ", metavar="FREQ")
    parser.add_option("-v", "--verbose", action="store_true", default=False,
                      help="verbose output")
    parser.add_option("-w", "--waveform-freq", type="eng_float", default=0,
                      help="set waveform frequency to FREQ [default=%default]")
    parser.add_option("-a", "--amplitude", type="eng_float", default=0.5,
                      help="set waveform amplitude to AMPLITUDE (0-1.0) [default=%default]", metavar="AMPL")
    parser.add_option("--offset", type="eng_float", default=0,
                      help="set waveform offset to OFFSET [default=%default]")
    parser.add_option("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
                      help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
    parser.add_option("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
                      help="generate a constant output")
    parser.add_option("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
                      help="generate Gaussian random output")
    parser.add_option("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
                      help="generate Uniform random output")

    (options, args) = parser.parse_args ()
    if len(args) != 0:
        parser.print_help()
        raise SystemExit, 1
    
    if options.tx_freq is None:
        parser.print_help()
        sys.stderr.write('You must specify the frequency with -f FREQ\n');
        raise SystemExit, 1
    
    return options


if __name__ == '__main__':
    options = get_options()
    tb = siggen_top_block(options)
    
    try:
        tb.run()
    except KeyboardInterrupt:
        pass