Changeset 369

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

Working dsa code for node2

Files:
1 modified

Legend:

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

    r364 r369  
    88from numpy import random 
    99import random, time, struct, sys, math 
    10 import traceback 
     10 
    1111# from current dir 
    1212from transmit_path import transmit_path 
    1313from receive_path import receive_path 
    14 global sync_status,mode 
    15 sync_status=False 
    16 mode = "sync" #default mode of operation... 
     14global sync_status,mode,traffic_flag,ch 
     15sync_status="False" 
     16mode = "sync" #default mode 
     17traffic_flag = False 
    1718class my_top_block(gr.top_block): 
    1819 
     
    2829#//////main//////  
    2930 
    30 global n_rcvd, n_right,ch,traffic_flag 
    31 traffic_flag = False 
     31global n_rcvd, n_right 
    3232 
    3333def main(): 
    34     global n_rcvd, n_right,sync_status,sync_pktno,ch,mode,traffic_flag 
    35     sync_pktno =0 
     34    global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 
    3635    n_rcvd = 0 
    3736    n_right = 0 
     
    4039 
    4140    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 ########################### 
     41        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 
     42        ########################## sync ########################### 
    4643        if mode == "sync": 
    4744                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         ##################################################### 
     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) == 's' and str(data_channel) == str(ch):  
     50                                sync_status = "True" 
     51                                print "received a sync packet on channel %s\n" %(data_channel) 
     52                                data = 'o' 
     53                                pktno=0 
     54                                ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
     55                                send_pkt(tb,ack_payload) #sending back the acknowledgement 
     56 
     57        ##################################################### 
    7658         
    7759        ######################### traffic ############################ 
    7860        if mode == "traffic": 
    79                  
    80                 if ok: 
    81                         print "received a packet" 
     61                if ok:  
    8262                        (data_header,) = struct.unpack('!s', payload[0]) 
    8363                        if data_header == 'd': 
    8464                                traffic_flag = True 
    85                                 (comm,) = struct.unpack('!14s', payload[1:15]) 
    86                                 print "received this ", comm,"\n" 
     65                                comm = struct.unpack('!14s', payload[1:15]) 
     66                                print("received this ", comm)  
    8767                                data = 'dI am fine.....' #Sending this message 
    8868                                payload = struct.pack('!15s', data) 
    8969                                send_pkt(tb,payload) 
    90                  
    91                 else: 
    92                         print "not ok" 
    93          
    9470        ############################################################## 
    9571 
    96  
    97  
    98         n_rcvd += 1 
     72        n_rcvd += 1 
    9973        if ok: 
    10074            n_right += 1 
     
    10276        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % ( 
    10377        #    ok, pktno, n_rcvd, n_right) 
    104         #print "ok = %5s  n_rcvd = %4d  n_right = %4d" % ( 
    105 #            ok, n_rcvd, n_right) 
    106  
    10778        #print "sync_status is ",sync_status," @@\n" 
    10879 
     
    140111        sys.exit(1) 
    141112    ############# Setting some default values for tx side of the block 
    142     options_tx.tx_freq = 462.5625e6  
     113    options_tx.tx_freq = 470e6#462.5625e6 
    143114    options_tx.samples_per_symbol =  2 
    144115    options_tx.modulation = 'dbpsk' 
     
    147118    options_tx.bitrate = 0.0125e6 
    148119    ############# 
     120 
    149121    if options_tx.tx_freq is None: 
    150122        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
     
    157129    # build the graph 
    158130    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: 
     131     
    168132    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
    169133    print "printing parser_rx",parser_rx,"!!!!!\n" 
     
    187151        sys.exit(1) 
    188152    ############# Setting some default values for rx side of the block 
    189     options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
     153    options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value 
    190154    options_rx.samples_per_symbol =  2 
    191155    options_rx.modulation = 'dbpsk' 
     
    194158    options_rx.bitrate = 0.0125e6 
    195159    ############# 
     160 
    196161    if options_rx.rx_freq is None: 
    197162        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
     
    199164        sys.exit(1) 
    200165    print "printing rx options",options_rx,"&&&&\n" 
    201     #options_rx.decim=128    #setting the decimation for receive path 
    202      
    203 # build the graph   
     166 
     167# build the graph 
    204168 
    205169    tb = my_top_block(mods[options_tx.modulation], 
     
    210174    if r != gr.RT_OK: 
    211175        print "Warning: failed to enable realtime scheduling" 
    212     print "printing the decimation ",options_rx.decim,"\n" 
     176     
    213177    tb.start() 
     178    #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats) 
     179    #tb.rxpath.connect(tb.rxpath.u, tb.rxpath.s2v) 
    214180    #listening to random frequencies untill a match is found 
    215181    running = True 
     
    218184        ################################################sync mode#################################### 
    219185        if mode == "sync": 
    220                  
    221                 if sync_status != True: 
     186                if sync_status != "True": 
    222187                        #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                          
     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                        print "hop freq is",hop_freq,"@@@@\n" 
     194                         
    238195                        tb.txpath.set_freq(hop_freq) 
    239196                        tb.rxpath.set_freq(hop_freq) 
     197                        ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     198                        print "channel energy is ",ch_energy,"\n" 
     199                        if int(ch_energy) > 1.50e8: #if primary user is there then dont transmit on this channel 
     200                                continue 
    240201                        time.sleep(0.05) 
    241                         #time.sleep(2) 
    242202                else: 
    243                         print "inside lese of while check" 
     203                        print "sync channel is found...channel ",ch,"\n" 
    244204                        check_sync = 0 
    245                         traffic_flag  = False 
    246                         print "sync channel is found! and it is channel ",ch," \n"   
    247205                        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####################################  
     206                        traffic_flag = False 
     207                        sync_status="False" 
     208                        continue 
     209     
     210    ################################################end of sync mode#################################### 
     211 
     212    ################################################Communications mode####################################      
    259213        if mode == "traffic": 
    260214                print "Inside traffic mode" 
    261                 ##################### for ensuring sync ##################### 
    262215                if check_sync == 0: 
    263                         time.sleep(0.4) 
     216                        time.sleep(0.8) 
    264217                        if traffic_flag != True: 
    265218                                mode = "sync" 
    266                                 sync_status=False 
    267219                                continue 
    268220                        else: 
    269                                 check_sync == 1  
    270                 ############################################################ 
    271  
     221                                check_sync = 1   
    272222                #nbytes = 15 
    273223                #pkt_size = 15 
    274                 #data_pktno = 0 
    275                 #n = 0 
     224                #sync_pktno = 0 
     225                #n =0 
    276226                #while n < nbytes: 
    277                 #       #print >> myfile, "inside while" 
     227                        #print >> myfile, "inside while" 
    278228                #       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" 
     229                                #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255 
     230                #               data = 'dI am fine.....' #Sending this message 
     231                                #print >> myfile, data 
     232                                #print "printing data",data,"****\n" 
    283233                #       else: 
    284234                #               data = source_file.read(pkt_size - 2) 
    285235                #               if data == '': 
    286236                #                       break; 
    287                 # 
     237         
    288238                #        
    289239                #       payload = struct.pack('!15s', data) 
     
    291241                                 
    292242                #       send_pkt(tb,payload) 
    293                 #       time.sleep(0.5) 
    294243                        #print "printing payload",payload,"**\n" 
    295244                #       n += len(payload) 
     
    298247                #               time.sleep(1) 
    299248                #       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. 
     249                #       #print "before sleeping for 10 seconds and value of resend count is",resend_count 
     250                time.sleep(0.2) 
     251                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     252                if int(ch_energy) > 1.50e8: #if primary user is there then dont transmit on this channel 
    306253                        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) 
     254                        continue 
     255         
     256    ################################################ end of Communications mode####################################      
     257 
     258 
    323259 
    324260    #tb.wait() 
    325     #tb.stop() 
    326  
     261     
    327262if __name__ == '__main__': 
    328263    try: