Changeset 369
- Timestamp:
- 07/25/09 17:55:54 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/branches/sriram/benchmark_txrxnode2.py
r364 r369 8 8 from numpy import random 9 9 import random, time, struct, sys, math 10 import traceback 10 11 11 # from current dir 12 12 from transmit_path import transmit_path 13 13 from receive_path import receive_path 14 global sync_status,mode 15 sync_status=False 16 mode = "sync" #default mode of operation... 14 global sync_status,mode,traffic_flag,ch 15 sync_status="False" 16 mode = "sync" #default mode 17 traffic_flag = False 17 18 class my_top_block(gr.top_block): 18 19 … … 28 29 #//////main////// 29 30 30 global n_rcvd, n_right,ch,traffic_flag 31 traffic_flag = False 31 global n_rcvd, n_right 32 32 33 33 def 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 36 35 n_rcvd = 0 37 36 n_right = 0 … … 40 39 41 40 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 ########################### 46 43 if mode == "sync": 47 44 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 ##################################################### 76 58 77 59 ######################### traffic ############################ 78 60 if mode == "traffic": 79 80 if ok: 81 print "received a packet" 61 if ok: 82 62 (data_header,) = struct.unpack('!s', payload[0]) 83 63 if data_header == 'd': 84 64 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) 87 67 data = 'dI am fine.....' #Sending this message 88 68 payload = struct.pack('!15s', data) 89 69 send_pkt(tb,payload) 90 91 else:92 print "not ok"93 94 70 ############################################################## 95 71 96 97 98 n_rcvd += 1 72 n_rcvd += 1 99 73 if ok: 100 74 n_right += 1 … … 102 76 #print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( 103 77 # ok, pktno, n_rcvd, n_right) 104 #print "ok = %5s n_rcvd = %4d n_right = %4d" % (105 # ok, n_rcvd, n_right)106 107 78 #print "sync_status is ",sync_status," @@\n" 108 79 … … 140 111 sys.exit(1) 141 112 ############# Setting some default values for tx side of the block 142 options_tx.tx_freq = 4 62.5625e6113 options_tx.tx_freq = 470e6#462.5625e6 143 114 options_tx.samples_per_symbol = 2 144 115 options_tx.modulation = 'dbpsk' … … 147 118 options_tx.bitrate = 0.0125e6 148 119 ############# 120 149 121 if options_tx.tx_freq is None: 150 122 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") … … 157 129 # build the graph 158 130 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 168 132 parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 169 133 print "printing parser_rx",parser_rx,"!!!!!\n" … … 187 151 sys.exit(1) 188 152 ############# Setting some default values for rx side of the block 189 options_rx.rx_freq = 4 62.5625e6 #setting default rx_freq value153 options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value 190 154 options_rx.samples_per_symbol = 2 191 155 options_rx.modulation = 'dbpsk' … … 194 158 options_rx.bitrate = 0.0125e6 195 159 ############# 160 196 161 if options_rx.rx_freq is None: 197 162 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") … … 199 164 sys.exit(1) 200 165 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 204 168 205 169 tb = my_top_block(mods[options_tx.modulation], … … 210 174 if r != gr.RT_OK: 211 175 print "Warning: failed to enable realtime scheduling" 212 print "printing the decimation ",options_rx.decim,"\n"176 213 177 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) 214 180 #listening to random frequencies untill a match is found 215 181 running = True … … 218 184 ################################################sync mode#################################### 219 185 if mode == "sync": 220 221 if sync_status != True: 186 if sync_status != "True": 222 187 #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 238 195 tb.txpath.set_freq(hop_freq) 239 196 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 240 201 time.sleep(0.05) 241 #time.sleep(2)242 202 else: 243 print " inside lese of while check"203 print "sync channel is found...channel ",ch,"\n" 244 204 check_sync = 0 245 traffic_flag = False246 print "sync channel is found! and it is channel ",ch," \n"247 205 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#################################### 259 213 if mode == "traffic": 260 214 print "Inside traffic mode" 261 ##################### for ensuring sync #####################262 215 if check_sync == 0: 263 time.sleep(0. 4)216 time.sleep(0.8) 264 217 if traffic_flag != True: 265 218 mode = "sync" 266 sync_status=False267 219 continue 268 220 else: 269 check_sync == 1 270 ############################################################ 271 221 check_sync = 1 272 222 #nbytes = 15 273 223 #pkt_size = 15 274 # data_pktno = 0275 #n = 224 #sync_pktno = 0 225 #n =0 276 226 #while n < nbytes: 277 ##print >> myfile, "inside while"227 #print >> myfile, "inside while" 278 228 # if options_tx.from_file is None: 279 ##data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255280 #data = 'dI am fine.....' #Sending this message281 ##print >> myfile, data282 ##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" 283 233 # else: 284 234 # data = source_file.read(pkt_size - 2) 285 235 # if data == '': 286 236 # break; 287 #237 288 238 # 289 239 # payload = struct.pack('!15s', data) … … 291 241 292 242 # send_pkt(tb,payload) 293 # time.sleep(0.5)294 243 #print "printing payload",payload,"**\n" 295 244 # n += len(payload) … … 298 247 # time.sleep(1) 299 248 # 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 306 253 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 323 259 324 260 #tb.wait() 325 #tb.stop() 326 261 327 262 if __name__ == '__main__': 328 263 try: