1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.network; |
---|
7 | |
---|
8 | |
---|
9 | import java.io.Serializable; |
---|
10 | import java.util.List; |
---|
11 | |
---|
12 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
13 | import vnsim.core.Communicator; |
---|
14 | import vnsim.core.events.*; |
---|
15 | import vnsim.map.object.*; |
---|
16 | import vnsim.map.utils.*; |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | public class RadioDev implements Serializable{ |
---|
23 | |
---|
24 | /** <code>serialVersionUID</code> */ |
---|
25 | private static final long serialVersionUID = -5883719321862303634L; |
---|
26 | |
---|
27 | public static final long LIGHT_SPEED = 299792458; // m/s |
---|
28 | |
---|
29 | public static final long FREQUENCY = 2400000000L; //Hz |
---|
30 | |
---|
31 | public static final double LAMBDA = (double)LIGHT_SPEED/FREQUENCY; |
---|
32 | |
---|
33 | private Communicator communicator; |
---|
34 | |
---|
35 | // height of the antenna in m |
---|
36 | private double antennaHeight; |
---|
37 | |
---|
38 | // radio device sensitivity in DBm (decibel referenced to 1 mW power) |
---|
39 | public double sensitivity; |
---|
40 | |
---|
41 | public double rxThreshold; |
---|
42 | |
---|
43 | public double rxThreshold_W; |
---|
44 | |
---|
45 | public double snrThreshold; |
---|
46 | |
---|
47 | // transmission power in DBm |
---|
48 | public double txPower; |
---|
49 | |
---|
50 | // antenna gain in DBm |
---|
51 | public double gain; |
---|
52 | |
---|
53 | public int lastTxTime = 0; |
---|
54 | |
---|
55 | public RadioDev(Communicator c){ |
---|
56 | communicator = c; |
---|
57 | |
---|
58 | antennaHeight = 1.5; // m |
---|
59 | |
---|
60 | sensitivity = -91.0; //DBm |
---|
61 | |
---|
62 | snrThreshold = 9.1; |
---|
63 | |
---|
64 | rxThreshold = -81.0; // DBm |
---|
65 | rxThreshold_W = convertDBmToW(rxThreshold); |
---|
66 | |
---|
67 | txPower = 15; //DBm |
---|
68 | //30 + 10 * Math.log10(0.1); |
---|
69 | |
---|
70 | gain = 0; // DBm |
---|
71 | } |
---|
72 | |
---|
73 | public ReceiveEvent receive(List<ReceiveEvent> receivedSignals){ |
---|
74 | |
---|
75 | if (Globals.engine.crtTime == communicator.getRadio().lastTxTime){ |
---|
76 | // ignore all signals, receiver radio in transmit mode |
---|
77 | return null; |
---|
78 | } |
---|
79 | |
---|
80 | int capturableSignals = 0; |
---|
81 | ReceiveEvent max = null; |
---|
82 | double maxrxpower = -1000; |
---|
83 | double maxrxpower_W = 0; |
---|
84 | double totalPower = 0; |
---|
85 | double totalPower_W = 0; |
---|
86 | |
---|
87 | for (ReceiveEvent re : receivedSignals){ |
---|
88 | RadioDev txradio = re.sender.getRadio(); |
---|
89 | |
---|
90 | Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); |
---|
91 | Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); |
---|
92 | Road r2 = (Road) Globals.map.roads.get(communicator.getRoadIdx()); |
---|
93 | Point p2 = (Point) r2.points.get(communicator.getPointIdx()); |
---|
94 | double distance = GPSutil.distance(p1, p2); |
---|
95 | |
---|
96 | double pl = pathloss(re.sender, communicator, distance); |
---|
97 | double rxpower = txradio.getTxPower() - pl; |
---|
98 | double rxpower_W = convertDBmToW(rxpower); |
---|
99 | |
---|
100 | totalPower += rxpower; |
---|
101 | totalPower_W += rxpower_W; |
---|
102 | |
---|
103 | if (rxpower > maxrxpower){ |
---|
104 | maxrxpower = rxpower; |
---|
105 | maxrxpower_W = rxpower_W; |
---|
106 | max = re; |
---|
107 | } |
---|
108 | |
---|
109 | // if (distance > 0.7) |
---|
110 | // System.out.println("DIST="+distance+"; RXPOWER="+rxpower+"; THR="+rxThreshold); |
---|
111 | |
---|
112 | if (rxpower >= this.rxThreshold){ |
---|
113 | capturableSignals ++; |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | if (distance > Globals.maxRange ) |
---|
118 | Globals.maxRange = distance; |
---|
119 | |
---|
120 | } |
---|
121 | } |
---|
122 | Globals.packets += capturableSignals; |
---|
123 | double noise_W = totalPower_W - convertDBmToW(maxrxpower); |
---|
124 | if (capturableSignals > 1){ |
---|
125 | int i = 0; |
---|
126 | i++; |
---|
127 | } |
---|
128 | if (maxrxpower_W >= this.rxThreshold_W && |
---|
129 | maxrxpower_W >= noise_W * this.snrThreshold){ |
---|
130 | Globals.lost += capturableSignals - 1; |
---|
131 | return max; |
---|
132 | } |
---|
133 | |
---|
134 | if (capturableSignals == 1){ |
---|
135 | Globals.interfered ++ ; |
---|
136 | } |
---|
137 | |
---|
138 | if (capturableSignals > 0){ |
---|
139 | // collision |
---|
140 | Globals.collided += capturableSignals; |
---|
141 | return null; |
---|
142 | } |
---|
143 | |
---|
144 | // only weak signals |
---|
145 | return null; |
---|
146 | } |
---|
147 | |
---|
148 | // Two ray pathloss |
---|
149 | public static double pathloss(Communicator s, Communicator d, double distance){ |
---|
150 | RadioDev tx = s.getRadio(); |
---|
151 | RadioDev rx = d.getRadio(); |
---|
152 | |
---|
153 | distance *= 1000; |
---|
154 | if (distance < 1) |
---|
155 | return 0; |
---|
156 | |
---|
157 | double pathloss = -(tx.getGain() + rx.getGain()); |
---|
158 | double freeSpace = (4.0 * Math.PI * distance) / LAMBDA; |
---|
159 | double planeEarth = (distance * distance) / (tx.antennaHeight * rx.antennaHeight); |
---|
160 | |
---|
161 | if (planeEarth > freeSpace){ |
---|
162 | pathloss += 20.0 * Math.log10(planeEarth); |
---|
163 | }else{ |
---|
164 | pathloss += 20.0 * Math.log10(freeSpace); |
---|
165 | } |
---|
166 | |
---|
167 | return pathloss; |
---|
168 | } |
---|
169 | /** |
---|
170 | * |
---|
171 | * @param dbm power value in DBm |
---|
172 | * @return power in mW |
---|
173 | */ |
---|
174 | public static double convertDBmToW(double dbm){ |
---|
175 | return Math.pow(10, (dbm - 30) / 10); |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * |
---|
180 | * @param w power in watts |
---|
181 | * @return value in DBm |
---|
182 | */ |
---|
183 | public static double convertWToDBm(double w){ |
---|
184 | return 30 + Math.log10(w); |
---|
185 | } |
---|
186 | |
---|
187 | public double getGain() { |
---|
188 | return gain; |
---|
189 | } |
---|
190 | |
---|
191 | public void setGain(double gain) { |
---|
192 | this.gain = gain; |
---|
193 | } |
---|
194 | |
---|
195 | public double getSensitivity() { |
---|
196 | return sensitivity; |
---|
197 | } |
---|
198 | |
---|
199 | public void setSensitivity(double sensitivity) { |
---|
200 | this.sensitivity = sensitivity; |
---|
201 | } |
---|
202 | |
---|
203 | public double getTxPower() { |
---|
204 | return txPower; |
---|
205 | } |
---|
206 | |
---|
207 | public void setTxPower(double txPower) { |
---|
208 | this.txPower = txPower; |
---|
209 | } |
---|
210 | |
---|
211 | public void setRadioTxMode(){ |
---|
212 | lastTxTime = Globals.engine.crtTime; |
---|
213 | } |
---|
214 | } |
---|