summaryrefslogtreecommitdiff
path: root/gnuradio-examples/python/gmsk2/receive_path.py
blob: 505ff7c4d344025a96f40f0b398d4cbf52732ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
#
# Copyright 2005,2006 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 

from gnuradio import gr, gru, blks
from gnuradio import usrp

# from current dir
from pick_bitrate import pick_rx_bitrate

# /////////////////////////////////////////////////////////////////////////////
#                              receive path
# /////////////////////////////////////////////////////////////////////////////

class receive_path(gr.hier_block):
    def __init__(self, fg, demod_class, rx_subdev_spec,
                 bitrate, decim, spb,
                 rx_callback, options, demod_kwargs):

        self.u = usrp.source_c (fusb_block_size=options.fusb_block_size,
                                fusb_nblocks=options.fusb_nblocks)
        adc_rate = self.u.adc_rate()

        (self._bitrate, self._spb, self._decim) = \
            pick_rx_bitrate(bitrate, demod_class.bits_per_baud(), spb, decim, adc_rate)

        self.u.set_decim_rate(self._decim)
        sw_decim = 1

        if rx_subdev_spec is None:
            rx_subdev_spec = usrp.pick_rx_subdevice(self.u)
        self.subdev = usrp.selected_subdev(self.u, rx_subdev_spec)
        print "Using RX d'board %s" % (self.subdev.side_and_name(),)

        self.u.set_mux(usrp.determine_rx_mux_value(self.u, rx_subdev_spec))

        # Create filter to get actual channel we want
        chan_coeffs = gr.firdes.low_pass (1.0,                  # gain
                                          sw_decim * self._spb, # sampling rate
                                          1.0,                  # midpoint of trans. band
                                          0.1,                  # width of trans. band
                                          gr.firdes.WIN_HANN)   # filter type 

        print "len(rx_chan_coeffs) =", len(chan_coeffs)

        # Decimating Channel filter
        # complex in and out, float taps
        self.chan_filt = gr.fft_filter_ccc(sw_decim, chan_coeffs)
        #self.chan_filt = gr.fir_filter_ccf(sw_decim, chan_coeffs)

        # receiver
        self.packet_receiver = \
            blks.demod_pkts(fg,
                            demod_class(fg, spb=self._spb, **demod_kwargs),
                            access_code=None,
                            callback=rx_callback,
                            threshold=-1)

        fg.connect(self.u, self.chan_filt, self.packet_receiver)
        gr.hier_block.__init__(self, fg, None, None)

        g = self.subdev.gain_range()
        #self.set_gain((g[0] + g[1])/2)        # set gain to midpoint
        self.set_gain(g[1])                    # set gain to max
        self.set_auto_tr(True)                 # enable Auto Transmit/Receive switching

        # Carrier Sensing Blocks
        alpha = 0.001
        thresh = 30   # in dB, will have to adjust
        self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
        fg.connect(self.chan_filt, self.probe)

    def set_freq(self, target_freq):
        """
        Set the center frequency we're interested in.

        @param target_freq: frequency in Hz
        @rypte: bool

        Tuning is a two step process.  First we ask the front-end to
        tune as close to the desired frequency as it can.  Then we use
        the result of that operation and our target_frequency to
        determine the value for the digital up converter.
        """
        r = self.u.tune(0, self.subdev, target_freq)
        if r:
            return True

        return False

    def set_gain(self, gain):
        if gain is None:
            r = self.subdev.gain_range()
            gain = (r[0] + r[1])/2               # set gain to midpoint
        self.gain = gain
        return self.subdev.set_gain(gain)

    def set_auto_tr(self, enable):
        return self.subdev.set_auto_tr(enable)
        
    def bitrate(self):
        return self._bitrate

    def spb(self):
        return self._spb

    def decim(self):
        return self._decim


    def carrier_sensed(self):
        """
        Return True if we think carrier is present.
        """
        #return self.probe.level() > X
        return self.probe.unmuted()

    def carrier_threshold(self):
        """
        Return current setting in dB.
        """
        return self.probe.threshold()

    def set_carrier_threshold(self, threshold_in_db):
        """
        Set carrier threshold.

        @param threshold_in_db: set detection threshold
        @type threshold_in_db:  float (dB)
        """
        self.probe.set_threshold(threshold_in_db)