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

Revision 369, 9.8 KB (checked in by sriram, 15 years ago)

Working dsa code for node2

  • 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                if sync_status != "True":
187                        #print "inside while"
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                       
195                        tb.txpath.set_freq(hop_freq)
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
201                        time.sleep(0.05)
202                else:
203                        print "sync channel is found...channel ",ch,"\n"
204                        check_sync = 0
205                        mode = "traffic"
206                        traffic_flag = False
207                        sync_status="False"
208                        continue
209   
210    ################################################end of sync mode####################################
211
212    ################################################Communications mode####################################     
213        if mode == "traffic":
214                print "Inside traffic mode"
215                if check_sync == 0:
216                        time.sleep(0.8)
217                        if traffic_flag != True:
218                                mode = "sync"
219                                continue
220                        else:
221                                check_sync = 1 
222                #nbytes = 15
223                #pkt_size = 15
224                #sync_pktno = 0
225                #n =0
226                #while n < nbytes:
227                        #print >> myfile, "inside while"
228                #       if options_tx.from_file is None:
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"
233                #       else:
234                #               data = source_file.read(pkt_size - 2)
235                #               if data == '':
236                #                       break;
237       
238                #       
239                #       payload = struct.pack('!15s', data)
240                        #print "printing payload",payload,"**\n"
241                               
242                #       send_pkt(tb,payload)
243                        #print "printing payload",payload,"**\n"
244                #       n += len(payload)
245                #       sys.stderr.write('.')
246                #       if options_tx.discontinuous and data_pktno % 5 == 4:
247                #               time.sleep(1)
248                #       data_pktno += 1
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
253                        mode = "sync"
254                        continue
255       
256    ################################################ end of Communications mode####################################     
257
258
259
260    #tb.wait()
261   
262if __name__ == '__main__':
263    try:
264        main()
265    except KeyboardInterrupt:
266        pass
267
268
269
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
Note: See TracBrowser for help on using the browser.