Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-examples / python / digital / transmit_path_lb.py
1 #
2 # Copyright 2005,2006 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio import gr, gru, blks
23 from gnuradio import eng_notation
24
25 import copy
26 import sys
27
28 # /////////////////////////////////////////////////////////////////////////////
29 #                              transmit path
30 # /////////////////////////////////////////////////////////////////////////////
31
32 class transmit_path(gr.hier_block):
33     def __init__(self, fg, modulator_class, options):
34         '''
35         See below for what options should hold
36         '''
37         
38         options = copy.copy(options)    # make a copy so we can destructively modify
39
40         self._verbose            = options.verbose
41         self._tx_amplitude       = options.tx_amplitude    # digital amplitude sent to USRP
42         self._bitrate            = options.bitrate         # desired bit rate
43         self._samples_per_symbol = options.samples_per_symbol  # desired samples/baud
44
45         self._modulator_class = modulator_class         # the modulator_class we are using
46
47         # Get mod_kwargs
48         mod_kwargs = self._modulator_class.extract_kwargs_from_options(options)
49     
50         # transmitter
51         self.packet_transmitter = \
52             blks.mod_pkts(fg,
53                           self._modulator_class(fg, **mod_kwargs),
54                           access_code=None,
55                           msgq_limit=4,
56                           pad_for_usrp=True)
57
58         self.amp = gr.multiply_const_cc(1)
59         self.set_tx_amplitude(self._tx_amplitude)
60
61         # Display some information about the setup
62         if self._verbose:
63             self._print_verbage()
64
65         # Connect components in the flowgraph
66         fg.connect(self.packet_transmitter, self.amp)
67
68         gr.hier_block.__init__(self, fg, None, self.amp)
69
70     def set_tx_amplitude(self, ampl):
71         """
72         Sets the transmit amplitude sent to the USRP
73         @param: ampl 0 <= ampl < 32768.  Try 8000
74         """
75         self._tx_amplitude = max(0.0, min(ampl, 32767.0))
76         self.amp.set_k(self._tx_amplitude)
77         
78     def send_pkt(self, payload='', eof=False):
79         """
80         Calls the transmitter method to send a packet
81         """
82         return self.packet_transmitter.send_pkt(payload, eof)
83         
84     def bitrate(self):
85         return self._bitrate
86
87     def samples_per_symbol(self):
88         return self._samples_per_symbol
89
90     def add_options(normal, expert):
91         """
92         Adds transmitter-specific options to the Options Parser
93         """
94         if not normal.has_option('--bitrate'):
95             normal.add_option("-r", "--bitrate", type="eng_float", default=100e3,
96                               help="specify bitrate [default=%default].")
97         normal.add_option("", "--tx-amplitude", type="eng_float", default=12000, metavar="AMPL",
98                           help="set transmitter digital amplitude: 0 <= AMPL < 32768 [default=%default]")
99         normal.add_option("-v", "--verbose", action="store_true", default=False)
100
101         expert.add_option("-S", "--samples-per-symbol", type="int", default=2,
102                           help="set samples/symbol [default=%default]")
103         expert.add_option("", "--log", action="store_true", default=False,
104                           help="Log all parts of flow graph to file (CAUTION: lots of data)")
105
106     # Make a static method to call before instantiation
107     add_options = staticmethod(add_options)
108
109     def _print_verbage(self):
110         """
111         Prints information about the transmit path
112         """
113         print "Tx amplitude     %s"    % (self._tx_amplitude)
114         print "modulation:      %s"    % (self._modulator_class.__name__)
115         print "bitrate:         %sb/s" % (eng_notation.num_to_str(self._bitrate))
116         print "samples/symbol:  %3d"   % (self._samples_per_symbol)
117