Houston, we have a trunk.
[debian/gnuradio] / gnuradio-examples / python / gmsk2 / 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 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., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, 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 bpsk  import bpsk_mod
33 from dbpsk import dbpsk_mod
34 from dqpsk import dqpsk_mod
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("-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("-g", "--gain", type="eng_float", default=100.0,
85                       help="transmitter gain [default=%default]")
86     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
87                       help="set megabytes to transmit [default=%default]")
88     parser.add_option("","--discontinuous", action="store_true", default=False,
89                       help="enable discontinous transmission (bursts of 5 packets)")
90     fusb_options.add_options(parser)
91     (options, args) = parser.parse_args ()
92
93     if len(args) != 0:
94         parser.print_help()
95         sys.exit(1)
96
97     if options.freq < 1e6:
98         options.freq *= 1e6
99
100     pkt_size = int(options.size)
101
102     mod_kwargs = {
103         'excess_bw' : options.excess_bw,
104         }
105
106     #FIXME: rework when static class defintions are ready for the modulation types
107     if(options.modulation=='bpsk'):
108         modulation=bpsk_mod
109     elif( options.modulation=='dbpsk'):
110         modulation=dbpsk_mod
111     else:
112         modulation=dqpsk_mod
113
114     # build the graph
115     fg = my_graph(modulation,
116                   options.tx_subdev_spec, options.bitrate, options.interp,
117                   options.spb, options.gain,
118                   options, mod_kwargs)
119
120     print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.txpath.bitrate()),)
121     print "spb:     %3d" % (fg.txpath.spb(),)
122     print "interp:  %3d" % (fg.txpath.interp(),)
123
124     ok = fg.txpath.set_freq(options.freq)
125     if not ok:
126         print "Failed to set Tx frequency to %s" % (eng_notation.num_to_str(options.freq),)
127         raise SystemExit
128
129     r = gr.enable_realtime_scheduling()
130     if r != gr.RT_OK:
131         print "Warning: failed to enable realtime scheduling"
132
133     fg.start()                       # start flow graph
134
135     # generate and send packets
136     nbytes = int(1e6 * options.megabytes)
137     n = 0
138     pktno = 0
139
140     while n < nbytes:
141         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
142         n += pkt_size
143         sys.stderr.write('.')
144         if options.discontinuous and pktno % 5 == 4:
145             time.sleep(1)
146         pktno += 1
147         
148     send_pkt(eof=True)
149     fg.wait()                       # wait for it to finish
150     fg.txpath.set_auto_tr(False)
151
152
153 if __name__ == '__main__':
154     try:
155         main()
156     except KeyboardInterrupt:
157         pass