Changeset 384

Show
Ignore:
Timestamp:
07/27/09 18:57:37 (15 years ago)
Author:
sriram
Message:

Adding the get_freq function which gets the new frequency from the cognitive engine

Files:
1 modified

Legend:

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

    r383 r384  
    88from numpy import random 
    99import random, time, struct, sys, math 
     10from datetime import datetime 
    1011 
    1112# from current dir 
     
    3132def main(): 
    3233        print "inside main" 
    33         global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts 
     34        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
    3435        n_rcvd = 0 
    3536        n_right = 0 
    3637        n_attempts = 5 
     38        return_flag = 0 
    3739        def send_pkt(self, payload='', eof=False): 
    3840                return self.txpath.send_pkt(payload, eof) 
    39  
     41         
     42        def get_freq(hop_freq,probe_level,absent_time): 
     43 
     44                channel = int(random.choice([1,7,8,14])) 
     45                if channel < 8: 
     46                        hop_freq = float(1e6 * (462.5625+(channel-1)*0.025))#setting the centre freq frequency for sending packets 
     47                else: 
     48                        hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets       
     49 
     50                return channel,hop_freq #returning the channel number and hop frequency 
     51         
    4052 
    4153        def rx_callback(ok, payload): 
     
    129141         
    130142        ############# Setting some default values for tx side of the block 
    131         options_tx.tx_freq = 470e6#462.5625e6 
     143        options_tx.tx_freq = 462.5625e6 
    132144        options_tx.samples_per_symbol =  2 
    133145        options_tx.modulation = 'dbpsk' 
     
    169181                sys.exit(1) 
    170182        ############# Setting some default values for rx side of the block 
    171         options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value 
     183        options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
    172184        options_rx.samples_per_symbol =  2 
    173185        options_rx.modulation = 'dbpsk' 
     
    200212        global ch 
    201213        running = True 
    202      
     214        ch_energy = tb.rxpath.probe.level() #setting initial value 
     215        hop_freq = options_tx.tx_freq #  = options_rx.rx_freq...same for tx and rx side 
    203216        while running: 
    204  
    205217 
    206218                ################################################sync mode#################################### 
    207219                if mode == "sync": 
    208220                        if sync_status != True: 
    209                                 ch = int(random.choice([1,7,8,14])) 
    210                                 if ch < 8: 
    211                                         hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets 
    212                                 else: 
    213                                         hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets 
    214                          
    215              
    216                                 tb.txpath.set_freq(hop_freq) 
     221                                 
     222                                if return_flag == 0: 
     223                                        ch,hop_freq = get_freq(hop_freq,ch_energy,0) 
     224                                else: 
     225                                        ch,hop_freq = get_freq(hop_freq,ch_energy,elapsed_time) 
     226                                        return_flag = 0 
     227 
     228                                tb.txpath.set_freq(hop_freq) 
    217229                                tb.rxpath.set_freq(hop_freq) 
    218                          
    219                                 k = 0 
    220                                 total_energy = 0  
    221                                 while k <  1: 
    222                                         ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    223                                         total_energy = total_energy + ch_energy 
    224                                         k+=1 
    225                          
    226                                 avg_energy = total_energy/1 
    227                                 print "printing hop frequency ",hop_freq,"and energy is ",avg_energy,"\n" 
    228                                 if int(avg_energy) > 2e7: #if primary user is there then dont transmit on this channel 
     230                 
     231                                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     232                                 
     233                                print "printing hop frequency ",hop_freq,"and energy is ",ch_energy,"\n" 
     234                                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
    229235                                        #while  
    230236                                        continue 
    231237                                        #print "present" 
    232                          
    233                                 nbytes = 5 #int(1e6 * .0003) 
     238                                 
     239                                nbytes = 5 #int(1e6 * .0003) 
    234240                                pkt_size = 5 
    235241                                n = 0 
     
    262268                                traffic_flag = False 
    263269                                sync_status="False" 
     270                                start_time = datetime.now() #measuring the time for which the primary user is away 
    264271         
    265272                ################################################end of sync mode#################################### 
     
    297304                                data_pktno += 1 
    298305                                #print "before sleeping for 10 seconds and value of resend count is",resend_count 
    299                                 time.sleep(0.1 + 0.05*int(random.choice([0,1,2,3]))) 
     306                                time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) 
    300307 
    301308                                if traffic_flag != True: 
     
    307314                                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    308315                                print "channel energy is ",ch_energy,"\n" 
    309                                 if int(ch_energy) > 2e7: #if primary user is there then dont transmit on this channel 
     316                                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
     317                                        stop_time = datetime.now()       
     318                                        _elapsed_time  = start_time - stop_time 
     319                                        elapsed_time = _elapsed_time.seconds 
    310320                                        mode = "sync" 
     321                                        return_flag = 1 
    311322                 
    312323