root/vtcross/branches/sriram/benchmark_txrxnode2.py @ 361

Revision 361, 11.2 KB (checked in by sriram, 15 years ago)

Correcting errors

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