root/vtcross/branches/sriram/benchmark_txrxnode1.py @ 359

Revision 359, 13.8 KB (checked in by sriram, 15 years ago)

More changes for getting the sync and data going one after the other

  • 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
6##########spectrum sense specific imports
7from gnuradio import optfir, window
8from gnuradio import usrp
9#from gnuradio.eng_option import eng_option
10#from usrpm import usrp_dbid
11##########
12from gnuradio.eng_option import eng_option
13from optparse import OptionParser
14from numpy import random
15import random, time, struct, sys, math
16
17# from current dir
18from transmit_path import transmit_path
19from receive_path import receive_path
20#import spec_sense_path
21
22global sync_status
23sync_status = False #default value is "False"...
24global mode
25#Defining modes of operation
26#sync = synchronizing with other node before data transfer
27#traffic = data transfer
28#spec_sense = sensing spectrum for primary user
29mode = "sync" #default mode of operation...
30class my_top_block(gr.top_block):
31
32    def __init__(self, mod_class, demod_class,
33                 rx_callback, options_tx,options_rx):
34
35        gr.top_block.__init__(self)
36        self.rxpath = receive_path(demod_class, rx_callback, options_rx)
37        self.txpath = transmit_path(mod_class, options_tx)
38        self.connect(self.txpath);
39        self.connect(self.rxpath);
40
41       
42#//////main////// 
43
44global n_rcvd, n_right,hop_freq
45
46def main():
47    global n_rcvd, n_right,sync_status,mode,hop_freq
48    n_rcvd = 0
49    n_right = 0
50    def send_pkt(self, payload='', eof=False):
51        return self.txpath.send_pkt(payload, eof)
52
53    def rx_callback(ok, payload):
54
55       
56        #print "inside rx callback"
57        global n_rcvd, n_right, sync_status,mode,hop_freq
58        #tb.rxpath.min_freq = hop_freq - 80e3
59        #tb.rxpath.max_freq = hop_freq + 80e3
60        #tb.rxpath.get_spec_stats(1e13,20,2)
61        (pktno,) = struct.unpack('!H', payload[0:2])
62        #temp = struct.unpack('!s', payload[3:10])
63        #print "printing the elements of packet",temp,"!!!!!!!!!!!\n"
64        #####################################################
65        if mode == "sync":
66                (sync_signal,) = struct.unpack('!s', payload[2])
67                (sync_signal_red,) = struct.unpack('!s', payload[3])  #redundant sync bit
68                (data_channel,) = struct.unpack('!s', payload[4])
69                (data_channel_red,) = struct.unpack('!s', payload[5])  #redundant channel bit
70                #print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",sync_signal_red," !!!!!\n"   
71                #print "printing the channel  ",data_channel_red,"  !!!!!!!!!!!  ",data_channel_red," !!!!!\n"
72                if ok: #== True :# and str(sync_signal) == 'o' and str(sync_signal_red) == 'o':
73                        #print "inside if"
74                        if ok: #== True: #str(data_channel) == str(data_channel_red):
75                                #print "inside if1$$$$$"
76                                if str(data_channel) == str(ch):
77                                        #print "inside if2"
78                                        sync_status = True
79                                        #tb.stop()
80                                        #print "after tb stop ans sync status is ",sync_status," sync status\n"
81                                        #print "received a sync packet on channel %s\n" %(data_channel)
82                                        #data = 'o'+'o'+str(data_channel)+str(data_channel)
83                                        #print "sending this data",data,"@@@@@\n"
84                                        #sync_pktno=0
85                                        #ack_payload = struct.pack('!H', sync_pktno & 0xffff) + data
86                                        #print "printing the ack packet",ack_payload,"$$$$$\n"
87                                        #k=0
88                                        #while k < 10000:
89                                        #print "inside while"
90                                        #send_pkt(tb,ack_payload) #sending back the acknowledgement
91                                        #k=k+1
92        #####################################################
93       
94        ######################### traffic ############################
95        if mode == "traffic":
96                (data_header,) = struct.unpack('!s', payload[0])
97                if ok and data_header == 'd':
98                        comm = struct.unpack('!14s', data)
99                        print("received this ", comm)
100       
101        ##############################################################
102       
103
104
105        n_rcvd += 1
106        if ok:
107            n_right += 1
108
109        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
110            #ok, pktno, n_rcvd, n_right)
111
112    mods = modulation_utils.type_1_mods()
113    demods = modulation_utils.type_1_demods()
114
115    #setting up the tx options parser
116    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
117    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
118    print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
119    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
120                      default='gmsk',
121                      help="Select modulation from: %s [default=%%default]"
122                            % (', '.join(mods.keys()),))
123
124    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
125                      help="set packet size [default=%default]")
126    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
127                      help="set megabytes to transmit [default=%default]")
128    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
129                      help="enable discontinous transmission (bursts of 5 packets)")
130    parser_tx.add_option("","--from-file", default=None,
131                      help="use file for packet contents")
132    print "printing parser_tx",parser_tx,"printing parser_tx"
133    transmit_path.add_options(parser_tx, expert_grp_tx)
134
135    for mod in mods.values():
136        mod.add_options(expert_grp_tx)
137
138    (options_tx, args_tx) = parser_tx.parse_args ()
139
140    if len(args_tx) != 0:
141        parser_tx.print_help()
142        sys.exit(1)
143    ############# Setting some default values for tx side of the block
144    options_tx.tx_freq = 462.5625e6
145    options_tx.samples_per_symbol =  2
146    options_tx.modulation = 'dbpsk'
147    options_tx.fusb_block_size = 4096
148    options_tx.fusb_nblocks = 16
149    options_tx.bitrate = 0.0125e6
150    #############
151    if options_tx.tx_freq is None:
152        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
153        parser_tx.print_help(sys.stderr)
154        sys.exit(1)
155
156    if options_tx.from_file is not None:
157        source_file = open(options_tx.from_file, 'r')
158    print "printing modulation type",mods[options_tx.modulation],"**"
159    # build the graph
160    print "printing tx options",options_tx,"&&&&\n"
161    #tb = my_top_block(mods[options_tx.modulation], options_tx)
162
163    #r = gr.enable_realtime_scheduling()
164    #if r != gr.RT_OK:
165    #   print "Warning: failed to enable realtime scheduling"
166
167    #setting up rx options parser
168   
169    # Create Options Parser:
170    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
171    print "printing parser_rx",parser_rx,"!!!!!\n"
172    print "after option parser"
173    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
174    print "printing expert group rx",expert_grp_rx,"@@@@\n"
175    parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
176                      default='gmsk',
177                      help="Select modulation from: %s [default=%%default]"
178                            % (', '.join(demods.keys()),))
179    print "inside rx option parser"
180    receive_path.add_options(parser_rx, expert_grp_rx)
181
182    for mod in demods.values():
183        mod.add_options(expert_grp_rx)
184
185    (options_rx, args_rx) = parser_rx.parse_args ()
186
187    if len(args_rx) != 0:
188        parser_rx.print_help(sys.stderr)
189        sys.exit(1)
190    ############# Setting some default values for rx side of the block
191    options_rx.rx_freq = 462.5625e6 #setting default rx_freq value
192    options_rx.samples_per_symbol =  2
193    options_rx.modulation = 'dbpsk'
194    options_rx.fusb_block_size = 4096
195    options_rx.fusb_nblocks = 16
196    options_rx.bitrate = 0.0125e6
197    #############
198    if options_rx.rx_freq is None:
199        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
200        parser_rx.print_help(sys.stderr)
201        sys.exit(1)
202    print "printing rx options",options_rx,"&&&&\n"
203
204
205    # build the graph
206
207    tb = my_top_block(mods[options_tx.modulation],
208                     demods[options_rx.modulation],
209                     rx_callback,options_tx,
210                     options_rx)
211    r = gr.enable_realtime_scheduling()
212    if r != gr.RT_OK:
213       print "Warning: failed to enable realtime scheduling"
214
215    tb.start()
216    #tb.rxpath.get_spec_stats(10) #10 is the number of fft frames that we are looking at to determine the average power
217    # generate and send packets
218    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
219   
220    #n = 0
221    #pktno = 0
222    #pkt_size = int(options_tx.size)
223    #options_tx.megabytes = 0.000048
224   
225    #options_tx.size = 6 #2 bytes for packet size,2 bytes for the synchronization request(including redundant bit) and 2 bits to specify the channel number(including redundant bit)
226   
227   
228    myfile = file("logfile.txt", 'w')
229    myfile.write("")
230    #sending sync packet-format
231    global ch
232    check =True
233    #ch = random.randint(1, 7)#which channel to send information on...this is the second data packet of the synchronization frame
234    #resend = 5; #number of times a batch of sync packets is sent on a channel before trying a different channel
235    running = True
236    #resend_count= 0 #initializing resend count
237    sync_pktno = 0
238    ch = int(random.choice('17'))
239    while running:
240        ################################################sync mode####################################
241        if mode == "sync":
242                #nbytes = int(1e6 * .0003)
243                #nbytes = int(1e6 * .0003)
244                nbytes = 6
245                pkt_size = 6
246                data_pktno = 0
247                print "printing packet size",pkt_size,"pkt size\n"
248                while sync_status != True:
249                        #ch = random.randint(1, 7)
250                        ch = int(random.choice('17'))
251                       
252                        #ch = random.randint(6,12)
253                        #ch = 1
254                        hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
255                        tb.rxpath.min_freq = hop_freq - 80e3
256                        tb.rxpath.max_freq = hop_freq + 80e3
257                        is_present = tb.rxpath.get_spec_stats(1e13,1) #check if primary user is present
258                        if is_present == True: #if primary user is there then dont transmit on this channel
259                                continue
260                                       
261                        #print "printing status ",status,"\n"
262                        #print "primary user present = ",is_present,"\n"
263                        #tb.rxpath.get_spec_stats(10) 
264                        print "hop freq is ",hop_freq
265                       
266                        #hop_freq = float(1e6 * (462.5625+(5-1)*0.05))#setting the centre freq frequency for sending packets
267                        tb.txpath.set_freq(hop_freq)
268                        tb.rxpath.set_freq(hop_freq)
269                        #print "inside while pppp printing sync channel",sync_status,"  done"
270                        #if sync_status == "True":
271                        #print "synchronization done..data channel is found"
272                        #break
273                        #else:
274                        #print "inside else1"
275                        n = 0
276                       
277                        print >> myfile, "out"
278                        while n < nbytes:
279                                print >> myfile, "inside while"
280                                if options_tx.from_file is None:
281                                        #data = (pkt_size - 2) * chr(sync_pktno & 0xff) #0xff is 255
282                                        data = 's'+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
283                                        #print >> myfile, data
284                                        #print "printing data",data,"****\n"
285                                else:
286                                        data = source_file.read(pkt_size - 2)
287                                        if data == '':
288                                                break;
289       
290                                payload = struct.pack('!H', sync_pktno & 0xffff) + data
291                                #print "printing payload",payload,"**\n"
292                               
293                                send_pkt(tb,payload)
294                                #print "printing payload",payload,"**\n"
295                                n += len(payload)
296                                sys.stderr.write('.')
297                                if options_tx.discontinuous and sync_pktno % 5 == 4:
298                                        time.sleep(1)
299                                sync_pktno += 1
300                                #print "before sleeping for 10 seconds and value of resend count is",resend_count
301       
302
303                        time.sleep(0.05)
304                        #time.sleep(2)
305                        #resend_count += 1
306           
307                print "sync channel found! and it is channel ",ch,"\n"
308                mode = "traffic"
309                 #send_pkt(tb,eof=True)
310                #myfile.close()
311                #tb.wait()
312        ################################################end of sync mode####################################
313
314        ################################################Communications mode####################################
315        if mode == "traffic":
316                print "inside traffic mode \n"
317                nbytes = 15
318                pkt_size = 15
319                sync_pktno = 0
320                while n < nbytes:
321                        #print >> myfile, "inside while"
322                        if options_tx.from_file is None:
323                                #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
324                                data = 'dHi how are you' #Sending this message
325                                #print >> myfile, data
326                                #print "printing data",data,"****\n"
327                        else:
328                                data = source_file.read(pkt_size - 2)
329                                if data == '':
330                                        break;
331       
332                       
333                        payload = struct.pack('!15s', data)
334                        #print "printing payload",payload,"**\n"
335                               
336                        send_pkt(tb,payload)
337                        #print "printing payload",payload,"**\n"
338                        n += len(payload)
339                        sys.stderr.write('.')
340                        if options_tx.discontinuous and data_pktno % 5 == 4:
341                                time.sleep(1)
342                        data_pktno += 1
343                        #print "before sleeping for 10 seconds and value of resend count is",resend_count
344                        #time.sleep(0.1)
345                        tb.rxpath.min_freq = hop_freq - 80e3
346                        tb.rxpath.max_freq = hop_freq + 80e3
347                        is_present = tb.rxpath.get_spec_stats(1e13,10) #check if primary user is present
348                        if is_present == True: #if primary user is present then get out of this communication channel, back to sync mode.
349                                mode = "sync"
350                               
351        ################################################ end of Communications mode####################################         
352
353
354
355if __name__ == '__main__':
356    try:
357        main()
358    except KeyboardInterrupt:
359        pass
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402   
403
404
405
Note: See TracBrowser for help on using the browser.