Changeset 367
- Timestamp:
- 07/25/09 17:53:13 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/branches/sriram/benchmark_txrxnode1.py
r365 r367 4 4 from gnuradio import gr, gru, modulation_utils 5 5 from gnuradio import eng_notation 6 ##########spectrum sense specific imports7 from gnuradio import optfir, window8 from gnuradio import usrp9 #from gnuradio.eng_option import eng_option10 #from usrpm import usrp_dbid11 ##########12 6 from gnuradio.eng_option import eng_option 13 7 from optparse import OptionParser … … 18 12 from transmit_path import transmit_path 19 13 from 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... 14 global sync_status,mode,ch 15 sync_status = "False" 16 mode = "sync"#default mode is sync 30 17 class my_top_block(gr.top_block): 31 18 … … 38 25 self.connect(self.txpath); 39 26 self.connect(self.rxpath); 40 41 27 42 28 #//////main////// 43 29 44 global n_rcvd, n_right ,hop_freq30 global n_rcvd, n_right 45 31 46 32 def main(): 47 global n_rcvd, n_right,sync_status,mode, hop_freq33 global n_rcvd, n_right,sync_status,mode,ch 48 34 n_rcvd = 0 49 35 n_right = 0 … … 52 38 53 39 def rx_callback(ok, payload): 54 55 56 40 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 ############################################################## 64 43 if mode == "sync": 65 44 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 ############################################################## 91 56 92 57 ######################### traffic ############################ 93 58 if mode == "traffic": 94 print "Mode is ",mode,"received traffic packet\n"59 95 60 if ok: 96 61 (data_header,) = struct.unpack('!s', payload[0]) 97 62 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) 102 65 103 66 ############################################################## 104 105 106 67 107 68 n_rcvd += 1 … … 110 71 111 72 #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) 113 74 114 75 mods = modulation_utils.type_1_mods() … … 139 100 140 101 (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 ############# 142 110 if len(args_tx) != 0: 143 111 parser_tx.print_help() 144 112 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 153 114 if options_tx.tx_freq is None: 154 115 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") … … 161 122 # build the graph 162 123 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 172 125 parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 173 126 print "printing parser_rx",parser_rx,"!!!!!\n" … … 191 144 sys.exit(1) 192 145 ############# Setting some default values for rx side of the block 193 options_rx.rx_freq = 4 62.5625e6 #setting default rx_freq value146 options_rx.rx_freq = 480e6#462.5625e6 #setting default rx_freq value 194 147 options_rx.samples_per_symbol = 2 195 148 options_rx.modulation = 'dbpsk' 196 options_rx.fusb_block_size = 4096197 options_rx.fusb_nblocks = 16149 #options_rx.fusb_block_size = 4096 150 #options_rx.fusb_nblocks = 16 198 151 options_rx.bitrate = 0.0125e6 199 152 ############# … … 204 157 print "printing rx options",options_rx,"&&&&\n" 205 158 206 207 # build the graph 159 # build the graph 208 160 209 161 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) 213 165 r = gr.enable_realtime_scheduling() 214 166 if r != gr.RT_OK: 215 print "Warning: failed to enable realtime scheduling"167 print "Warning: failed to enable realtime scheduling" 216 168 217 169 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) 227 173 #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" 229 176 230 177 myfile = file("logfile.txt", 'w') … … 232 179 #sending sync packet-format 233 180 global ch 234 check =True235 #ch = random.randint(1, 7)#which channel to send information on...this is the second data packet of the synchronization frame236 #resend = 5; #number of times a batch of sync packets is sent on a channel before trying a different channel237 181 running = True 238 #resend_count= 0 #initializing resend count 239 sync_pktno = 0 240 #ch = int(random.choice('17')) 182 241 183 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 271 195 tb.txpath.set_freq(hop_freq) 272 196 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 279 213 n = 0 280 281 print >> myfile, "out" 214 pktno = 0 282 215 while n < nbytes: 283 #print >> myfile, "inside while"216 284 217 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 297 228 send_pkt(tb,payload) 298 #print "printing payload",payload,"**\n"299 229 n += len(payload) 300 230 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" 316 240 ################################################end of sync mode#################################### 317 241 318 ################################################Communications mode####################################242 ################################################Communications mode#################################### 319 243 if mode == "traffic": 320 244 print "inside traffic mode \n" 321 245 nbytes = 15 322 246 pkt_size = 15 323 sync_pktno = 0247 data_pktno = 0 324 248 n = 0 325 249 while n < nbytes: … … 340 264 341 265 send_pkt(tb,payload) 342 time.sleep(0.5)343 266 #print "printing payload",payload,"**\n" 344 267 n += len(payload) … … 348 271 data_pktno += 1 349 272 #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 355 276 mode = "sync" 356 sync_status = False357 print "Moving out of this channel..Primary user detected\n"277 continue 278 358 279 ################################################ end of Communications mode#################################### 359 360 280 281 361 282 362 283 if __name__ == '__main__': … … 392 313 393 314 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 315 410 316