Simple code cleanup.
[debian/gnuradio] / gr-noaa / apps / usrp_rx_hrpt_nogui.py
1 #!/usr/bin/env python
2 ##################################################
3 # Gnuradio Python Flow Graph
4 # Title: Usrp Rx Hrpt Nogui
5 # Generated: Mon Nov  9 08:03:25 2009
6 ##################################################
7
8 from gnuradio import eng_notation
9 from gnuradio import gr
10 from gnuradio import noaa
11 from gnuradio.eng_option import eng_option
12 from gnuradio.gr import firdes
13 from grc_gnuradio import usrp as grc_usrp
14 from optparse import OptionParser
15 import ConfigParser
16 import math, os
17
18 class usrp_rx_hrpt_nogui(gr.top_block):
19
20         def __init__(self):
21                 gr.top_block.__init__(self, "Usrp Rx Hrpt Nogui")
22
23                 ##################################################
24                 # Variables
25                 ##################################################
26                 self.config_filename = config_filename = os.environ['HOME']+'/.gnuradio/config.conf'
27                 self._decim_config = ConfigParser.ConfigParser()
28                 self._decim_config.read(config_filename)
29                 try: decim = self._decim_config.getfloat('usrp_rx_hrpt', 'decim')
30                 except: decim = 32
31                 self.decim = decim
32                 self.sym_rate = sym_rate = 600*1109
33                 self.sample_rate = sample_rate = 64e6/decim
34                 self.sps = sps = sample_rate/sym_rate
35                 self._side_config = ConfigParser.ConfigParser()
36                 self._side_config.read(config_filename)
37                 try: side = self._side_config.get('usrp_rx_hrpt', 'side')
38                 except: side = 'A'
39                 self.side = side
40                 self._pll_alpha_config = ConfigParser.ConfigParser()
41                 self._pll_alpha_config.read(config_filename)
42                 try: pll_alpha = self._pll_alpha_config.getfloat('usrp_rx_hrpt', 'pll_alpha')
43                 except: pll_alpha = 0.01
44                 self.pll_alpha = pll_alpha
45                 self._output_filename_config = ConfigParser.ConfigParser()
46                 self._output_filename_config.read(config_filename)
47                 try: output_filename = self._output_filename_config.get('usrp_rx_hrpt', 'filename')
48                 except: output_filename = 'frames.hrpt'
49                 self.output_filename = output_filename
50                 self.max_clock_offset = max_clock_offset = 100e-6
51                 self.max_carrier_offset = max_carrier_offset = 2*math.pi*100e3/sample_rate
52                 self.hs = hs = int(sps/2.0)
53                 self._gain_config = ConfigParser.ConfigParser()
54                 self._gain_config.read(config_filename)
55                 try: gain = self._gain_config.getfloat('usrp_rx_hrpt', 'gain')
56                 except: gain = 35
57                 self.gain = gain
58                 self._freq_config = ConfigParser.ConfigParser()
59                 self._freq_config.read(config_filename)
60                 try: freq = self._freq_config.getfloat('usrp_rx_hrpt', 'freq')
61                 except: freq = 1698e6
62                 self.freq = freq
63                 self._clock_alpha_config = ConfigParser.ConfigParser()
64                 self._clock_alpha_config.read(config_filename)
65                 try: clock_alpha = self._clock_alpha_config.getfloat('usrp_rx_hrpt', 'clock_alpha')
66                 except: clock_alpha = 0.01
67                 self.clock_alpha = clock_alpha
68
69                 ##################################################
70                 # Blocks
71                 ##################################################
72                 self.agc = gr.agc_cc(1e-6, 1.0, 1.0, 1.0)
73                 self.decoder = noaa.hrpt_decoder(True,True)
74                 self.deframer = noaa.hrpt_deframer()
75                 self.frame_sink = gr.file_sink(gr.sizeof_short*1, output_filename)
76                 self.gr_binary_slicer_fb_0 = gr.binary_slicer_fb()
77                 self.gr_clock_recovery_mm_xx_0 = gr.clock_recovery_mm_ff(sps/2.0, clock_alpha**2/4.0, 0.5, clock_alpha, max_clock_offset)
78                 self.gr_moving_average_xx_0 = gr.moving_average_ff(hs, 1.0/hs, 4000)
79                 self.pll = noaa.hrpt_pll_cf(pll_alpha, pll_alpha**2/4.0, max_carrier_offset)
80                 self.usrp_source = grc_usrp.simple_source_c(which=0, side=side, rx_ant="RXA")
81                 self.usrp_source.set_decim_rate(decim)
82                 self.usrp_source.set_frequency(freq, verbose=True)
83                 self.usrp_source.set_gain(gain)
84
85                 ##################################################
86                 # Connections
87                 ##################################################
88                 self.connect((self.usrp_source, 0), (self.agc, 0))
89                 self.connect((self.agc, 0), (self.pll, 0))
90                 self.connect((self.pll, 0), (self.gr_moving_average_xx_0, 0))
91                 self.connect((self.gr_moving_average_xx_0, 0), (self.gr_clock_recovery_mm_xx_0, 0))
92                 self.connect((self.gr_clock_recovery_mm_xx_0, 0), (self.gr_binary_slicer_fb_0, 0))
93                 self.connect((self.deframer, 0), (self.frame_sink, 0))
94                 self.connect((self.deframer, 0), (self.decoder, 0))
95                 self.connect((self.gr_binary_slicer_fb_0, 0), (self.deframer, 0))
96
97         def set_config_filename(self, config_filename):
98                 self.config_filename = config_filename
99                 self._gain_config = ConfigParser.ConfigParser()
100                 self._gain_config.read(self.config_filename)
101                 if not self._gain_config.has_section('usrp_rx_hrpt'):
102                         self._gain_config.add_section('usrp_rx_hrpt')
103                 self._gain_config.set('usrp_rx_hrpt', 'gain', str(self.gain))
104                 self._gain_config.write(open(self.config_filename, 'w'))
105                 self._freq_config = ConfigParser.ConfigParser()
106                 self._freq_config.read(self.config_filename)
107                 if not self._freq_config.has_section('usrp_rx_hrpt'):
108                         self._freq_config.add_section('usrp_rx_hrpt')
109                 self._freq_config.set('usrp_rx_hrpt', 'freq', str(self.freq))
110                 self._freq_config.write(open(self.config_filename, 'w'))
111                 self._side_config = ConfigParser.ConfigParser()
112                 self._side_config.read(self.config_filename)
113                 if not self._side_config.has_section('usrp_rx_hrpt'):
114                         self._side_config.add_section('usrp_rx_hrpt')
115                 self._side_config.set('usrp_rx_hrpt', 'side', str(self.side))
116                 self._side_config.write(open(self.config_filename, 'w'))
117                 self._decim_config = ConfigParser.ConfigParser()
118                 self._decim_config.read(self.config_filename)
119                 if not self._decim_config.has_section('usrp_rx_hrpt'):
120                         self._decim_config.add_section('usrp_rx_hrpt')
121                 self._decim_config.set('usrp_rx_hrpt', 'decim', str(self.decim))
122                 self._decim_config.write(open(self.config_filename, 'w'))
123                 self._pll_alpha_config = ConfigParser.ConfigParser()
124                 self._pll_alpha_config.read(self.config_filename)
125                 if not self._pll_alpha_config.has_section('usrp_rx_hrpt'):
126                         self._pll_alpha_config.add_section('usrp_rx_hrpt')
127                 self._pll_alpha_config.set('usrp_rx_hrpt', 'pll_alpha', str(self.pll_alpha))
128                 self._pll_alpha_config.write(open(self.config_filename, 'w'))
129                 self._clock_alpha_config = ConfigParser.ConfigParser()
130                 self._clock_alpha_config.read(self.config_filename)
131                 if not self._clock_alpha_config.has_section('usrp_rx_hrpt'):
132                         self._clock_alpha_config.add_section('usrp_rx_hrpt')
133                 self._clock_alpha_config.set('usrp_rx_hrpt', 'clock_alpha', str(self.clock_alpha))
134                 self._clock_alpha_config.write(open(self.config_filename, 'w'))
135                 self._output_filename_config = ConfigParser.ConfigParser()
136                 self._output_filename_config.read(self.config_filename)
137                 if not self._output_filename_config.has_section('usrp_rx_hrpt'):
138                         self._output_filename_config.add_section('usrp_rx_hrpt')
139                 self._output_filename_config.set('usrp_rx_hrpt', 'filename', str(self.output_filename))
140                 self._output_filename_config.write(open(self.config_filename, 'w'))
141
142         def set_decim(self, decim):
143                 self.decim = decim
144                 self.set_sample_rate(64e6/self.decim)
145                 self.usrp_source.set_decim_rate(self.decim)
146                 self._decim_config = ConfigParser.ConfigParser()
147                 self._decim_config.read(self.config_filename)
148                 if not self._decim_config.has_section('usrp_rx_hrpt'):
149                         self._decim_config.add_section('usrp_rx_hrpt')
150                 self._decim_config.set('usrp_rx_hrpt', 'decim', str(self.decim))
151                 self._decim_config.write(open(self.config_filename, 'w'))
152
153         def set_sym_rate(self, sym_rate):
154                 self.sym_rate = sym_rate
155                 self.set_sps(self.sample_rate/self.sym_rate)
156
157         def set_sample_rate(self, sample_rate):
158                 self.sample_rate = sample_rate
159                 self.set_max_carrier_offset(2*math.pi*100e3/self.sample_rate)
160                 self.set_sps(self.sample_rate/self.sym_rate)
161
162         def set_sps(self, sps):
163                 self.sps = sps
164                 self.set_hs(int(self.sps/2.0))
165                 self.gr_clock_recovery_mm_xx_0.set_omega(self.sps/2.0)
166
167         def set_side(self, side):
168                 self.side = side
169                 self._side_config = ConfigParser.ConfigParser()
170                 self._side_config.read(self.config_filename)
171                 if not self._side_config.has_section('usrp_rx_hrpt'):
172                         self._side_config.add_section('usrp_rx_hrpt')
173                 self._side_config.set('usrp_rx_hrpt', 'side', str(self.side))
174                 self._side_config.write(open(self.config_filename, 'w'))
175
176         def set_pll_alpha(self, pll_alpha):
177                 self.pll_alpha = pll_alpha
178                 self.pll.set_alpha(self.pll_alpha)
179                 self.pll.set_beta(self.pll_alpha**2/4.0)
180                 self._pll_alpha_config = ConfigParser.ConfigParser()
181                 self._pll_alpha_config.read(self.config_filename)
182                 if not self._pll_alpha_config.has_section('usrp_rx_hrpt'):
183                         self._pll_alpha_config.add_section('usrp_rx_hrpt')
184                 self._pll_alpha_config.set('usrp_rx_hrpt', 'pll_alpha', str(self.pll_alpha))
185                 self._pll_alpha_config.write(open(self.config_filename, 'w'))
186
187         def set_output_filename(self, output_filename):
188                 self.output_filename = output_filename
189                 self._output_filename_config = ConfigParser.ConfigParser()
190                 self._output_filename_config.read(self.config_filename)
191                 if not self._output_filename_config.has_section('usrp_rx_hrpt'):
192                         self._output_filename_config.add_section('usrp_rx_hrpt')
193                 self._output_filename_config.set('usrp_rx_hrpt', 'filename', str(self.output_filename))
194                 self._output_filename_config.write(open(self.config_filename, 'w'))
195
196         def set_max_clock_offset(self, max_clock_offset):
197                 self.max_clock_offset = max_clock_offset
198
199         def set_max_carrier_offset(self, max_carrier_offset):
200                 self.max_carrier_offset = max_carrier_offset
201                 self.pll.set_max_offset(self.max_carrier_offset)
202
203         def set_hs(self, hs):
204                 self.hs = hs
205                 self.gr_moving_average_xx_0.set_length_and_scale(self.hs, 1.0/self.hs)
206
207         def set_gain(self, gain):
208                 self.gain = gain
209                 self._gain_config = ConfigParser.ConfigParser()
210                 self._gain_config.read(self.config_filename)
211                 if not self._gain_config.has_section('usrp_rx_hrpt'):
212                         self._gain_config.add_section('usrp_rx_hrpt')
213                 self._gain_config.set('usrp_rx_hrpt', 'gain', str(self.gain))
214                 self._gain_config.write(open(self.config_filename, 'w'))
215                 self.usrp_source.set_gain(self.gain)
216
217         def set_freq(self, freq):
218                 self.freq = freq
219                 self._freq_config = ConfigParser.ConfigParser()
220                 self._freq_config.read(self.config_filename)
221                 if not self._freq_config.has_section('usrp_rx_hrpt'):
222                         self._freq_config.add_section('usrp_rx_hrpt')
223                 self._freq_config.set('usrp_rx_hrpt', 'freq', str(self.freq))
224                 self._freq_config.write(open(self.config_filename, 'w'))
225                 self.usrp_source.set_frequency(self.freq)
226
227         def set_clock_alpha(self, clock_alpha):
228                 self.clock_alpha = clock_alpha
229                 self.gr_clock_recovery_mm_xx_0.set_gain_omega(self.clock_alpha**2/4.0)
230                 self.gr_clock_recovery_mm_xx_0.set_gain_mu(self.clock_alpha)
231                 self._clock_alpha_config = ConfigParser.ConfigParser()
232                 self._clock_alpha_config.read(self.config_filename)
233                 if not self._clock_alpha_config.has_section('usrp_rx_hrpt'):
234                         self._clock_alpha_config.add_section('usrp_rx_hrpt')
235                 self._clock_alpha_config.set('usrp_rx_hrpt', 'clock_alpha', str(self.clock_alpha))
236                 self._clock_alpha_config.write(open(self.config_filename, 'w'))
237
238 if __name__ == '__main__':
239         parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
240         (options, args) = parser.parse_args()
241         if gr.enable_realtime_scheduling() != gr.RT_OK:
242                 print "Error: failed to enable realtime scheduling."
243         tb = usrp_rx_hrpt_nogui()
244         tb.start()
245         raw_input('Press Enter to quit: ')
246         tb.stop()
247