Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-examples / python / digital / limbo / benchmark_gmsk_tx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006 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, blks
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28
29 import random
30 import time
31 import struct
32 import sys
33
34 # from current dir
35 from transmit_path import transmit_path
36 import fusb_options
37
38 #import os
39 #print os.getpid()
40 #raw_input('Attach and press enter')
41
42 class my_graph(gr.flow_graph):
43
44     def __init__(self, options, mod_kwargs):
45         gr.flow_graph.__init__(self)
46         self.txpath = transmit_path(self, options, mod_kwargs)
47
48
49 # /////////////////////////////////////////////////////////////////////////////
50 #                                   main
51 # /////////////////////////////////////////////////////////////////////////////
52
53 def main():
54
55     def send_pkt(payload='', eof=False):
56         return fg.txpath.send_pkt(payload, eof)
57
58     def rx_callback(ok, payload):
59         print "ok = %r, payload = '%s'" % (ok, payload)
60
61     parser = OptionParser (option_class=eng_option)
62     parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
63                       help="select USRP Tx side A or B")
64     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
65                       help="set Tx and Rx frequency to FREQ [default=%default]",
66                       metavar="FREQ")
67     parser.add_option("-r", "--bitrate", type="eng_float", default=None,
68                       help="specify bitrate.  spb and interp will be derived.")
69     parser.add_option("-S", "--spb", type="int", default=2,
70                       help="set samples/baud [default=%default]")
71     parser.add_option("-i", "--interp", type="intx", default=None,
72                       help="set fpga interpolation rate to INTERP [default=%default]")
73     parser.add_option("-s", "--size", type="eng_float", default=1500,
74                       help="set packet size [default=%default]")
75     parser.add_option("", "--bt", type="float", default=0.3,
76                       help="set bandwidth-time product [default=%default]")
77     parser.add_option("-g", "--gain", type="eng_float", default=100.0,
78                       help="transmitter gain [default=%default]")
79     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
80                       help="set megabytes to transmit [default=%default]")
81     parser.add_option("","--discontinuous", action="store_true", default=False,
82                       help="enable discontinous transmission (bursts of 5 packets)")
83     fusb_options.add_options(parser)
84     (options, args) = parser.parse_args ()
85
86     if len(args) != 0:
87         parser.print_help()
88         sys.exit(1)
89
90     if options.freq < 1e6:
91         options.freq *= 1e6
92
93     pkt_size = int(options.size)
94
95     # Add gmsk_mod modulator class to options list
96     options.modulation = getattr(blks, "gmsk_mod")
97
98     # Add GMSK modulator's properties
99     mod_kwargs = {
100         'spb': options.spb,
101         'bt' : options.bt,
102         'verbose' : False,
103         'debug'   : False,
104         }
105         
106     # build the graph
107     fg = my_graph(options, mod_kwargs)
108
109     print "bitrate: %sbps" % (eng_notation.num_to_str(fg.txpath.bitrate()),)
110     print "spb:     %3d" % (fg.txpath.spb(),)
111     print "interp:  %3d" % (fg.txpath.interp(),)
112
113     ok = fg.txpath.set_freq(options.freq)
114     if not ok:
115         print "Failed to set Tx frequency to %s" % (eng_notation.num_to_str(options.freq),)
116         raise SystemExit
117     else:
118         print "Transmitting on frequency %s" % (eng_notation.num_to_str(options.freq))
119
120     r = gr.enable_realtime_scheduling()
121     if r != gr.RT_OK:
122         print "Warning: failed to enable realtime scheduling"
123     else:
124         print "Started realtime scheduling"
125
126     fg.start()                       # start flow graph
127
128     # generate and send packets
129     nbytes = int(1e6 * options.megabytes)
130     n = 0
131     pktno = 0
132
133     while n < nbytes:
134         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
135         n += pkt_size
136         sys.stderr.write('.')
137         if options.discontinuous and pktno % 5 == 4:
138             time.sleep(1)
139         pktno += 1
140         
141     send_pkt(eof=True)
142     fg.wait()                       # wait for it to finish
143     fg.txpath.set_auto_tr(False)
144
145
146 if __name__ == '__main__':
147     try:
148         main()
149     except KeyboardInterrupt:
150         pass