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

Revision 364, 12.1 KB (checked in by sriram, 15 years ago)

This does not work...keeping a backup

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