root/vtcross/branches/sriram/benchmark_txrxnode1.py @ 339

Revision 339, 12.3 KB (checked in by sriram, 15 years ago)

Changes

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#Transmission and reception, one ata a time on the same antena
3
4from gnuradio import gr, gru, modulation_utils
5from gnuradio import eng_notation
6##########spectrum sense specific imports
7from gnuradio import optfir, window
8from gnuradio import usrp
9#from gnuradio.eng_option import eng_option
10#from usrpm import usrp_dbid
11##########
12from gnuradio.eng_option import eng_option
13from optparse import OptionParser
14from numpy import random
15import random, time, struct, sys, math
16
17# from current dir
18from transmit_path import transmit_path
19from receive_path import receive_path
20#import spec_sense_path
21
22global sync_status
23sync_status = False #default value is "False"...
24global 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
29mode = "sync" #default mode of operation...
30class my_top_block(gr.top_block):
31
32    def __init__(self, mod_class, demod_class,
33                 rx_callback, options_tx,options_rx):
34
35        gr.top_block.__init__(self)
36        self.rxpath = receive_path(demod_class, rx_callback, options_rx)
37        self.txpath = transmit_path(mod_class, options_tx)
38       
39        ########connect for the spectrum sense#########
40        #default_min_freq = 462.4825e6 #80 Khz to the left of channel 1 (462.5625e6) in frs band
41        #default_max_freq = 462.6425e6 #80 Khz to the right of channel 1 (462.5625e6) in frs band
42        #self.sp_sense_path = spec_sense_path.spec_sense_top_block(default_min_freq,default_min_freq)
43        #self.connect(self.sp_sense_path.u, self.sp_sense_path.s2v, self.sp_sense_path.fft, self.sp_sense_path.c2mag, self.sp_sense_path.stats)
44
45        self.connect(self.txpath);
46        self.connect(self.rxpath);
47
48       
49#//////main////// 
50
51global n_rcvd, n_right,hop_freq
52
53def main():
54    global n_rcvd, n_right,sync_status,mode,hop_freq
55    n_rcvd = 0
56    n_right = 0
57    def send_pkt(self, payload='', eof=False):
58        return self.txpath.send_pkt(payload, eof)
59
60    def rx_callback(ok, payload):
61
62       
63        #print "inside rx callback"
64        global n_rcvd, n_right, sync_status,mode,hop_freq
65        tb.rxpath.min_freq = hop_freq - 80e3
66        tb.rxpath.max_freq = hop_freq + 80e3
67        tb.rxpath.get_avg_power(10)
68        (pktno,) = struct.unpack('!H', payload[0:2])
69        #temp = struct.unpack('!s', payload[3:10])
70        #print "printing the elements of packet",temp,"!!!!!!!!!!!\n"
71        if mode == "sync":
72        #####################################################
73                (sync_signal,) = struct.unpack('!s', payload[2])
74                (sync_signal_red,) = struct.unpack('!s', payload[3])  #redundant sync bit
75                (data_channel,) = struct.unpack('!s', payload[4])
76                (data_channel_red,) = struct.unpack('!s', payload[5])  #redundant channel bit
77                #print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",sync_signal_red," !!!!!\n"   
78                #print "printing the channel  ",data_channel_red,"  !!!!!!!!!!!  ",data_channel_red," !!!!!\n"
79                if ok: #== True :# and str(sync_signal) == 'o' and str(sync_signal_red) == 'o':
80                        #print "inside if"
81                        if ok: #== True: #str(data_channel) == str(data_channel_red):
82                                #print "inside if1$$$$$"
83                                if str(data_channel) == str(ch):
84                                        #print "inside if2"
85                                        sync_status = True
86                                        #tb.stop()
87                                        #print "after tb stop ans sync status is ",sync_status," sync status\n"
88                                        #print "received a sync packet on channel %s\n" %(data_channel)
89                                        #data = 'o'+'o'+str(data_channel)+str(data_channel)
90                                        #print "sending this data",data,"@@@@@\n"
91                                        #pktno=0
92                                        #ack_payload = struct.pack('!H', pktno & 0xffff) + data
93                                        #print "printing the ack packet",ack_payload,"$$$$$\n"
94                                        #k=0
95                                        #while k < 10000:
96                                        #print "inside while"
97                                        #send_pkt(tb,ack_payload) #sending back the acknowledgement
98                                        #k=k+1
99        #####################################################
100       
101        n_rcvd += 1
102        if ok:
103            n_right += 1
104
105        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
106            #ok, pktno, n_rcvd, n_right)
107
108    mods = modulation_utils.type_1_mods()
109    demods = modulation_utils.type_1_demods()
110
111    #setting up the tx options parser
112    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
113    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
114    print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
115    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
116                      default='gmsk',
117                      help="Select modulation from: %s [default=%%default]"
118                            % (', '.join(mods.keys()),))
119
120    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
121                      help="set packet size [default=%default]")
122    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
123                      help="set megabytes to transmit [default=%default]")
124    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
125                      help="enable discontinous transmission (bursts of 5 packets)")
126    parser_tx.add_option("","--from-file", default=None,
127                      help="use file for packet contents")
128    print "printing parser_tx",parser_tx,"printing parser_tx"
129    transmit_path.add_options(parser_tx, expert_grp_tx)
130
131    for mod in mods.values():
132        mod.add_options(expert_grp_tx)
133
134    (options_tx, args_tx) = parser_tx.parse_args ()
135
136    if len(args_tx) != 0:
137        parser_tx.print_help()
138        sys.exit(1)
139    ############# Setting some default values for tx side of the block
140    options_tx.tx_freq = 462.5625e6
141    options_tx.samples_per_symbol =  2
142    options_tx.modulation = 'dbpsk'
143    options_tx.fusb_block_size = 4096
144    options_tx.fusb_nblocks = 16
145    options_tx.bitrate = 0.0125e6
146    #############
147    if options_tx.tx_freq is None:
148        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
149        parser_tx.print_help(sys.stderr)
150        sys.exit(1)
151
152    if options_tx.from_file is not None:
153        source_file = open(options_tx.from_file, 'r')
154    print "printing modulation type",mods[options_tx.modulation],"**"
155    # build the graph
156    print "printing tx options",options_tx,"&&&&\n"
157    #tb = my_top_block(mods[options_tx.modulation], options_tx)
158
159    #r = gr.enable_realtime_scheduling()
160    #if r != gr.RT_OK:
161    #   print "Warning: failed to enable realtime scheduling"
162
163    #setting up rx options parser
164   
165    # Create Options Parser:
166    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
167    print "printing parser_rx",parser_rx,"!!!!!\n"
168    print "after option parser"
169    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
170    print "printing expert group rx",expert_grp_rx,"@@@@\n"
171    parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
172                      default='gmsk',
173                      help="Select modulation from: %s [default=%%default]"
174                            % (', '.join(demods.keys()),))
175    print "inside rx option parser"
176    receive_path.add_options(parser_rx, expert_grp_rx)
177
178    for mod in demods.values():
179        mod.add_options(expert_grp_rx)
180
181    (options_rx, args_rx) = parser_rx.parse_args ()
182
183    if len(args_rx) != 0:
184        parser_rx.print_help(sys.stderr)
185        sys.exit(1)
186    ############# Setting some default values for rx side of the block
187    options_rx.rx_freq = 462.5625e6 #setting default rx_freq value
188    options_rx.samples_per_symbol =  2
189    options_rx.modulation = 'dbpsk'
190    options_rx.fusb_block_size = 4096
191    options_rx.fusb_nblocks = 16
192    options_rx.bitrate = 0.0125e6
193    #############
194    if options_rx.rx_freq is None:
195        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
196        parser_rx.print_help(sys.stderr)
197        sys.exit(1)
198    print "printing rx options",options_rx,"&&&&\n"
199
200
201
202    ######################## adding another top block for usrp spectrum sense ########################
203    #This is not needed anymore
204    #trials = 10
205   
206    #tb_spec_sense = spec_sense_top_block(default_min_freq,default_min_freq)
207    #tb_spec_sense.subdev.select_rx_antenna('RX2')
208    #tb_spec_sense.start()              # start executing flow graph in another thread...
209    #avg = tb_spec_sense.get_avg_power(trials)
210    #print "printing the average power ",avg, "\n"
211    #tb_spec_sense.stop()   
212   
213
214    ##################################################################################################
215
216    # build the graph
217
218    tb = my_top_block(mods[options_tx.modulation],
219                     demods[options_rx.modulation],
220                     rx_callback,options_tx,
221                     options_rx)
222    r = gr.enable_realtime_scheduling()
223    if r != gr.RT_OK:
224       print "Warning: failed to enable realtime scheduling"
225
226    tb.start()
227    #tb.rxpath.get_avg_power(10) #10 is the number of fft frames that we are looking at to determine the average power
228    # generate and send packets
229    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
230   
231    #n = 0
232    #pktno = 0
233    #pkt_size = int(options_tx.size)
234    #options_tx.megabytes = 0.000048
235   
236    #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)
237   
238   
239    myfile = file("logfile.txt", 'w')
240    myfile.write("")
241    #sending sync packet-format
242    global ch
243    check =True
244    #ch = random.randint(1, 7)#which channel to send information on...this is the second data packet of the synchronization frame
245    #resend = 5; #number of times a batch of sync packets is sent on a channel before trying a different channel
246    running = True
247    #resend_count= 0 #initializing resend count
248    #while running
249    if mode  == "sync":
250        #nbytes = int(1e6 * .0003)
251        #nbytes = int(1e6 * .0003)
252        nbytes = 6
253        pkt_size = 6
254        print "printing packet size",pkt_size,"pkt size\n"
255        while sync_status != True:
256                #ch = random.randint(1, 7)
257                #ch = int(random.choice('17'))
258                #ch = random.randint(6,12)
259                ch = 1
260                hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
261                print "hop freq is ",hop_freq
262               
263                #hop_freq = float(1e6 * (462.5625+(5-1)*0.05))#setting the centre freq frequency for sending packets
264                tb.txpath.set_freq(hop_freq)
265                tb.rxpath.set_freq(hop_freq)
266                #print "inside while pppp printing sync channel",sync_status,"  done"
267                #if sync_status == "True":
268                #print "synchronization done..data channel is found"
269                #break
270                #else:
271                #print "inside else1"
272                n = 0
273                pktno = 0
274                print >> myfile, "out"
275                while n < nbytes:
276                        print >> myfile, "inside while"
277                        if options_tx.from_file is None:
278                                #data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255
279                                data = 's'+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
280                                #print >> myfile, data
281                                #print "printing data",data,"****\n"
282                        else:
283                                data = source_file.read(pkt_size - 2)
284                                if data == '':
285                                        break;
286
287                        payload = struct.pack('!H', pktno & 0xffff) + data
288                        #print "printing payload",payload,"**\n"
289                       
290                        send_pkt(tb,payload)
291                        #print "printing payload",payload,"**\n"
292                        n += len(payload)
293                        sys.stderr.write('.')
294                        if options_tx.discontinuous and pktno % 5 == 4:
295                                time.sleep(1)
296                        pktno += 1
297                        #print "before sleeping for 10 seconds and value of resend count is",resend_count
298
299                tb.rxpath.min_freq = hop_freq - 80e3
300                tb.rxpath.max_freq = hop_freq + 80e3
301                tb.rxpath.get_avg_power(10) #10 is the number of fft frames that we are looking at to determine the average power
302
303                time.sleep(0.05)
304                #time.sleep(2)
305                #resend_count += 1
306    print "sync channel found! and it is channel ",ch,"\n"
307    send_pkt(tb,eof=True)
308    #myfile.close()
309    tb.wait()
310   
311
312
313if __name__ == '__main__':
314    try:
315        main()
316    except KeyboardInterrupt:
317        pass
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360   
361
362
363
Note: See TracBrowser for help on using the browser.