root/vtcross/branches/sriram/dsa.py @ 381

Revision 381, 11.4 KB (checked in by sriram, 15 years ago)

Correcting bug

Line 
1#!/usr/bin/env python
2#Transmission and reception, one ata a time on the same antena
3
4from gnuradio import gr, gru, modulation_utils
5from gnuradio import eng_notation
6from gnuradio.eng_option import eng_option
7from optparse import OptionParser
8from numpy import random
9import random, time, struct, sys, math
10
11# from current dir
12from transmit_path import transmit_path
13from receive_path import receive_path
14global sync_status,mode,ch,traffic_flag
15sync_status = False
16mode = "sync"#default mode is sync
17traffic_flag = False
18class my_top_block(gr.top_block):
19
20        def __init__(self, mod_class, demod_class,
21                 rx_callback, options_tx,options_rx):
22
23                gr.top_block.__init__(self)
24                self.rxpath = receive_path(demod_class, rx_callback, options_rx)
25                self.txpath = transmit_path(mod_class, options_tx)
26                self.connect(self.txpath);
27                self.connect(self.rxpath);
28
29global n_rcvd, n_right
30
31def main():
32        print "inside main"
33        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
34        n_rcvd = 0
35        n_right = 0
36        def send_pkt(self, payload='', eof=False):
37                return self.txpath.send_pkt(payload, eof)
38
39
40        def rx_callback(ok, payload):
41                print "inside rx callback\n"
42                global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
43                ##############################################################
44                if mode == "sync":
45                        if ok:
46                                (pktno,) = struct.unpack('!H', payload[0:2])
47                                (sync_signal,) = struct.unpack('!s', payload[2])
48                                (data_channel,) = struct.unpack('!H', payload[3:5])
49                                print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",data_channel," !!!!!\n" 
50                                 
51                                if str(sync_signal) == 'o' and str(data_channel) == str(ch):
52                                        #print "inside if2"
53                                        sync_status = True
54                                        #tb.stop()
55                                        #print "after tb stop ans sync status is ",sync_status," sync status\n"
56                               
57                                if str(sync_signal) == 's' and str(data_channel) == str(ch):
58                                        sync_status = True
59                                        print "received a sync packet on channel %s\n" %(data_channel)
60                                        data = 'o'
61                                        pktno=0
62                                        ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
63                                        send_pkt(tb,ack_payload) #sending back the acknowledgement
64                        else:
65                                print "sync packet not ok\n"
66                ##############################################################
67               
68                ######################### traffic ############################
69                if mode == "traffic":
70                        if ok:
71                                (data_header,) = struct.unpack('!s', payload[0])
72                                if data_header == 'd':
73                                        traffic_flag = True
74                                        comm = struct.unpack('!14s', payload[1:15])
75                                        print("received this ", comm)
76                                        data = 'dI am fine.....' #Sending this message
77                                        payload = struct.pack('!15s', data)
78                                        send_pkt(tb,payload)
79                               
80                                #(data_header,) = struct.unpack('!s', payload[0])
81                                #if data_header == 'd':
82                                #       comm = struct.unpack('!14s', payload[1:15])
83                                #       print("received this ", comm)
84                        else:
85                                print "data packet not ok\n"
86                ##############################################################
87
88                n_rcvd += 1
89                if ok:
90                        n_right += 1
91
92                #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
93                #    ok, pktno, n_rcvd, n_right)
94                #print "sync_status is ",sync_status," @@\n"
95
96        mods = modulation_utils.type_1_mods()
97        #print "printing mods",mods,"$$$$\n"
98        demods = modulation_utils.type_1_demods()
99
100        #setting up the tx options parser
101        parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
102        expert_grp_tx = parser_tx.add_option_group("Expert_tx")
103        #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
104        parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
105                        default='gmsk',
106                        help="Select modulation from: %s [default=%%default]"
107                                % (', '.join(mods.keys()),))
108
109        parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
110                        help="set packet size [default=%default]")
111        parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
112                        help="set megabytes to transmit [default=%default]")
113        parser_tx.add_option("","--discontinuous", action="store_true", default=False,
114                        help="enable discontinous transmission (bursts of 5 packets)")
115        parser_tx.add_option("","--from-file", default=None,
116                        help="use file for packet contents")
117        print "printing parser_tx",parser_tx,"printing parser_tx"
118        transmit_path.add_options(parser_tx, expert_grp_tx)
119
120        for mod in mods.values():
121                mod.add_options(expert_grp_tx)
122
123        (options_tx, args_tx) = parser_tx.parse_args ()
124
125        if len(args_tx) != 0:
126                parser_tx.print_help()
127                sys.exit(1)
128       
129        ############# Setting some default values for tx side of the block
130        options_tx.tx_freq = 470e6#462.5625e6
131        options_tx.samples_per_symbol =  2
132        options_tx.modulation = 'dbpsk'
133        options_tx.fusb_block_size = 4096
134        options_tx.fusb_nblocks = 16
135        options_tx.bitrate = 0.0125e6
136        #############
137
138        if options_tx.tx_freq is None:
139                sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
140                parser_tx.print_help(sys.stderr)
141                sys.exit(1)
142
143        if options_tx.from_file is not None:
144                source_file = open(options_tx.from_file, 'r')
145        print "printing modulation type",mods[options_tx.modulation],"**"
146        # build the graph
147        print "printing tx options",options_tx,"&&&&\n"
148   
149        parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
150        print "printing parser_rx",parser_rx,"!!!!!\n"
151        print "after option parser"
152        expert_grp_rx = parser_rx.add_option_group("Expert_rx")
153        print "printing expert group rx",expert_grp_rx,"@@@@\n"
154        parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
155                        default='gmsk',
156                        help="Select modulation from: %s [default=%%default]"
157                                % (', '.join(demods.keys()),))
158        print "inside rx option parser"
159        receive_path.add_options(parser_rx, expert_grp_rx)
160
161        for mod in demods.values():
162                mod.add_options(expert_grp_rx)
163
164        (options_rx, args_rx) = parser_rx.parse_args ()
165
166        if len(args_rx) != 0:
167                parser_rx.print_help(sys.stderr)
168                sys.exit(1)
169        ############# Setting some default values for rx side of the block
170        options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value
171        options_rx.samples_per_symbol =  2
172        options_rx.modulation = 'dbpsk'
173        options_rx.fusb_block_size = 4096
174        options_rx.fusb_nblocks = 16
175        options_rx.bitrate = 0.0125e6
176        #############
177
178        if options_rx.rx_freq is None:
179                sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
180                parser_rx.print_help(sys.stderr)
181                sys.exit(1)
182        print "printing rx options",options_rx,"&&&&\n"
183
184        # build the graph
185
186        tb = my_top_block(mods[options_tx.modulation],
187                        demods[options_rx.modulation],
188                        rx_callback,options_tx,
189                        options_rx)
190        r = gr.enable_realtime_scheduling()
191        if r != gr.RT_OK:
192                print "Warning: failed to enable realtime scheduling"
193   
194        tb.start()
195        #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats)
196        #tb.rxpath.connect(tb.rxpath.u, tb.rxpath.s2v)
197        #listening to random frequencies untill a match is found
198        running = True
199        global ch
200        running = True
201   
202        while running:
203
204
205                ################################################sync mode####################################
206                if mode == "sync":
207                        if sync_status != True:
208                                ch = int(random.choice([1,7,8,14]))
209                                if ch < 8:
210                                        hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
211                                else:
212                                        hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets
213                       
214           
215                                tb.txpath.set_freq(hop_freq)
216                                tb.rxpath.set_freq(hop_freq)
217                       
218                                k = 0
219                                total_energy = 0
220                                while k <  1:
221                                        ch_energy = tb.rxpath.probe.level() #check if primary user is present
222                                        total_energy = total_energy + ch_energy
223                                        k+=1
224                       
225                                avg_energy = total_energy/1
226                                print "printing hop frequency ",hop_freq,"and energy is ",avg_energy,"\n"
227                                if int(avg_energy) > 2e7: #if primary user is there then dont transmit on this channel
228                                        #while
229                                        continue
230                                        #print "present"
231                       
232                                nbytes = 5 #int(1e6 * .0003)
233                                pkt_size = 5
234                                n = 0
235                                pktno = 0
236                                while n < nbytes:
237                                        if options_tx.from_file is None:
238                                                #data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255
239                                                data = 's'#+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
240                                        else:
241                                                data = source_file.read(pkt_size - 2)
242                                                if data == '':
243                                                        break;
244
245                                        payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
246                                        #print "printing payload",payload,"**\n"
247       
248                                        send_pkt(tb,payload)
249                                        print "sending this data ",data,"\n"
250                                        n += len(payload)
251                                        sys.stderr.write('.')
252                                        if options_tx.discontinuous and pktno % 5 == 4:
253                                                time.sleep(1)
254                                                pktno += 1
255                                time.sleep(0.1)
256                        #resend_count += 1
257                        else:
258                                print "sync channel found..channel ",ch,"\n" 
259                                check_sync = 0
260                                mode = "traffic"
261                                traffic_flag = False
262                                sync_status="False"
263       
264                ################################################end of sync mode####################################
265
266                ################################################Communications mode#################################
267                if mode == "traffic":
268                                print "Inside traffic mode"
269                        if check_sync == 0:
270                                time.sleep(0.8)
271                                if traffic_flag != True:
272                                        mode = "sync"
273                                        continue
274                                else:
275                                        check_sync = 1 
276
277                        nbytes = 15
278                        pkt_size = 15
279                        data_pktno = 0
280                        n = 0
281                        while n < nbytes:
282                                #print >> myfile, "inside while"
283                                if options_tx.from_file is None:
284                                        #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
285                                        data = 'dHi how are you' #Sending this message
286                                        #print >> myfile, data
287                                        #print "printing data",data,"****\n"
288                                else:
289                                        data = source_file.read(pkt_size - 2)
290                                        if data == '':
291                                                break;
292       
293                       
294                                payload = struct.pack('!15s', data)
295                                #print "printing payload",payload,"**\n"
296                               
297                                send_pkt(tb,payload)
298                                print "printing payload",data,"**\n"
299                                n += len(payload)
300                                sys.stderr.write('.')
301                                if options_tx.discontinuous and data_pktno % 5 == 4:
302                                        time.sleep(1)
303                                data_pktno += 1
304                                #print "before sleeping for 10 seconds and value of resend count is",resend_count
305                                time.sleep(0.2)
306                                ch_energy = tb.rxpath.probe.level() #check if primary user is present
307                                print "channel energy is ",ch_energy,"\n"
308                                if int(ch_energy) > 2e7: #if primary user is there then dont transmit on this channel
309                                        mode = "sync"
310               
311
312
313if __name__ == '__main__':
314    try:
315        main()
316    except KeyboardInterrupt:
317        pass
318
319
320
321
322               
Note: See TracBrowser for help on using the browser.