root/vtcross/branches/sriram/dsa.py @ 378

Revision 378, 9.2 KB (checked in by sriram, 15 years ago)

Same code for both the nodes

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,ch,traffic_flag
15sync_status = False
16mode = "sync"#default mode is sync
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
29global n_rcvd, n_right
30
31def main():
32        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
33        n_rcvd = 0
34        n_right = 0
35        def send_pkt(self, payload='', eof=False):
36                return self.txpath.send_pkt(payload, eof)
37
38
39        def rx_callback(ok, payload):
40                global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
41                ##############################################################
42                if mode == "sync":
43                        if ok:
44                                (pktno,) = struct.unpack('!H', payload[0:2])
45                                (sync_signal,) = struct.unpack('!s', payload[2])
46                                (data_channel,) = struct.unpack('!H', payload[3:5])
47                                print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",data_channel," !!!!!\n" 
48                                 
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                                if str(sync_signal) == 's' and str(data_channel) == str(ch):
56                                        sync_status = True
57                                        print "received a sync packet on channel %s\n" %(data_channel)
58                                        data = 'o'
59                                        pktno=0
60                                        ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
61                                        send_pkt(tb,ack_payload) #sending back the acknowledgement
62                ##############################################################
63               
64                ######################### traffic ############################
65                if mode == "traffic":
66                        if ok:
67                                (data_header,) = struct.unpack('!s', payload[0])
68                                if data_header == 'd':
69                                        traffic_flag = True
70                                        comm = struct.unpack('!14s', payload[1:15])
71                                        print("received this ", comm)
72                                        data = 'dI am fine.....' #Sending this message
73                                        payload = struct.pack('!15s', data)
74                                        send_pkt(tb,payload)
75                ##############################################################
76
77                n_rcvd += 1
78                if ok:
79                n_right += 1
80
81                #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
82                #    ok, pktno, n_rcvd, n_right)
83                #print "sync_status is ",sync_status," @@\n"
84
85        mods = modulation_utils.type_1_mods()
86        #print "printing mods",mods,"$$$$\n"
87        demods = modulation_utils.type_1_demods()
88
89        #setting up the tx options parser
90        parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
91        expert_grp_tx = parser_tx.add_option_group("Expert_tx")
92        #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
93        parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
94                        default='gmsk',
95                        help="Select modulation from: %s [default=%%default]"
96                                % (', '.join(mods.keys()),))
97
98        parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
99                        help="set packet size [default=%default]")
100        parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
101                        help="set megabytes to transmit [default=%default]")
102        parser_tx.add_option("","--discontinuous", action="store_true", default=False,
103                        help="enable discontinous transmission (bursts of 5 packets)")
104        parser_tx.add_option("","--from-file", default=None,
105                        help="use file for packet contents")
106        print "printing parser_tx",parser_tx,"printing parser_tx"
107        transmit_path.add_options(parser_tx, expert_grp_tx)
108
109        for mod in mods.values():
110                mod.add_options(expert_grp_tx)
111
112        (options_tx, args_tx) = parser_tx.parse_args ()
113
114        if len(args_tx) != 0:
115                parser_tx.print_help()
116                sys.exit(1)
117       
118        ############# Setting some default values for tx side of the block
119        options_tx.tx_freq = 470e6#462.5625e6
120        options_tx.samples_per_symbol =  2
121        options_tx.modulation = 'dbpsk'
122        options_tx.fusb_block_size = 4096
123        options_tx.fusb_nblocks = 16
124        options_tx.bitrate = 0.0125e6
125        #############
126
127        if options_tx.tx_freq is None:
128                sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
129                parser_tx.print_help(sys.stderr)
130                sys.exit(1)
131
132        if options_tx.from_file is not None:
133                source_file = open(options_tx.from_file, 'r')
134        print "printing modulation type",mods[options_tx.modulation],"**"
135        # build the graph
136        print "printing tx options",options_tx,"&&&&\n"
137   
138        parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
139        print "printing parser_rx",parser_rx,"!!!!!\n"
140        print "after option parser"
141        expert_grp_rx = parser_rx.add_option_group("Expert_rx")
142        print "printing expert group rx",expert_grp_rx,"@@@@\n"
143        parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
144                        default='gmsk',
145                        help="Select modulation from: %s [default=%%default]"
146                                % (', '.join(demods.keys()),))
147        print "inside rx option parser"
148        receive_path.add_options(parser_rx, expert_grp_rx)
149
150        for mod in demods.values():
151                mod.add_options(expert_grp_rx)
152
153        (options_rx, args_rx) = parser_rx.parse_args ()
154
155        if len(args_rx) != 0:
156                parser_rx.print_help(sys.stderr)
157                sys.exit(1)
158        ############# Setting some default values for rx side of the block
159        options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value
160        options_rx.samples_per_symbol =  2
161        options_rx.modulation = 'dbpsk'
162        options_rx.fusb_block_size = 4096
163        options_rx.fusb_nblocks = 16
164        options_rx.bitrate = 0.0125e6
165        #############
166
167        if options_rx.rx_freq is None:
168                sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
169                parser_rx.print_help(sys.stderr)
170                sys.exit(1)
171        print "printing rx options",options_rx,"&&&&\n"
172
173        # build the graph
174
175        tb = my_top_block(mods[options_tx.modulation],
176                        demods[options_rx.modulation],
177                        rx_callback,options_tx,
178                        options_rx)
179        r = gr.enable_realtime_scheduling()
180        if r != gr.RT_OK:
181                print "Warning: failed to enable realtime scheduling"
182   
183        tb.start()
184        #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats)
185        #tb.rxpath.connect(tb.rxpath.u, tb.rxpath.s2v)
186        #listening to random frequencies untill a match is found
187        running = True
188        global ch
189
190        ################################################sync mode####################################
191        if mode == "sync":
192                if sync_status != True:
193                        ch = int(random.choice([1,7,8,14]))
194                        if ch < 8:
195                                hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
196                        else:
197                                hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets
198                       
199           
200                        tb.txpath.set_freq(hop_freq)
201                        tb.rxpath.set_freq(hop_freq)
202                       
203                        k = 0
204                        total_energy = 0
205                        while k <  1:
206                                ch_energy = tb.rxpath.probe.level() #check if primary user is present
207                                total_energy = total_energy + ch_energy
208                                k+=1
209                       
210                        avg_energy = total_energy/1
211                        print "printing hop frequency ",hop_freq,"and energy is ",avg_energy,"\n"
212                        if int(avg_energy) > 2e7: #if primary user is there then dont transmit on this channel
213                                #while
214                                continue
215                                #print "present"
216                       
217                        nbytes = 5 #int(1e6 * .0003)
218                        pkt_size = 5
219                        n = 0
220                        pktno = 0
221                        while n < nbytes:
222                                if options_tx.from_file is None:
223                                        #data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255
224                                        data = 's'#+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
225                                else:
226                                        data = source_file.read(pkt_size - 2)
227                                        if data == '':
228                                                break;
229
230                                payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
231                                #print "printing payload",payload,"**\n"
232       
233                                send_pkt(tb,payload)
234                                #print "sending this data ",data,"\n"
235                                n += len(payload)
236                                sys.stderr.write('.')
237                                if options_tx.discontinuous and pktno % 5 == 4:
238                                        time.sleep(1)
239                                        pktno += 1
240                        time.sleep(0.1)
241                #resend_count += 1
242                else:
243                        print "sync channel found..channel ",ch,"\n" 
244                        mode = "traffic"
245                        sync_status = False
246        ################################################end of sync mode####################################
247
248
249
250
251
252
253
Note: See TracBrowser for help on using the browser.