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 | //Petroaca - the Application layer of DSRC |
---|
7 | package vnsim.network.dsrc; |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | import java.io.ByteArrayInputStream; |
---|
12 | import java.util.ArrayList; |
---|
13 | |
---|
14 | import vnsim.core.CarInfo; |
---|
15 | import vnsim.core.Communicator; |
---|
16 | import vnsim.map.object.Globals; |
---|
17 | import vnsim.map.object.Point; |
---|
18 | import vnsim.map.object.Road; |
---|
19 | import vnsim.map.utils.GPSutil; |
---|
20 | |
---|
21 | |
---|
22 | public class Application |
---|
23 | { |
---|
24 | //the vehicles around the host vehicle used in forward collision warning and lane change assistance |
---|
25 | private CarInfo FVehicle,NFVehicle,LAdjancedVehicle,RAdjancedVehicle; |
---|
26 | |
---|
27 | private double FVehicleDistance,NFVehicleDistance,LAVehicleDistance,RAVehicleDistance; |
---|
28 | |
---|
29 | private boolean forwardCrashSignal,leftCrashSignal,rightCrashSignal,emergencyVehicleSignal; |
---|
30 | |
---|
31 | private double lastFVSpeed,lastNFVSpeed; |
---|
32 | |
---|
33 | private int emergencyVehicleHpoz,emergencyVehicleVpoz; |
---|
34 | |
---|
35 | private int currentTime; |
---|
36 | |
---|
37 | private long lastIRTFV,lastIRTNFV,lastIRTLAV,lastIRTRAV; |
---|
38 | |
---|
39 | public Application() |
---|
40 | { |
---|
41 | this.FVehicle=this.NFVehicle=this.LAdjancedVehicle=this.RAdjancedVehicle=null; |
---|
42 | |
---|
43 | this.FVehicleDistance=this.NFVehicleDistance=this.LAVehicleDistance=this.RAVehicleDistance=30000; |
---|
44 | |
---|
45 | |
---|
46 | this.forwardCrashSignal=false; |
---|
47 | this.leftCrashSignal=this.rightCrashSignal=false; |
---|
48 | this.emergencyVehicleSignal=false; |
---|
49 | |
---|
50 | this.currentTime=0; |
---|
51 | |
---|
52 | this.lastFVSpeed=-1; |
---|
53 | this.lastNFVSpeed=-1; |
---|
54 | |
---|
55 | this.lastIRTFV=this.lastIRTNFV=this.lastIRTLAV=this.lastIRTRAV=0; |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | //it refreshes the FV , NFV and it calculates the probability of a crash with these vehicles |
---|
60 | public void ForwardCollisionWarning(byte[] bytesReceived,CarInfo self) |
---|
61 | { |
---|
62 | |
---|
63 | switch(bytesReceived[0]) |
---|
64 | { |
---|
65 | case Globals.PROT_NEIGHBOR_DISCOVERY: |
---|
66 | |
---|
67 | CarInfo sc=new CarInfo(); |
---|
68 | sc.wrap(bytesReceived,2); |
---|
69 | |
---|
70 | calculateFCWVehicles(self,sc); |
---|
71 | calculateLCAVehicles(self,sc); |
---|
72 | |
---|
73 | |
---|
74 | break; |
---|
75 | |
---|
76 | case Globals.PROT_SETOFCARS: |
---|
77 | ByteArrayInputStream byteStream = new ByteArrayInputStream(bytesReceived); |
---|
78 | byte[] b = new byte[Globals.NONAGG_RECORD_SIZE]; |
---|
79 | byteStream.read(); |
---|
80 | int numberOfRecords = (byte)byteStream.read(); |
---|
81 | int messageRoadDirection = byteStream.read(); |
---|
82 | |
---|
83 | int recordsRead=0; |
---|
84 | |
---|
85 | while(recordsRead < numberOfRecords) |
---|
86 | { |
---|
87 | int n = byteStream.read(b, 0, Globals.NONAGG_RECORD_SIZE); |
---|
88 | |
---|
89 | if (n == -1 || n < Globals.NONAGG_RECORD_SIZE){ |
---|
90 | break; |
---|
91 | } |
---|
92 | recordsRead++; |
---|
93 | |
---|
94 | CarInfo sc2=new CarInfo(); |
---|
95 | sc2.wrap(b); |
---|
96 | |
---|
97 | calculateFCWVehicles(self,sc2); |
---|
98 | calculateLCAVehicles(self,sc2); |
---|
99 | } |
---|
100 | break; |
---|
101 | |
---|
102 | default:break; |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | // check FV and NFV speeds and calculate probability of crash |
---|
107 | if(this.FVehicle!=null) |
---|
108 | { |
---|
109 | if(this.lastFVSpeed!=-1) |
---|
110 | { |
---|
111 | if(self.getSpeed()-this.FVehicle.getSpeed()>=Globals.SPEED_DIFFERENCE || this.lastFVSpeed-this.FVehicle.getSpeed()>=Globals.SPEED_DIFFERENCE) |
---|
112 | this.forwardCrashSignal=true; |
---|
113 | } |
---|
114 | |
---|
115 | this.lastFVSpeed=this.FVehicle.getSpeed(); |
---|
116 | } |
---|
117 | |
---|
118 | if(this.NFVehicle!=null) |
---|
119 | { |
---|
120 | if(this.lastNFVSpeed!=-1) |
---|
121 | { |
---|
122 | if(self.getSpeed()-this.NFVehicle.getSpeed()>=Globals.SPEED_DIFFERENCE || this.lastNFVSpeed-this.NFVehicle.getSpeed()>=Globals.SPEED_DIFFERENCE) |
---|
123 | this.forwardCrashSignal=true; |
---|
124 | } |
---|
125 | |
---|
126 | this.lastNFVSpeed=this.NFVehicle.getSpeed(); |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | if(this.currentTime%40==0 && self.getVehicleId()==100) |
---|
131 | { |
---|
132 | System.out.println("Time "+this.currentTime/Globals.MINUTE+":"+(this.currentTime%Globals.MINUTE)/Globals.SECOND+" Car "+self.getVehicleId()+" has FV:"+((this.FVehicle==null)?"none":this.FVehicle.getVehicleId())+" NFV:"+((this.NFVehicle==null)?"none":this.NFVehicle.getVehicleId())+ |
---|
133 | " LAV:"+((this.LAdjancedVehicle==null)?"none":this.LAdjancedVehicle.getVehicleId())+" RAV:"+((this.RAdjancedVehicle==null)?"none":this.RAdjancedVehicle.getVehicleId())); |
---|
134 | } |
---|
135 | */ |
---|
136 | } |
---|
137 | |
---|
138 | public void LaneChangeAssistance(byte[] bytesReceived,CarInfo self) |
---|
139 | { |
---|
140 | //calculate the LAV and RAV and calculate the crash probability |
---|
141 | |
---|
142 | switch(bytesReceived[0]) |
---|
143 | { |
---|
144 | case Globals.PROT_NEIGHBOR_DISCOVERY: |
---|
145 | CarInfo sc=new CarInfo(); |
---|
146 | sc.wrap(bytesReceived,2); |
---|
147 | |
---|
148 | calculateLCAVehicles(self,sc); |
---|
149 | |
---|
150 | break; |
---|
151 | |
---|
152 | case Globals.PROT_SETOFCARS: |
---|
153 | |
---|
154 | ByteArrayInputStream byteStream = new ByteArrayInputStream(bytesReceived); |
---|
155 | byte[] b = new byte[Globals.NONAGG_RECORD_SIZE]; |
---|
156 | byteStream.read(); |
---|
157 | int numberOfRecords = (byte)byteStream.read(); |
---|
158 | int messageRoadDirection = byteStream.read(); |
---|
159 | |
---|
160 | |
---|
161 | int recordsRead=0; |
---|
162 | |
---|
163 | while(recordsRead < numberOfRecords) |
---|
164 | { |
---|
165 | int n = byteStream.read(b, 0, Globals.NONAGG_RECORD_SIZE); |
---|
166 | |
---|
167 | if (n == -1 || n < Globals.NONAGG_RECORD_SIZE){ |
---|
168 | break; |
---|
169 | } |
---|
170 | recordsRead++; |
---|
171 | |
---|
172 | CarInfo sc2=new CarInfo(); |
---|
173 | sc2.wrap(b); |
---|
174 | |
---|
175 | calculateLCAVehicles(self,sc2); |
---|
176 | } |
---|
177 | break; |
---|
178 | |
---|
179 | default:break; |
---|
180 | } |
---|
181 | |
---|
182 | if(this.LAdjancedVehicle!=null) |
---|
183 | this.leftCrashSignal=true; |
---|
184 | |
---|
185 | if(this.RAdjancedVehicle!=null) |
---|
186 | this.rightCrashSignal=true; |
---|
187 | |
---|
188 | /* |
---|
189 | if(this.LAdjancedVehicle!=null) |
---|
190 | System.out.println(self+" LAV:"+this.LAdjancedVehicle.getVehicleId()); |
---|
191 | |
---|
192 | if(this.RAdjancedVehicle!=null) |
---|
193 | System.out.println(self+" RAV:"+this.RAdjancedVehicle.getVehicleId()); |
---|
194 | */ |
---|
195 | } |
---|
196 | |
---|
197 | public void EmergencyVehicleWarning(byte[] bytesReceived,CarInfo self) |
---|
198 | { |
---|
199 | //if emergency message received activate emergencyVehicleSignal |
---|
200 | //check the position of the emergency vehicle |
---|
201 | CarInfo sc=new CarInfo(); |
---|
202 | sc.wrap(bytesReceived,2); |
---|
203 | |
---|
204 | if(self.getRoadIdx()==sc.getRoadIdx() && self.getDirection()==sc.getDirection()) |
---|
205 | { |
---|
206 | this.emergencyVehicleSignal=true; |
---|
207 | |
---|
208 | switch(self.getDirection()) |
---|
209 | { |
---|
210 | case 1: if(sc.getPointIdx()>self.getPointIdx()) |
---|
211 | this.emergencyVehicleVpoz=1; |
---|
212 | else |
---|
213 | { |
---|
214 | if(sc.getPointIdx()<self.getPointIdx()) |
---|
215 | this.emergencyVehicleVpoz=0; |
---|
216 | else |
---|
217 | this.emergencyVehicleVpoz=2; |
---|
218 | } |
---|
219 | break; |
---|
220 | case 0: if(sc.getPointIdx()<self.getPointIdx()) |
---|
221 | this.emergencyVehicleVpoz=1; |
---|
222 | else |
---|
223 | { |
---|
224 | if(sc.getPointIdx()>self.getPointIdx()) |
---|
225 | this.emergencyVehicleVpoz=0; |
---|
226 | else |
---|
227 | this.emergencyVehicleVpoz=2; |
---|
228 | } |
---|
229 | break; |
---|
230 | default:break; |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | if(sc.getLane()<self.getLane()) |
---|
235 | this.emergencyVehicleHpoz=1; |
---|
236 | else |
---|
237 | { |
---|
238 | if(sc.getLane()>self.getLane()) |
---|
239 | this.emergencyVehicleHpoz=0; |
---|
240 | else |
---|
241 | this.emergencyVehicleHpoz=2; |
---|
242 | } |
---|
243 | |
---|
244 | } |
---|
245 | Globals.EMERGENCY_RECEIVED++; |
---|
246 | //System.out.println("Car "+self.getVehicleId()+" got emergency message from car "+sc.getVehicleId()); |
---|
247 | } |
---|
248 | |
---|
249 | public boolean getForwardCrashSignal() |
---|
250 | { |
---|
251 | return this.forwardCrashSignal; |
---|
252 | } |
---|
253 | |
---|
254 | public boolean getLeftCrashSignal() |
---|
255 | { |
---|
256 | return this.leftCrashSignal; |
---|
257 | } |
---|
258 | |
---|
259 | public boolean getRightCrashSignal() |
---|
260 | { |
---|
261 | return this.rightCrashSignal; |
---|
262 | } |
---|
263 | |
---|
264 | public boolean getEmergencySignal() |
---|
265 | { |
---|
266 | return this.emergencyVehicleSignal; |
---|
267 | } |
---|
268 | |
---|
269 | public void refreshApplication(int currentTime) |
---|
270 | { |
---|
271 | if(currentTime-this.currentTime>=Globals.WIRELESS_TRANSMISSION_FRAMES+Globals.CCW_REFRESH_TIME) |
---|
272 | { |
---|
273 | this.FVehicle=this.NFVehicle=this.LAdjancedVehicle=this.RAdjancedVehicle=null; |
---|
274 | |
---|
275 | this.lastIRTFV=this.lastIRTNFV=this.lastIRTLAV=this.lastIRTRAV=this.currentTime; |
---|
276 | |
---|
277 | this.FVehicleDistance=this.NFVehicleDistance=this.LAVehicleDistance=this.RAVehicleDistance=30000; |
---|
278 | |
---|
279 | this.forwardCrashSignal=this.leftCrashSignal=this.rightCrashSignal=this.emergencyVehicleSignal=false; |
---|
280 | |
---|
281 | } |
---|
282 | |
---|
283 | this.currentTime=currentTime; |
---|
284 | } |
---|
285 | |
---|
286 | //calculates the FV and the NFV |
---|
287 | private void calculateFCWVehicles(CarInfo self,CarInfo sc) |
---|
288 | { |
---|
289 | |
---|
290 | Road r1,r2 = null; |
---|
291 | Point p1,p2 = null; |
---|
292 | |
---|
293 | r1 = (Road) Globals.map.roads.get(sc.getRoadIdx()); |
---|
294 | p1 = (Point) r1.points.get(sc.getPointIdx()); |
---|
295 | |
---|
296 | r2 = (Road) Globals.map.roads.get(self.getRoadIdx()); |
---|
297 | p2 = (Point) r2.points.get(self.getPointIdx()); |
---|
298 | |
---|
299 | |
---|
300 | //refresh the FV |
---|
301 | if(GPSutil.distance(p1, p2) <= this.FVehicleDistance && self.getRoadIdx()==sc.getRoadIdx() && self.getDirection()==sc.getDirection() && self.getLane()==sc.getLane()) |
---|
302 | { |
---|
303 | |
---|
304 | switch(self.getDirection()) |
---|
305 | { |
---|
306 | case 1: if(sc.getPointIdx()>self.getPointIdx()) |
---|
307 | { |
---|
308 | this.FVehicle=sc; |
---|
309 | this.FVehicleDistance=GPSutil.distance(p1, p2); |
---|
310 | Globals.CUMMULATIVE_PACKETS_FV++; |
---|
311 | Globals.IRT_FV+=this.currentTime-this.lastIRTFV; |
---|
312 | this.lastIRTFV=this.currentTime; |
---|
313 | return; |
---|
314 | } |
---|
315 | break; |
---|
316 | case 0 :if(sc.getPointIdx()<self.getPointIdx()) |
---|
317 | { |
---|
318 | this.FVehicle=sc; |
---|
319 | this.FVehicleDistance=GPSutil.distance(p1, p2); |
---|
320 | Globals.CUMMULATIVE_PACKETS_FV++; |
---|
321 | Globals.IRT_FV+=this.currentTime-this.lastIRTFV; |
---|
322 | this.lastIRTFV=this.currentTime; |
---|
323 | return; |
---|
324 | } |
---|
325 | break; |
---|
326 | default: break; |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | } |
---|
331 | |
---|
332 | if(this.FVehicle==null) |
---|
333 | return; |
---|
334 | |
---|
335 | |
---|
336 | //refresh the NFV |
---|
337 | if(this.FVehicle.getVehicleId()!=sc.getVehicleId() && GPSutil.distance(p1, p2) <= this.NFVehicleDistance && self.getRoadIdx()==sc.getRoadIdx() && self.getDirection()==sc.getDirection() && self.getLane()==sc.getLane()) |
---|
338 | { |
---|
339 | switch(self.getDirection()) |
---|
340 | { |
---|
341 | case 1: if(sc.getPointIdx()>self.getPointIdx()) |
---|
342 | { |
---|
343 | this.NFVehicle=sc; |
---|
344 | this.NFVehicleDistance=GPSutil.distance(p1, p2); |
---|
345 | Globals.CUMMULATIVE_PACKETS_NFV++; |
---|
346 | Globals.IRT_NFV+=this.currentTime-this.lastIRTNFV; |
---|
347 | this.lastIRTNFV=this.currentTime; |
---|
348 | } |
---|
349 | break; |
---|
350 | case 0 :if(sc.getPointIdx()<self.getPointIdx()) |
---|
351 | { |
---|
352 | this.NFVehicle=sc; |
---|
353 | this.NFVehicleDistance=GPSutil.distance(p1, p2); |
---|
354 | Globals.CUMMULATIVE_PACKETS_NFV++; |
---|
355 | Globals.IRT_NFV+=this.currentTime-this.lastIRTNFV; |
---|
356 | this.lastIRTNFV=this.currentTime; |
---|
357 | } |
---|
358 | break; |
---|
359 | default: break; |
---|
360 | } |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | //calculates the LAV and the RAV |
---|
365 | private void calculateLCAVehicles(CarInfo self,CarInfo sc) |
---|
366 | { |
---|
367 | Road r1,r2 = null; |
---|
368 | Point p1,p2 = null; |
---|
369 | |
---|
370 | r1 = (Road) Globals.map.roads.get(sc.getRoadIdx()); |
---|
371 | p1 = (Point) r1.points.get(sc.getPointIdx()); |
---|
372 | |
---|
373 | r2 = (Road) Globals.map.roads.get(self.getRoadIdx()); |
---|
374 | p2 = (Point) r2.points.get(self.getPointIdx()); |
---|
375 | |
---|
376 | //refresh the LAV and RAV |
---|
377 | if(self.getRoadIdx()==sc.getRoadIdx() && self.getDirection()==sc.getDirection() && self.getLane()!=sc.getLane() && Math.abs(self.getPointIdx()-sc.getPointIdx())<=1) |
---|
378 | { |
---|
379 | if(sc.getLane()==self.getLane()+1 && GPSutil.distance(p1,p2)<this.LAVehicleDistance) |
---|
380 | { |
---|
381 | this.LAdjancedVehicle=sc; |
---|
382 | this.LAVehicleDistance=GPSutil.distance(p1,p2); |
---|
383 | Globals.CUMMULATIVE_PACKETS_LAV++; |
---|
384 | Globals.IRT_LAV+=this.currentTime-this.lastIRTLAV; |
---|
385 | this.lastIRTLAV=this.currentTime; |
---|
386 | return; |
---|
387 | } |
---|
388 | |
---|
389 | if(sc.getLane()==self.getLane()-1 && GPSutil.distance(p1,p2)<this.RAVehicleDistance) |
---|
390 | { |
---|
391 | this.RAdjancedVehicle=sc; |
---|
392 | this.RAVehicleDistance=GPSutil.distance(p1,p2); |
---|
393 | Globals.CUMMULATIVE_PACKETS_RAV++; |
---|
394 | Globals.IRT_RAV+=this.currentTime-this.lastIRTRAV; |
---|
395 | this.lastIRTRAV=this.currentTime; |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | public String getFV() |
---|
401 | { |
---|
402 | return ((this.FVehicle==null)?"none":(Integer.toString(this.FVehicle.getVehicleId()))); |
---|
403 | } |
---|
404 | |
---|
405 | public String getNFV() |
---|
406 | { |
---|
407 | return ((this.NFVehicle==null)?"none":(Integer.toString(this.NFVehicle.getVehicleId()))); |
---|
408 | } |
---|
409 | |
---|
410 | public String getLAV() |
---|
411 | { |
---|
412 | return ((this.LAdjancedVehicle==null)?"none":(Integer.toString(this.LAdjancedVehicle.getVehicleId()))); |
---|
413 | } |
---|
414 | |
---|
415 | public String getRAV() |
---|
416 | { |
---|
417 | return ((this.RAdjancedVehicle==null)?"none":(Integer.toString(this.RAdjancedVehicle.getVehicleId()))); |
---|
418 | } |
---|
419 | |
---|
420 | public CarInfo getFVC() |
---|
421 | { |
---|
422 | return this.FVehicle; |
---|
423 | } |
---|
424 | |
---|
425 | public CarInfo getNFVC() |
---|
426 | { |
---|
427 | return this.NFVehicle; |
---|
428 | } |
---|
429 | |
---|
430 | public CarInfo getLAVC() |
---|
431 | { |
---|
432 | return this.LAdjancedVehicle; |
---|
433 | } |
---|
434 | |
---|
435 | public CarInfo getRAVC() |
---|
436 | { |
---|
437 | return this.RAdjancedVehicle; |
---|
438 | } |
---|
439 | } |
---|