Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gnuradio-examples / python / gmsk2 / 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 2, 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
43 class my_graph(gr.flow_graph):
44
45     def __init__(self, mod_class, tx_subdev_spec,
46                  bitrate, interp_rate, spb, gain,
47                  options, mod_kwargs):
48         gr.flow_graph.__init__(self)
49         self.txpath = transmit_path(self, mod_class, tx_subdev_spec,
50                                     bitrate, interp_rate, spb, gain,
51                                     options, mod_kwargs)
52
53
54 # /////////////////////////////////////////////////////////////////////////////
55 #                                   main
56 # /////////////////////////////////////////////////////////////////////////////
57
58 def main():
59
60     def send_pkt(payload='', eof=False):
61         return fg.txpath.send_pkt(payload, eof)
62
63     def rx_callback(ok, payload):
64         print "ok = %r, payload = '%s'" % (ok, payload)
65
66     parser = OptionParser (option_class=eng_option)
67     parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
68                       help="select USRP Tx side A or B")
69     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
70                       help="set Tx and Rx frequency to FREQ [default=%default]",
71                       metavar="FREQ")
72     parser.add_option("-r", "--bitrate", type="eng_float", default=None,
73                       help="specify bitrate.  spb and interp will be derived.")
74     parser.add_option("-S", "--spb", type="int", default=None,
75                       help="set samples/baud [default=%default]")
76     parser.add_option("-i", "--interp", type="intx", default=None,
77                       help="set fpga interpolation rate to INTERP [default=%default]")
78     parser.add_option("-s", "--size", type="eng_float", default=1500,
79                       help="set packet size [default=%default]")
80     parser.add_option("", "--bt", type="float", default=0.3,
81                       help="set bandwidth-time product [default=%default]")
82     parser.add_option("-g", "--gain", type="eng_float", default=100.0,
83                       help="transmitter gain [default=%default]")
84     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
85                       help="set megabytes to transmit [default=%default]")
86     parser.add_option("","--discontinuous", action="store_true", default=False,
87                       help="enable discontinous transmission (bursts of 5 packets)")
88     fusb_options.add_options(parser)
89     (options, args) = parser.parse_args ()
90
91     if len(args) != 0:
92         parser.print_help()
93         sys.exit(1)
94
95     if options.freq < 1e6:
96         options.freq *= 1e6
97
98     pkt_size = int(options.size)
99
100     mod_kwargs = {
101         'bt' : options.bt,
102         }
103
104     # build the graph
105     fg = my_graph(blks.gmsk2_mod, options.tx_subdev_spec,
106                   options.bitrate, options.interp, options.spb, options.gain,
107                   options, mod_kwargs)
108
109     print "bitrate: %sb/sec" % (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
118     r = gr.enable_realtime_scheduling()
119     if r != gr.RT_OK:
120         print "Warning: failed to enable realtime scheduling"
121
122     fg.start()                       # start flow graph
123
124     # generate and send packets
125     nbytes = int(1e6 * options.megabytes)
126     n = 0
127     pktno = 0
128
129     while n < nbytes:
130         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
131         n += pkt_size
132         sys.stderr.write('.')
133         if options.discontinuous and pktno % 5 == 4:
134             time.sleep(1)
135         pktno += 1
136         
137     send_pkt(eof=True)
138     fg.wait()                       # wait for it to finish
139     fg.txpath.set_auto_tr(False)
140
141
142 if __name__ == '__main__':
143     try:
144         main()
145     except KeyboardInterrupt:
146         pass