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