Changeset 367

Show
Ignore:
Timestamp:
07/25/09 17:53:13 (15 years ago)
Author:
sriram
Message:

Working dsa code for node 1

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/sriram/benchmark_txrxnode1.py

    r365 r367  
    44from gnuradio import gr, gru, modulation_utils 
    55from gnuradio import eng_notation 
    6 ##########spectrum sense specific imports 
    7 from gnuradio import optfir, window 
    8 from gnuradio import usrp 
    9 #from gnuradio.eng_option import eng_option 
    10 #from usrpm import usrp_dbid 
    11 ########## 
    126from gnuradio.eng_option import eng_option 
    137from optparse import OptionParser 
     
    1812from transmit_path import transmit_path 
    1913from receive_path import receive_path 
    20 #import spec_sense_path 
    21  
    22 global sync_status 
    23 sync_status = False #default value is "False"... 
    24 global 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 
    29 mode = "sync" #default mode of operation... 
     14global sync_status,mode,ch 
     15sync_status = "False" 
     16mode = "sync"#default mode is sync 
    3017class my_top_block(gr.top_block): 
    3118 
     
    3825        self.connect(self.txpath); 
    3926        self.connect(self.rxpath); 
    40  
    4127         
    4228#//////main//////   
    4329 
    44 global n_rcvd, n_right,hop_freq 
     30global n_rcvd, n_right 
    4531 
    4632def main(): 
    47     global n_rcvd, n_right,sync_status,mode,hop_freq 
     33    global n_rcvd, n_right,sync_status,mode,ch 
    4834    n_rcvd = 0 
    4935    n_right = 0 
     
    5238 
    5339    def rx_callback(ok, payload): 
    54  
    55          
    5640        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         ##################################################### 
     41        global n_rcvd, n_right, sync_status,mode,ch 
     42        ############################################################## 
    6443        if mode == "sync": 
    6544                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         ##################################################### 
     45                        (pktno,) = struct.unpack('!H', payload[0:2]) 
     46                        (sync_signal,) = struct.unpack('!s', payload[2])  
     47                        (data_channel,) = struct.unpack('!H', payload[3:5]) 
     48                        print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",data_channel," !!!!!\n"     
     49                        if str(sync_signal) == 'o' and str(data_channel) == str(ch): 
     50                                #print "inside if2" 
     51                                sync_status = "True" 
     52                                #tb.stop() 
     53                                #print "after tb stop ans sync status is ",sync_status," sync status\n" 
     54                    
     55        ############################################################## 
    9156         
    9257        ######################### traffic ############################ 
    9358        if mode == "traffic": 
    94                 print "Mode is ",mode,"received traffic packet\n" 
     59                 
    9560                if ok: 
    9661                        (data_header,) = struct.unpack('!s', payload[0]) 
    9762                        if data_header == 'd': 
    98                                 (comm,) = struct.unpack('!14s', payload[1:15]) 
    99                                 print "received this ", comm,"\n" 
    100                 else : 
    101                         print"not ok" 
     63                                comm = struct.unpack('!14s', payload[1:15]) 
     64                                print("received this ", comm)  
    10265         
    10366        ############################################################## 
    104          
    105  
    10667 
    10768        n_rcvd += 1 
     
    11071 
    11172        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % ( 
    112             #ok, pktno, n_rcvd, n_right) 
     73        #    ok, pktno, n_rcvd, n_right) 
    11374 
    11475    mods = modulation_utils.type_1_mods() 
     
    139100 
    140101    (options_tx, args_tx) = parser_tx.parse_args () 
    141  
     102    ############# Setting some default values for tx side of the block 
     103    options_tx.tx_freq = 480e6#462.5625e6  
     104    options_tx.samples_per_symbol =  2 
     105    options_tx.modulation = 'dbpsk' 
     106    #options_tx.fusb_block_size = 4096 
     107    #options_tx.fusb_nblocks = 16 
     108    options_tx.bitrate = 0.0125e6 
     109    ############# 
    142110    if len(args_tx) != 0: 
    143111        parser_tx.print_help() 
    144112        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     ############# 
     113 
    153114    if options_tx.tx_freq is None: 
    154115        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
     
    161122    # build the graph 
    162123    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: 
     124 
    172125    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
    173126    print "printing parser_rx",parser_rx,"!!!!!\n" 
     
    191144        sys.exit(1) 
    192145    ############# Setting some default values for rx side of the block 
    193     options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
     146    options_rx.rx_freq = 480e6#462.5625e6 #setting default rx_freq value 
    194147    options_rx.samples_per_symbol =  2 
    195148    options_rx.modulation = 'dbpsk' 
    196     options_rx.fusb_block_size = 4096 
    197     options_rx.fusb_nblocks = 16 
     149    #options_rx.fusb_block_size = 4096 
     150    #options_rx.fusb_nblocks = 16 
    198151    options_rx.bitrate = 0.0125e6 
    199152    ############# 
     
    204157    print "printing rx options",options_rx,"&&&&\n" 
    205158 
    206  
    207     # build the graph 
     159# build the graph 
    208160 
    209161    tb = my_top_block(mods[options_tx.modulation], 
    210                      demods[options_rx.modulation], 
    211                      rx_callback,options_tx, 
    212                      options_rx) 
     162                      demods[options_rx.modulation], 
     163                      rx_callback,options_tx, 
     164                      options_rx) 
    213165    r = gr.enable_realtime_scheduling() 
    214166    if r != gr.RT_OK: 
    215        print "Warning: failed to enable realtime scheduling" 
     167        print "Warning: failed to enable realtime scheduling" 
    216168 
    217169    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      
     170    #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats) 
     171 
     172    nbytes = 5 #int(1e6 * .0003) 
    227173    #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      
     174    pkt_size = 5 
     175    print "printing packet size",pkt_size,"pkt size\n" 
    229176     
    230177    myfile = file("logfile.txt", 'w') 
     
    232179    #sending sync packet-format  
    233180    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 
    237181    running = True 
    238     #resend_count= 0 #initializing resend count 
    239     sync_pktno = 0 
    240     #ch = int(random.choice('17')) 
     182     
    241183    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 
     184         
     185        ################################################sync mode#################################### 
     186        if mode == "sync": 
     187                if sync_status != "True": 
     188                        ch = int(random.choice([1,7,8,14])) 
     189                        if ch < 8: 
     190                                hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets 
     191                        else: 
     192                                hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets 
     193                         
     194             
    271195                        tb.txpath.set_freq(hop_freq) 
    272196                        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" 
     197                         
     198                        k = 0 
     199                        total_energy = 0  
     200                        while k <  1: 
     201                                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     202                                total_energy = total_energy + ch_energy 
     203                                k+=1 
     204                         
     205                        avg_energy = total_energy/1 
     206                        print "printing hop frequency ",hop_freq,"and energy is ",avg_energy,"\n" 
     207                        if int(avg_energy) > 1.50e8: #if primary user is there then dont transmit on this channel 
     208                                #while  
     209                                continue 
     210                                #print "present" 
     211                         
     212 
    279213                        n = 0 
    280                          
    281                         print >> myfile, "out" 
     214                        pktno = 0 
    282215                        while n < nbytes: 
    283                                 #print >> myfile, "inside while" 
     216                         
    284217                                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                                  
     218                                        #data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255 
     219                                        data = 's'#+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel 
     220                                else: 
     221                                        data = source_file.read(pkt_size - 2) 
     222                                        if data == '': 
     223                                                break; 
     224 
     225                                payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
     226                                #print "printing payload",payload,"**\n" 
     227         
    297228                                send_pkt(tb,payload) 
    298                                 #print "printing payload",payload,"**\n" 
    299229                                n += len(payload) 
    300230                                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() 
     231                                if options_tx.discontinuous and pktno % 5 == 4: 
     232                                        time.sleep(1) 
     233                                        pktno += 1 
     234                        time.sleep(0.05) 
     235                #resend_count += 1  
     236                else: 
     237                        print "sync channel found..channel ",ch,"\n"   
     238                        mode = "traffic" 
     239                        sync_status = "False" 
    316240        ################################################end of sync mode#################################### 
    317241 
    318         ################################################Communications mode#################################### 
     242        ################################################Communications mode#################################### 
    319243        if mode == "traffic": 
    320244                print "inside traffic mode \n" 
    321245                nbytes = 15 
    322246                pkt_size = 15 
    323                 sync_pktno = 0 
     247                data_pktno = 0 
    324248                n = 0 
    325249                while n < nbytes: 
     
    340264                                 
    341265                        send_pkt(tb,payload) 
    342                         time.sleep(0.5) 
    343266                        #print "printing payload",payload,"**\n" 
    344267                        n += len(payload) 
     
    348271                        data_pktno += 1 
    349272                        #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. 
     273                        time.sleep(0.2) 
     274                        ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     275                        if int(ch_energy) > 1.50e8: #if primary user is there then dont transmit on this channel 
    355276                                mode = "sync" 
    356                                 sync_status = False 
    357                                 print "Moving out of this channel..Primary user detected\n" 
     277                                continue 
     278                                 
    358279        ################################################ end of Communications mode####################################          
    359  
    360  
     280         
     281     
    361282 
    362283if __name__ == '__main__': 
     
    392313 
    393314 
    394  
    395  
    396  
    397  
    398  
    399  
    400  
    401  
    402  
    403  
    404  
    405  
    406  
    407  
    408  
    409315     
    410316