blob: 5f328a9d8e300fe9dcea10cd8529e8eb6c4efd09 [file] [log] [blame]
piotr4089c1a2014-08-06 14:10:56 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
ptrkrysik529895b2014-12-02 18:07:38 +01004# Copyright 2014 Piotr Krysik <ptrkrysik@gmail.com>
piotr4089c1a2014-08-06 14:10:56 +02005#
6# This 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# This software 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 this software; 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
22from numpy import *
23from gnuradio import gr
24import pmt
Piotr K8cfe4c42014-08-07 17:03:10 +020025from threading import Timer
piotr4089c1a2014-08-06 14:10:56 +020026
27class clock_offset_control(gr.basic_block):
28 """
29 docstring for block clock_offset_control
30 """
ptrkrysik4c538ba2014-11-28 23:40:12 +010031 def __init__(self, fc):
piotr4089c1a2014-08-06 14:10:56 +020032 gr.basic_block.__init__(self,
Piotr K66bb3cd2014-08-13 19:04:57 +020033 name="gsm_clock_offset_control",
piotr4089c1a2014-08-06 14:10:56 +020034 in_sig=[],
35 out_sig=[])
36 self.fc = fc
piotr4089c1a2014-08-06 14:10:56 +020037 self.message_port_register_in(pmt.intern("measurements"))
38 self.set_msg_handler(pmt.intern("measurements"), self.process_measurement)
39 self.message_port_register_out(pmt.intern("ppm"))
Piotr K66bb3cd2014-08-13 19:04:57 +020040 self.alfa = 0.3
piotr4089c1a2014-08-06 14:10:56 +020041 self.ppm_estimate = -1e6
42 self.first_measurement = True
43 self.counter = 0
Piotr K8cfe4c42014-08-07 17:03:10 +020044 self.last_state = ""
Piotr K77f3b062014-08-07 23:04:43 +020045 self.timer = Timer(0.5, self.timed_reset)
ptrkrysik62215e82014-10-30 08:59:38 +010046 self.last_ppm_estimate = -1e6
piotr4089c1a2014-08-06 14:10:56 +020047
48 def process_measurement(self,msg):
49 if pmt.is_tuple(msg):
50 key = pmt.symbol_to_string(pmt.tuple_ref(msg,0))
51 if key == "freq_offset":
52 freq_offset = pmt.to_double(pmt.tuple_ref(msg,1))
53 ppm = -freq_offset/self.fc*1.0e6
54 state = pmt.symbol_to_string(pmt.tuple_ref(msg,2))
piotr9b59e822014-08-06 20:14:46 +020055
Piotr K8cfe4c42014-08-07 17:03:10 +020056 self.last_state = state
57
58 if abs(ppm) > 100: #safeguard against flawed measurements
piotr9b59e822014-08-06 20:14:46 +020059 ppm = 0
60 self.reset()
61
piotrd6d66872014-08-06 15:20:33 +020062 if state == "fcch_search":
piotr4089c1a2014-08-06 14:10:56 +020063 msg_ppm = pmt.from_double(ppm)
64 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
Piotr K8cfe4c42014-08-07 17:03:10 +020065 self.timer.cancel()
Piotr K77f3b062014-08-07 23:04:43 +020066 self.timer = Timer(0.5, self.timed_reset)
Piotr K8cfe4c42014-08-07 17:03:10 +020067 self.timer.start()
68 elif state == "synchronized":
Piotr K77f3b062014-08-07 23:04:43 +020069 self.timer.cancel()
piotr4089c1a2014-08-06 14:10:56 +020070 if self.first_measurement:
71 self.ppm_estimate = ppm
72 self.first_measurement = False
73 else:
74 self.ppm_estimate = (1-self.alfa)*self.ppm_estimate+self.alfa*ppm
75
76 if self.counter == 5:
77 self.counter = 0
ptrkrysik62215e82014-10-30 08:59:38 +010078 if abs(self.last_ppm_estimate-self.ppm_estimate) > 0.1:
Piotr K66bb3cd2014-08-13 19:04:57 +020079 msg_ppm = pmt.from_double(ppm)
80 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
ptrkrysik62215e82014-10-30 08:59:38 +010081 self.last_ppm_estimate = self.ppm_estimate
piotr4089c1a2014-08-06 14:10:56 +020082 else:
83 self.counter=self.counter+1
Piotr K8cfe4c42014-08-07 17:03:10 +020084 elif state == "sync_loss":
piotr9b59e822014-08-06 20:14:46 +020085 self.reset()
piotr4089c1a2014-08-06 14:10:56 +020086 msg_ppm = pmt.from_double(0.0)
87 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
Piotr K8cfe4c42014-08-07 17:03:10 +020088
Piotr K66bb3cd2014-08-13 19:04:57 +020089
Piotr K77f3b062014-08-07 23:04:43 +020090 def timed_reset(self):
Piotr K8cfe4c42014-08-07 17:03:10 +020091 if self.last_state != "synchronized":
92# print "conditional reset"
93 self.reset()
94 msg_ppm = pmt.from_double(0.0)
95 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
96
piotr9b59e822014-08-06 20:14:46 +020097 def reset(self):
98 self.ppm_estimate = -1e6
99 self.counter = 0
100 self.first_measurement = True
Piotr K77f3b062014-08-07 23:04:43 +0200101