33259a2313d541016657b32103f80ceeb3006376
[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: Sun Nov  1 19:32:32 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 = 16
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.05
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.dat'
49                 self.output_filename = output_filename
50                 self.max_clock_offset = max_clock_offset = 0.1
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.05
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()
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.noaa_hrpt_bit_sync_0 = noaa.hrpt_bit_sync()
80                 self.pll = noaa.hrpt_pll_cf(pll_alpha, pll_alpha**2/4.0, max_carrier_offset)
81                 self.usrp_source = grc_usrp.simple_source_c(which=0, side=side, rx_ant="RXA")
82                 self.usrp_source.set_decim_rate(decim)
83                 self.usrp_source.set_frequency(freq, verbose=True)
84                 self.usrp_source.set_gain(gain)
85
86                 ##################################################
87                 # Connections
88                 ##################################################
89                 self.connect((self.usrp_source, 0), (self.agc, 0))
90                 self.connect((self.agc, 0), (self.pll, 0))
91                 self.connect((self.pll, 0), (self.gr_moving_average_xx_0, 0))
92                 self.connect((self.gr_moving_average_xx_0, 0), (self.gr_clock_recovery_mm_xx_0, 0))
93                 self.connect((self.noaa_hrpt_bit_sync_0, 0), (self.deframer, 0))
94                 self.connect((self.gr_binary_slicer_fb_0, 0), (self.noaa_hrpt_bit_sync_0, 0))
95                 self.connect((self.gr_clock_recovery_mm_xx_0, 0), (self.gr_binary_slicer_fb_0, 0))
96                 self.connect((self.deframer, 0), (self.frame_sink, 0))
97                 self.connect((self.deframer, 0), (self.decoder, 0))
98
99         def set_config_filename(self, config_filename):
100                 self.config_filename = config_filename
101                 self._clock_alpha_config = ConfigParser.ConfigParser()
102                 self._clock_alpha_config.read(self.config_filename)
103                 if not self._clock_alpha_config.has_section('usrp_rx_hrpt'):
104                         self._clock_alpha_config.add_section('usrp_rx_hrpt')
105                 self._clock_alpha_config.set('usrp_rx_hrpt', 'clock_alpha', str(self.clock_alpha))
106                 self._clock_alpha_config.write(open(self.config_filename, 'w'))
107                 self._gain_config = ConfigParser.ConfigParser()
108                 self._gain_config.read(self.config_filename)
109                 if not self._gain_config.has_section('usrp_rx_hrpt'):
110                         self._gain_config.add_section('usrp_rx_hrpt')
111                 self._gain_config.set('usrp_rx_hrpt', 'gain', str(self.gain))
112                 self._gain_config.write(open(self.config_filename, 'w'))
113                 self._freq_config = ConfigParser.ConfigParser()
114                 self._freq_config.read(self.config_filename)
115                 if not self._freq_config.has_section('usrp_rx_hrpt'):
116                         self._freq_config.add_section('usrp_rx_hrpt')
117                 self._freq_config.set('usrp_rx_hrpt', 'freq', str(self.freq))
118                 self._freq_config.write(open(self.config_filename, 'w'))
119                 self._decim_config = ConfigParser.ConfigParser()
120                 self._decim_config.read(self.config_filename)
121                 if not self._decim_config.has_section('usrp_rx_hrpt'):
122                         self._decim_config.add_section('usrp_rx_hrpt')
123                 self._decim_config.set('usrp_rx_hrpt', 'decim', str(self.decim))
124                 self._decim_config.write(open(self.config_filename, 'w'))
125                 self._output_filename_config = ConfigParser.ConfigParser()
126                 self._output_filename_config.read(self.config_filename)
127                 if not self._output_filename_config.has_section('usrp_rx_hrpt'):
128                         self._output_filename_config.add_section('usrp_rx_hrpt')
129                 self._output_filename_config.set('usrp_rx_hrpt', 'filename', str(self.output_filename))
130                 self._output_filename_config.write(open(self.config_filename, 'w'))
131                 self._pll_alpha_config = ConfigParser.ConfigParser()
132                 self._pll_alpha_config.read(self.config_filename)
133                 if not self._pll_alpha_config.has_section('usrp_rx_hrpt'):
134                         self._pll_alpha_config.add_section('usrp_rx_hrpt')
135                 self._pll_alpha_config.set('usrp_rx_hrpt', 'pll_alpha', str(self.pll_alpha))
136                 self._pll_alpha_config.write(open(self.config_filename, 'w'))
137                 self._side_config = ConfigParser.ConfigParser()
138                 self._side_config.read(self.config_filename)
139                 if not self._side_config.has_section('usrp_rx_hrpt'):
140                         self._side_config.add_section('usrp_rx_hrpt')
141                 self._side_config.set('usrp_rx_hrpt', 'side', str(self.side))
142                 self._side_config.write(open(self.config_filename, 'w'))
143
144         def set_decim(self, decim):
145                 self.decim = decim
146                 self.set_sample_rate(64e6/self.decim)
147                 self._decim_config = ConfigParser.ConfigParser()
148                 self._decim_config.read(self.config_filename)
149                 if not self._decim_config.has_section('usrp_rx_hrpt'):
150                         self._decim_config.add_section('usrp_rx_hrpt')
151                 self._decim_config.set('usrp_rx_hrpt', 'decim', str(self.decim))
152                 self._decim_config.write(open(self.config_filename, 'w'))
153                 self.usrp_source.set_decim_rate(self.decim)
154
155         def set_sym_rate(self, sym_rate):
156                 self.sym_rate = sym_rate
157                 self.set_sps(self.sample_rate/self.sym_rate)
158
159         def set_sample_rate(self, sample_rate):
160                 self.sample_rate = sample_rate
161                 self.set_max_carrier_offset(2*math.pi*100e3/self.sample_rate)
162                 self.set_sps(self.sample_rate/self.sym_rate)
163
164         def set_sps(self, sps):
165                 self.sps = sps
166                 self.set_hs(int(self.sps/2.0))
167                 self.gr_clock_recovery_mm_xx_0.set_omega(self.sps/2.0)
168
169         def set_side(self, side):
170                 self.side = side
171                 self._side_config = ConfigParser.ConfigParser()
172                 self._side_config.read(self.config_filename)
173                 if not self._side_config.has_section('usrp_rx_hrpt'):
174                         self._side_config.add_section('usrp_rx_hrpt')
175                 self._side_config.set('usrp_rx_hrpt', 'side', str(self.side))
176                 self._side_config.write(open(self.config_filename, 'w'))
177
178         def set_pll_alpha(self, pll_alpha):
179                 self.pll_alpha = pll_alpha
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                 self.pll.set_alpha(self.pll_alpha)
187                 self.pll.set_beta(self.pll_alpha**2/4.0)
188
189         def set_output_filename(self, output_filename):
190                 self.output_filename = output_filename
191                 self._output_filename_config = ConfigParser.ConfigParser()
192                 self._output_filename_config.read(self.config_filename)
193                 if not self._output_filename_config.has_section('usrp_rx_hrpt'):
194                         self._output_filename_config.add_section('usrp_rx_hrpt')
195                 self._output_filename_config.set('usrp_rx_hrpt', 'filename', str(self.output_filename))
196                 self._output_filename_config.write(open(self.config_filename, 'w'))
197
198         def set_max_clock_offset(self, max_clock_offset):
199                 self.max_clock_offset = max_clock_offset
200
201         def set_max_carrier_offset(self, max_carrier_offset):
202                 self.max_carrier_offset = max_carrier_offset
203                 self.pll.set_max_offset(self.max_carrier_offset)
204
205         def set_hs(self, hs):
206                 self.hs = hs
207                 self.gr_moving_average_xx_0.set_length_and_scale(self.hs, 1.0/self.hs)
208
209         def set_gain(self, gain):
210                 self.gain = gain
211                 self._gain_config = ConfigParser.ConfigParser()
212                 self._gain_config.read(self.config_filename)
213                 if not self._gain_config.has_section('usrp_rx_hrpt'):
214                         self._gain_config.add_section('usrp_rx_hrpt')
215                 self._gain_config.set('usrp_rx_hrpt', 'gain', str(self.gain))
216                 self._gain_config.write(open(self.config_filename, 'w'))
217                 self.usrp_source.set_gain(self.gain)
218
219         def set_freq(self, freq):
220                 self.freq = freq
221                 self._freq_config = ConfigParser.ConfigParser()
222                 self._freq_config.read(self.config_filename)
223                 if not self._freq_config.has_section('usrp_rx_hrpt'):
224                         self._freq_config.add_section('usrp_rx_hrpt')
225                 self._freq_config.set('usrp_rx_hrpt', 'freq', str(self.freq))
226                 self._freq_config.write(open(self.config_filename, 'w'))
227                 self.usrp_source.set_frequency(self.freq)
228
229         def set_clock_alpha(self, clock_alpha):
230                 self.clock_alpha = 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                 self.gr_clock_recovery_mm_xx_0.set_gain_omega(self.clock_alpha**2/4.0)
238                 self.gr_clock_recovery_mm_xx_0.set_gain_mu(self.clock_alpha)
239
240 if __name__ == '__main__':
241         parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
242         (options, args) = parser.parse_args()
243         if gr.enable_realtime_scheduling() != gr.RT_OK:
244                 print "Error: failed to enable realtime scheduling."
245         tb = usrp_rx_hrpt_nogui()
246         tb.start()
247         raw_input('Press Enter to quit: ')
248         tb.stop()
249