~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to gr-gpio/src/python/gpio_usrp_siggen.py

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 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
from gnuradio import gr, gru
 
23
from gnuradio import usrp
 
24
from gnuradio.eng_option import eng_option
 
25
from gnuradio import eng_notation
 
26
from optparse import OptionParser
 
27
import sys
 
28
 
 
29
from gnuradio import gpio
 
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_CONST_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,fpga_filename=gpio.fpga_filename)
 
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
        self.vecgen = gr.vector_source_c ([complex(1.0,0.0),complex(0.0,0.0),complex(1.0,1.0),complex(0.0,1.0)],True)
 
89
 
 
90
        # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
 
91
 
 
92
    def _configure_graph (self, type):
 
93
        try:
 
94
            self.lock()
 
95
            self.disconnect_all ()
 
96
            if type == gr.GR_SIN_WAVE:
 
97
                self.connect (self.siggen, self.u)
 
98
                # self.connect (self.siggen, self.file_sink)
 
99
                self.siggen.set_waveform (type)
 
100
                self.src = self.siggen
 
101
            elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
 
102
                self.connect (self.noisegen, self.u)
 
103
                self.noisegen.set_type (type)
 
104
                self.src = self.noisegen
 
105
            elif type == gr.GR_CONST_WAVE:
 
106
                self.connect (self.vecgen, self.u)
 
107
                self.src = self.vecgen
 
108
            else:
 
109
                raise ValueError, type
 
110
        finally:
 
111
            self.unlock()
 
112
 
 
113
    def set_freq(self, target_freq):
 
114
        """
 
115
        Set the center frequency we're interested in.
 
116
 
 
117
        @param target_freq: frequency in Hz
 
118
        @rypte: bool
 
119
 
 
120
        Tuning is a two step process.  First we ask the front-end to
 
121
        tune as close to the desired frequency as it can.  Then we use
 
122
        the result of that operation and our target_frequency to
 
123
        determine the value for the digital up converter.
 
124
        """
 
125
        r = self.u.tune(self.subdev._which, self.subdev, target_freq)
 
126
        if r:
 
127
            #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
 
128
            #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
 
129
            #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
 
130
            #print "r.inverted      =", r.inverted
 
131
            return True
 
132
 
 
133
        return False
 
134
 
 
135
 
 
136
 
 
137
def main ():
 
138
    parser = OptionParser (option_class=eng_option)
 
139
    parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
 
140
                       help="select USRP Tx side A or B")
 
141
    parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
 
142
                       help="set RF center frequency to FREQ")
 
143
    parser.add_option ("-i", "--interp", type="int", default=512,
 
144
                       help="set fgpa interpolation rate to INTERP [default=%default]")
 
145
    parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
 
146
                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
 
147
 
 
148
    parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
 
149
                       help="generate Gaussian random output")
 
150
    parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
 
151
                       help="generate Uniform random output")
 
152
 
 
153
    parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
 
154
                       help="set waveform frequency to FREQ [default=%default]")
 
155
    parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
 
156
                       help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
 
157
    parser.add_option ("-g", "--gain", type="eng_float", default=None,
 
158
                       help="set output gain to GAIN [default=%default]")
 
159
    parser.add_option ("-o", "--offset", type="eng_float", default=0,
 
160
                       help="set waveform offset to OFFSET [default=%default]")
 
161
    parser.add_option ("--digital", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
 
162
                       help="generate (only) a digital wave on lsb (will be output on gpio pins with special usrp firmware)")
 
163
    (options, args) = parser.parse_args ()
 
164
 
 
165
    if len(args) != 0:
 
166
        parser.print_help()
 
167
        raise SystemExit
 
168
 
 
169
    if options.rf_freq is None:
 
170
        sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
 
171
        parser.print_help()
 
172
        raise SystemExit
 
173
 
 
174
    tb = my_top_block()
 
175
    tb.set_interpolator (options.interp)
 
176
    tb.set_waveform_type (options.type)
 
177
    tb.set_waveform_freq (options.waveform_freq)
 
178
    tb.set_waveform_ampl (options.amplitude)
 
179
    tb.set_waveform_offset (options.offset)
 
180
 
 
181
    # determine the daughterboard subdevice we're using
 
182
    if options.tx_subdev_spec is None:
 
183
        options.tx_subdev_spec = usrp.pick_tx_subdevice(tb.u)
 
184
 
 
185
    m = usrp.determine_tx_mux_value(tb.u, options.tx_subdev_spec)
 
186
    #print "mux = %#04x" % (m,)
 
187
    tb.u.set_mux(m)
 
188
    tb.subdev = usrp.selected_subdev(tb.u, options.tx_subdev_spec)
 
189
    print "Using TX d'board %s" % (tb.subdev.side_and_name(),)
 
190
    
 
191
    if options.gain is None:
 
192
        tb.subdev.set_gain(tb.subdev.gain_range()[1])    # set max Tx gain
 
193
    else:
 
194
        tb.subdev.set_gain(options.gain)    # set max Tx gain
 
195
 
 
196
    if not tb.set_freq(options.rf_freq):
 
197
        sys.stderr.write('Failed to set RF frequency\n')
 
198
        raise SystemExit
 
199
    
 
200
    tb.subdev.set_enable(True)                       # enable transmitter
 
201
 
 
202
    try:
 
203
        tb.run()
 
204
    except KeyboardInterrupt:
 
205
        pass
 
206
 
 
207
if __name__ == '__main__':
 
208
    main ()