root/vtcross/branches/sriram/benchmark_txrxnode2.py @ 373

Revision 373, 9.9 KB (checked in by sriram, 15 years ago)

Changing the threshold

  • 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
6from gnuradio.eng_option import eng_option
7from optparse import OptionParser
8from numpy import random
9import random, time, struct, sys, math
10
11# from current dir
12from transmit_path import transmit_path
13from receive_path import receive_path
14global sync_status,mode,traffic_flag,ch
15sync_status="False"
16mode = "sync" #default mode
17traffic_flag = False
18class my_top_block(gr.top_block):
19
20    def __init__(self, mod_class, demod_class,
21                 rx_callback, options_tx,options_rx):
22
23        gr.top_block.__init__(self)
24        self.rxpath = receive_path(demod_class, rx_callback, options_rx)
25        self.txpath = transmit_path(mod_class, options_tx)
26        self.connect(self.txpath);
27        self.connect(self.rxpath);
28   
29#//////main//////
30
31global n_rcvd, n_right
32
33def main():
34    global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
35    n_rcvd = 0
36    n_right = 0
37    def send_pkt(self, payload='', eof=False):
38        return self.txpath.send_pkt(payload, eof)
39
40    def rx_callback(ok, payload):
41        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
42        ########################## sync ###########################
43        if mode == "sync":
44                if ok == True:
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        #####################################################
58       
59        ######################### traffic ############################
60        if mode == "traffic":
61                if ok:
62                        (data_header,) = struct.unpack('!s', payload[0])
63                        if data_header == 'd':
64                                traffic_flag = True
65                                comm = struct.unpack('!14s', payload[1:15])
66                                print("received this ", comm)
67                                data = 'dI am fine.....' #Sending this message
68                                payload = struct.pack('!15s', data)
69                                send_pkt(tb,payload)
70        ##############################################################
71
72        n_rcvd += 1
73        if ok:
74            n_right += 1
75
76        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
77        #    ok, pktno, n_rcvd, n_right)
78        #print "sync_status is ",sync_status," @@\n"
79
80    mods = modulation_utils.type_1_mods()
81    #print "printing mods",mods,"$$$$\n"
82    demods = modulation_utils.type_1_demods()
83
84    #setting up the tx options parser
85    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
86    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
87    #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
88    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
89                      default='gmsk',
90                      help="Select modulation from: %s [default=%%default]"
91                            % (', '.join(mods.keys()),))
92
93    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
94                      help="set packet size [default=%default]")
95    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
96                      help="set megabytes to transmit [default=%default]")
97    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
98                      help="enable discontinous transmission (bursts of 5 packets)")
99    parser_tx.add_option("","--from-file", default=None,
100                      help="use file for packet contents")
101    print "printing parser_tx",parser_tx,"printing parser_tx"
102    transmit_path.add_options(parser_tx, expert_grp_tx)
103
104    for mod in mods.values():
105        mod.add_options(expert_grp_tx)
106
107    (options_tx, args_tx) = parser_tx.parse_args ()
108
109    if len(args_tx) != 0:
110        parser_tx.print_help()
111        sys.exit(1)
112    ############# Setting some default values for tx side of the block
113    options_tx.tx_freq = 470e6#462.5625e6
114    options_tx.samples_per_symbol =  2
115    options_tx.modulation = 'dbpsk'
116    options_tx.fusb_block_size = 4096
117    options_tx.fusb_nblocks = 16
118    options_tx.bitrate = 0.0125e6
119    #############
120
121    if options_tx.tx_freq is None:
122        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
123        parser_tx.print_help(sys.stderr)
124        sys.exit(1)
125
126    if options_tx.from_file is not None:
127        source_file = open(options_tx.from_file, 'r')
128    print "printing modulation type",mods[options_tx.modulation],"**"
129    # build the graph
130    print "printing tx options",options_tx,"&&&&\n"
131   
132    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
133    print "printing parser_rx",parser_rx,"!!!!!\n"
134    print "after option parser"
135    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
136    print "printing expert group rx",expert_grp_rx,"@@@@\n"
137    parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
138                      default='gmsk',
139                      help="Select modulation from: %s [default=%%default]"
140                            % (', '.join(demods.keys()),))
141    print "inside rx option parser"
142    receive_path.add_options(parser_rx, expert_grp_rx)
143
144    for mod in demods.values():
145        mod.add_options(expert_grp_rx)
146
147    (options_rx, args_rx) = parser_rx.parse_args ()
148
149    if len(args_rx) != 0:
150        parser_rx.print_help(sys.stderr)
151        sys.exit(1)
152    ############# Setting some default values for rx side of the block
153    options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value
154    options_rx.samples_per_symbol =  2
155    options_rx.modulation = 'dbpsk'
156    options_rx.fusb_block_size = 4096
157    options_rx.fusb_nblocks = 16
158    options_rx.bitrate = 0.0125e6
159    #############
160
161    if options_rx.rx_freq is None:
162        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
163        parser_rx.print_help(sys.stderr)
164        sys.exit(1)
165    print "printing rx options",options_rx,"&&&&\n"
166
167# build the graph
168
169    tb = my_top_block(mods[options_tx.modulation],
170                      demods[options_rx.modulation],
171                      rx_callback,options_tx,
172                      options_rx)
173    r = gr.enable_realtime_scheduling()
174    if r != gr.RT_OK:
175        print "Warning: failed to enable realtime scheduling"
176   
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)
180    #listening to random frequencies untill a match is found
181    running = True
182    global ch
183    while running:
184        ################################################sync mode####################################
185        if mode == "sync":
186                #sync_status="False"
187                if sync_status != "True":
188                        #print "inside while"
189                        ch = int(random.choice([1,7,8,14]))
190                        if ch < 8:
191                                hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
192                        else:
193                                hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets
194                        #print "hop freq is",hop_freq,"@@@@\n"
195                       
196                        tb.txpath.set_freq(hop_freq)
197                        tb.rxpath.set_freq(hop_freq)
198                        ch_energy = tb.rxpath.probe.level() #check if primary user is present
199                        #if int(ch_energy) > 1.0e7:
200                        print "hop freq is ",hop_freq," and channel energy is ",ch_energy,"\n"
201                        if int(ch_energy) > 2e7: #if primary user is there then dont transmit on this channel
202                                continue
203                        time.sleep(0.1)
204                else:
205                        print "sync channel is found...channel ",ch,"\n"
206                        check_sync = 0
207                        mode = "traffic"
208                        traffic_flag = False
209                        sync_status="False"
210                        continue
211   
212    ################################################end of sync mode####################################
213
214    ################################################Communications mode####################################     
215        if mode == "traffic":
216                print "Inside traffic mode"
217                if check_sync == 0:
218                        time.sleep(0.8)
219                        if traffic_flag != True:
220                                mode = "sync"
221                                continue
222                        else:
223                                check_sync = 1 
224                #nbytes = 15
225                #pkt_size = 15
226                #sync_pktno = 0
227                #n =0
228                #while n < nbytes:
229                        #print >> myfile, "inside while"
230                #       if options_tx.from_file is None:
231                                #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
232                #               data = 'dI am fine.....' #Sending this message
233                                #print >> myfile, data
234                                #print "printing data",data,"****\n"
235                #       else:
236                #               data = source_file.read(pkt_size - 2)
237                #               if data == '':
238                #                       break;
239       
240                #       
241                #       payload = struct.pack('!15s', data)
242                        #print "printing payload",payload,"**\n"
243                               
244                #       send_pkt(tb,payload)
245                        #print "printing payload",payload,"**\n"
246                #       n += len(payload)
247                #       sys.stderr.write('.')
248                #       if options_tx.discontinuous and data_pktno % 5 == 4:
249                #               time.sleep(1)
250                #       data_pktno += 1
251                #       #print "before sleeping for 10 seconds and value of resend count is",resend_count
252                time.sleep(0.2)
253                ch_energy = tb.rxpath.probe.level() #check if primary user is present
254                print "channel energy is ",ch_energy,"\n"
255                if int(ch_energy) > 2e7: #if primary user is there then dont transmit on this channel
256                        mode = "sync"
257                        continue
258       
259    ################################################ end of Communications mode####################################     
260
261
262
263    #tb.wait()
264   
265if __name__ == '__main__':
266    try:
267        main()
268    except KeyboardInterrupt:
269        pass
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297   
298
299
300
301
Note: See TracBrowser for help on using the browser.