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

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