[31] | 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.applications.adaptiveTL; |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | import java.io.ByteArrayInputStream; |
---|
| 10 | import java.io.ByteArrayOutputStream; |
---|
| 11 | import java.io.Serializable; |
---|
| 12 | import java.nio.ByteBuffer; |
---|
| 13 | import java.util.ArrayList; |
---|
| 14 | import java.util.Collections; |
---|
| 15 | import java.util.LinkedList; |
---|
| 16 | import java.util.TreeMap; |
---|
| 17 | |
---|
| 18 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
| 19 | import vnsim.core.*; |
---|
| 20 | import vnsim.core.events.ReceiveEvent; |
---|
| 21 | import vnsim.core.events.SendEvent; |
---|
| 22 | import vnsim.core.events.UnicastSendEvent; |
---|
| 23 | import vnsim.map.object.*; |
---|
| 24 | import vnsim.map.utils.GPSutil; |
---|
| 25 | import vnsim.network.RadioDev; |
---|
| 26 | import vnsim.vehicular.routePlan.RoutingConstants; |
---|
| 27 | import vnsim.vehicular.simulator.intersections.*; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | public class WirelessTrafficLight |
---|
| 35 | extends IntersectionWithTrafficLights |
---|
| 36 | implements Communicator{ |
---|
| 37 | |
---|
| 38 | private static final long serialVersionUID = 8724226247069906455L; |
---|
| 39 | |
---|
| 40 | public final static byte PROT_RESERVED = 0; |
---|
| 41 | |
---|
| 42 | public static final double LOW_SPEED = 5.0; // km/h |
---|
| 43 | public static final double MAX_QUEUE_GAP = 0.02; // km |
---|
| 44 | |
---|
| 45 | public ArrayList <IntersectionCarRecord> trafficDB = new ArrayList<IntersectionCarRecord>(); |
---|
| 46 | |
---|
| 47 | public ArrayList <IntersectionCarRecord> intersectionCars = new ArrayList<IntersectionCarRecord>(); |
---|
| 48 | |
---|
| 49 | public Integer DBmutex = new Integer(0); |
---|
| 50 | |
---|
| 51 | // public ArrayList<TrafficLightInfo> newLightInfos; |
---|
| 52 | public int newCycleLength; |
---|
| 53 | |
---|
| 54 | public int phases; |
---|
| 55 | |
---|
| 56 | int lastMediumTransmission; |
---|
| 57 | |
---|
| 58 | public LinkedList<ReceiveEvent> receiveEvents = new LinkedList<ReceiveEvent>(); |
---|
| 59 | |
---|
| 60 | public TreeMap<Integer,Integer> messagePeriods = new TreeMap<Integer,Integer>(); |
---|
| 61 | |
---|
| 62 | boolean promiscuousMode; |
---|
| 63 | |
---|
| 64 | public static boolean adaptive = true; |
---|
| 65 | // public LinkedList lastHourHistory = new LinkedList(); |
---|
| 66 | |
---|
| 67 | protected boolean initialized = false; |
---|
| 68 | |
---|
| 69 | final static int CRTDIR = 0; |
---|
| 70 | final static int OPPDIR = 1; |
---|
| 71 | |
---|
| 72 | private final RadioDev radio = new RadioDev(this); |
---|
| 73 | |
---|
| 74 | public void init(){ |
---|
| 75 | |
---|
| 76 | for (int i = 0; i < segments.size(); i++){ |
---|
| 77 | DirectedRoadSegment seg = segments.get(i); |
---|
| 78 | seg.segmentIndex = i; |
---|
| 79 | seg.intersection = this; |
---|
| 80 | Road r1 = Globals.map.roads.get(seg.roadIndex); |
---|
| 81 | seg.endOfQueue = new IntersectionCarRecord[r1.laneNo + 1];//new ArrayList<IntersectionCarRecord>(r1.laneNo); |
---|
| 82 | seg.queueDischargeRate = new double[r1.laneNo+1]; |
---|
| 83 | |
---|
| 84 | for (int j = 0; j <= r1.laneNo; j++){ |
---|
| 85 | seg.endOfQueue[j] = new IntersectionCarRecord(null); |
---|
| 86 | seg.queueDischargeRate[j] = -1; |
---|
| 87 | } |
---|
| 88 | if (seg.isExitOnly()) |
---|
| 89 | continue; |
---|
| 90 | for (int j = 0; j < segments.size(); j++){ |
---|
| 91 | if (i != j && !segments.get(j).isEntryOnly()){ |
---|
| 92 | seg.paths.add(new IntersectionPath(this, seg, segments.get(j))); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | Intersection intersection = null; |
---|
| 97 | if(seg.direction) { |
---|
| 98 | for(int k=0; k < r1.crosses.size();k++) { |
---|
| 99 | if(r1.crosses.get(k).getPointIndex() > seg.pointIndex) { |
---|
| 100 | //that's the next cross |
---|
| 101 | intersection=Globals.map.allIntersections.get(r1.crosses.get(k).intersectionIndex); |
---|
| 102 | seg.nextwtlPointIndex = r1.crosses.get(k).getPointIndex(); |
---|
| 103 | break; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } else { |
---|
| 107 | for(int k = r1.crosses.size()-1; k >= 0; k--) { |
---|
| 108 | if(r1.crosses.get(k).getPointIndex() < seg.pointIndex) { |
---|
| 109 | //that's the next cross |
---|
| 110 | intersection=Globals.map.allIntersections.get(r1.crosses.get(k).intersectionIndex); |
---|
| 111 | seg.nextwtlPointIndex = r1.crosses.get(k).getPointIndex(); |
---|
| 112 | break; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | if (intersection == null){ |
---|
| 117 | seg.nextwtl = null; |
---|
| 118 | } |
---|
| 119 | if (intersection instanceof WirelessTrafficLight){ |
---|
| 120 | seg.nextwtl = (WirelessTrafficLight)intersection; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | public void computeNextSignalPlan(int crtTime){ |
---|
| 127 | |
---|
| 128 | if (crtTime < 3 * Globals.MINUTE){ |
---|
| 129 | newCycleLength = cycleLength; |
---|
| 130 | for( int j = 0; j < segments.size(); j++ ) { |
---|
| 131 | segments.get(j).setNewLightInfo(segments.get(j).getLightInfo()); |
---|
| 132 | } |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | if (adaptive){ |
---|
| 136 | int interGreenTime = 0; |
---|
| 137 | interGreenTime = Globals.DEFAULT_YELLOW_TIME + Globals.DEFAULT_ALL_RED; |
---|
| 138 | |
---|
| 139 | int green=30; |
---|
| 140 | int time=0, j, lostTime = 0; |
---|
| 141 | double Y = 0, ycr = 0; |
---|
| 142 | int[] v = new int[segments.size()]; |
---|
| 143 | double[] yc = new double[segments.size()]; |
---|
| 144 | |
---|
| 145 | for( j = 0; j < segments.size(); j++ ) |
---|
| 146 | v[j] = 0; |
---|
| 147 | |
---|
| 148 | // CALCULATE SUM OF CRITICAL FLOW RATIOS |
---|
| 149 | if (Globals.map.allIntersections.size() >= 2) |
---|
| 150 | if(this.equals(Globals.map.allIntersections.get(1))){ |
---|
| 151 | v[0] = 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | // System.out.println("%%%%%%%%%% The intersections"); |
---|
| 155 | // System.out.println(Globals.map.allIntersections.toString()); |
---|
| 156 | |
---|
| 157 | phases = 0; |
---|
| 158 | for( j = 0; j < segments.size(); j++ ) { |
---|
| 159 | DirectedRoadSegment seg1 = segments.get(j); |
---|
| 160 | if (v[j] == 1) |
---|
| 161 | continue; |
---|
| 162 | phases ++; |
---|
| 163 | if (!seg1.isExitOnly()){ |
---|
| 164 | double satflow = Globals.SATURATION_FLOW * seg1.road.laneNo; |
---|
| 165 | // if (segments.get(j).road.laneNo > 1) |
---|
| 166 | // satflow -= 300; |
---|
| 167 | double correction = 0; |
---|
| 168 | if (seg1.demand > seg1.totalVolume) |
---|
| 169 | correction = (double)(seg1.demand - seg1.totalVolume); |
---|
| 170 | yc[j] = (double)(seg1.demand + seg1.carryOver) / satflow; |
---|
| 171 | }else{ |
---|
| 172 | yc[j] = 0; |
---|
| 173 | } |
---|
| 174 | v[seg1.segmentIndex] = 1; |
---|
| 175 | |
---|
| 176 | /*if (Globals.map.roads.get(0).getName().startsWith("Iuliu")){ |
---|
| 177 | if (j < 2) |
---|
| 178 | seg1.pair = seg1.front; |
---|
| 179 | else{ |
---|
| 180 | seg1.pair = null; |
---|
| 181 | } |
---|
| 182 | }else*/{ |
---|
| 183 | seg1.pair = seg1.front; |
---|
| 184 | if (seg1.front != null) |
---|
| 185 | seg1.front.pair = seg1; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | DirectedRoadSegment seg2 = seg1.pair; |
---|
| 189 | if (seg2 == null){ |
---|
| 190 | Y += yc[j]; |
---|
| 191 | continue; |
---|
| 192 | }else{ |
---|
| 193 | if (!seg2.isExitOnly()){ |
---|
| 194 | double satflow = Globals.SATURATION_FLOW * seg2.road.laneNo; |
---|
| 195 | // if (segments.get(j).road.laneNo > 1) |
---|
| 196 | // satflow -= 300; |
---|
| 197 | double correction = 0; |
---|
| 198 | if (seg2.demand > seg2.totalVolume) |
---|
| 199 | correction = (double)(seg2.demand - seg2.totalVolume); |
---|
| 200 | yc[seg2.segmentIndex] = (double)(seg2.demand + seg2.carryOver) / satflow; |
---|
| 201 | }else{ |
---|
| 202 | yc[seg2.segmentIndex] = 0; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | ycr = yc[j]; |
---|
| 207 | if (ycr < yc[seg2.segmentIndex]){ |
---|
| 208 | ycr = yc[seg2.segmentIndex]; |
---|
| 209 | yc[j] = ycr; |
---|
| 210 | }else{ |
---|
| 211 | yc[seg2.segmentIndex] = ycr; |
---|
| 212 | } |
---|
| 213 | Y += ycr; |
---|
| 214 | v[seg2.segmentIndex] = 1; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | // CALCULATE CYCLE LENGTH WITH WEBSTER EQUATION |
---|
| 218 | if (Y >= 1){ |
---|
| 219 | newCycleLength = Globals.MAXIMUM_CYCLE_LENGTH; |
---|
| 220 | }else{ |
---|
| 221 | lostTime = (int)Math.ceil((double)segments.size() / 2) * interGreenTime; |
---|
| 222 | //Webster's equation |
---|
| 223 | newCycleLength = (int)((double)(1.5 * lostTime + 5) / (1 - Globals.Xc * Y)); |
---|
| 224 | // newCycleLength = (int)(2.0 * newCycleLength); |
---|
| 225 | if (newCycleLength > Globals.MAXIMUM_CYCLE_LENGTH || newCycleLength < 0) |
---|
| 226 | newCycleLength = Globals.MAXIMUM_CYCLE_LENGTH; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | if (Globals.monitorWTL != null && this.equals(Globals.monitorWTL)){ |
---|
| 230 | Globals.pw.print(crtTime/Globals.SECOND + " "); |
---|
| 231 | } |
---|
| 232 | //COMPUTE GREEN SPLITS |
---|
| 233 | |
---|
| 234 | // newLightInfos = new ArrayList<TrafficLightInfo>(); |
---|
| 235 | double extra = 0, grprop; |
---|
| 236 | double maxIncreaseProp = 1; |
---|
| 237 | int[] greens = new int[segments.size()]; |
---|
| 238 | double[] increaseProp = new double[segments.size()]; |
---|
| 239 | int count = 0, sum = 0, dec = 0; |
---|
| 240 | |
---|
| 241 | for(j=0;j<segments.size();j++) { |
---|
| 242 | DirectedRoadSegment seg = segments.get(j); |
---|
| 243 | |
---|
| 244 | grprop = (double)(newCycleLength - lostTime) * yc[j] / Y; |
---|
| 245 | green = (int) Math.ceil(grprop); |
---|
| 246 | greens[j] = green; |
---|
| 247 | if (greens[j] == 0){ |
---|
| 248 | greens[j] = 1; |
---|
| 249 | } |
---|
| 250 | // System.out.println("Greens [ "+j+" ] " + greens[j]); |
---|
| 251 | // get the perpendicular segments with green for pedestrians |
---|
| 252 | int maxPedestrianTime = 0; |
---|
| 253 | if (seg.left != null && seg.left.pedestrianCrossingTime > maxPedestrianTime){ |
---|
| 254 | maxPedestrianTime = (int)seg.left.pedestrianCrossingTime; |
---|
| 255 | } |
---|
| 256 | if (seg.right != null && seg.right.pedestrianCrossingTime > maxPedestrianTime){ |
---|
| 257 | maxPedestrianTime = (int)seg.right.pedestrianCrossingTime; |
---|
| 258 | } |
---|
| 259 | // compare to minimum accepted green |
---|
| 260 | int mingreen = (int)(maxPedestrianTime + (double)Globals.MIN_WALK_LIGHT_TIME/Globals.SECOND); |
---|
| 261 | // System.out.println("MaxPedestrianTime [ "+j+" ] " + mingreen); |
---|
| 262 | increaseProp[j] = 0; |
---|
| 263 | if (green + interGreenTime < mingreen){ |
---|
| 264 | increaseProp[j] = (double)(mingreen - interGreenTime) / greens[j]; |
---|
| 265 | // System.out.println("Increase [ "+j+" ] " + increaseProp[j]); |
---|
| 266 | if (maxIncreaseProp < increaseProp[j]) |
---|
| 267 | maxIncreaseProp = increaseProp[j]; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | for( j = 0; j < segments.size(); j++ ) |
---|
| 272 | v[j] = 0; |
---|
| 273 | |
---|
| 274 | for(j=0;j<segments.size();j ++) { |
---|
| 275 | DirectedRoadSegment seg1 = segments.get(j); |
---|
| 276 | if (v[j] == 1) |
---|
| 277 | continue; |
---|
| 278 | if (maxIncreaseProp > 0){ |
---|
| 279 | greens[j] = (int)Math.round(maxIncreaseProp * greens[j]); |
---|
| 280 | } |
---|
| 281 | if ( increaseProp[j] == 0){ |
---|
| 282 | count ++; |
---|
| 283 | } |
---|
| 284 | sum += greens[j] + interGreenTime; |
---|
| 285 | v[seg1.segmentIndex] = 1; |
---|
| 286 | DirectedRoadSegment seg2 = seg1.pair; |
---|
| 287 | if (seg2 != null){ |
---|
| 288 | if (maxIncreaseProp > 0){ |
---|
| 289 | greens[seg2.segmentIndex] = (int)Math.round(maxIncreaseProp * greens[j]); |
---|
| 290 | } |
---|
| 291 | v[seg2.segmentIndex] = 1; |
---|
| 292 | } |
---|
| 293 | // System.out.println("NewGreens [ "+j+" ] " + greens[j]); |
---|
| 294 | } |
---|
| 295 | if (sum > Globals.MAXIMUM_CYCLE_LENGTH){ |
---|
| 296 | dec = (int)Math.ceil((double)(sum - Globals.MAXIMUM_CYCLE_LENGTH)/count); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | for( j = 0; j < segments.size(); j++ ) |
---|
| 301 | v[j] = 0; |
---|
| 302 | |
---|
| 303 | //SET FINAL TIMING PLANS |
---|
| 304 | for( j = 0; j < segments.size(); j++ ) { |
---|
| 305 | DirectedRoadSegment seg1 = segments.get(j); |
---|
| 306 | if (v[j] == 1) |
---|
| 307 | continue; |
---|
| 308 | |
---|
| 309 | if ( increaseProp[j] == 0 && dec > 0) |
---|
| 310 | greens[j] -= dec; |
---|
| 311 | if (!seg1.isExitOnly()){ |
---|
| 312 | seg1.setNewLightInfo(new TrafficLightInfo(time, time + greens[j])); |
---|
| 313 | }else{ |
---|
| 314 | seg1.setNewLightInfo(new TrafficLightInfo(time, time)); |
---|
| 315 | } |
---|
| 316 | v[seg1.segmentIndex] = 1; |
---|
| 317 | DirectedRoadSegment seg2 = seg1.pair; |
---|
| 318 | if (seg2 != null){ |
---|
| 319 | v[seg2.segmentIndex] = 1; |
---|
| 320 | greens[seg2.segmentIndex] = greens[j]; |
---|
| 321 | if (!seg2.isExitOnly()){ |
---|
| 322 | seg2.setNewLightInfo(new TrafficLightInfo(time, time + greens[j])); |
---|
| 323 | }else{ |
---|
| 324 | seg2.setNewLightInfo(new TrafficLightInfo(time, time)); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | if (Globals.monitorWTL != null && this.equals(Globals.monitorWTL)){ |
---|
| 328 | Globals.pw.print(" " +time +" " + (time + greens[j]) +" "); |
---|
| 329 | } |
---|
| 330 | time = time + greens[j] + interGreenTime; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | newCycleLength = time;//Math.ceil(extra); |
---|
| 334 | if (Globals.monitorWTL != null && this.equals(Globals.monitorWTL)){ |
---|
| 335 | Globals.pw.print(" "+newCycleLength + " s "); |
---|
| 336 | Globals.pw.print(" MaxInc " + maxIncreaseProp+"; \n"); |
---|
| 337 | Globals.pw.flush(); |
---|
| 338 | } |
---|
| 339 | }else{ |
---|
| 340 | newCycleLength = cycleLength; |
---|
| 341 | for( int j = 0; j < segments.size(); j++ ) { |
---|
| 342 | segments.get(j).setNewLightInfo(segments.get(j).getLightInfo()); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | public void extendGreen(int t, int secs, DirectedRoadSegment segment){ |
---|
| 348 | Globals.pw.print("Green Extension\t"); |
---|
| 349 | |
---|
| 350 | TrafficLightInfo tli = segment.getLightInfo(); |
---|
| 351 | if (secs > (tli.greenEnd - tli.greenStart)/2) |
---|
| 352 | secs = (tli.greenEnd - tli.greenStart)/2; |
---|
| 353 | |
---|
| 354 | for(int j = 0; j < segments.size(); j++ ) { |
---|
| 355 | DirectedRoadSegment seg = segments.get(j); |
---|
| 356 | tli = seg.getLightInfo(); |
---|
| 357 | if (t < tli.yellowStart) |
---|
| 358 | seg.setLightInfo(new TrafficLightInfo(tli.greenStart + secs, tli.greenEnd + secs)); |
---|
| 359 | if (seg.equals(segment)) |
---|
| 360 | seg.setLightInfo(new TrafficLightInfo(tli.greenStart, tli.greenEnd + secs)); |
---|
| 361 | tli = seg.getLightInfo(); |
---|
| 362 | Globals.pw.print(" " +tli.greenStart +" " + tli.greenEnd +" "); |
---|
| 363 | } |
---|
| 364 | cycleLength += secs; |
---|
| 365 | Globals.pw.println(" "+cycleLength + " s"); |
---|
| 366 | Globals.pw.flush(); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | int changeTime = -1; |
---|
| 371 | public void step(int crtTime){ |
---|
| 372 | if (!initialized){ |
---|
| 373 | initialized = true; |
---|
| 374 | init(); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | //recommend speeds to cars |
---|
| 378 | // scheduleSingleSendEvent(1, Globals.PROT_TL_FEEDBACK); |
---|
| 379 | |
---|
| 380 | if (crtTime % (10 * Globals.SECOND) == 0){ |
---|
| 381 | for (int k = 0; k < this.segments.size(); k++) { |
---|
| 382 | DirectedRoadSegment seg = segments.get(k); |
---|
| 383 | seg.recordQueueSize(); |
---|
| 384 | } |
---|
| 385 | /*//broadcast feedback |
---|
| 386 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 387 | scheduleSingleSendEvent(0, Globals.PROT_TL_FEEDBACK); |
---|
| 388 | }*/ |
---|
| 389 | } |
---|
| 390 | // compute the next cycle signal plan with 2 seconds before switching |
---|
| 391 | if (crtTime == lastCycleStartTime + cycleLength * Globals.SECOND - 2 * Globals.SECOND){ |
---|
| 392 | |
---|
| 393 | if (Globals.monitorWTL != null && this.equals(Globals.monitorWTL)){ |
---|
| 394 | Globals.pw.println("\n"+crtTime/Globals.SECOND + "s ) WTL"); |
---|
| 395 | } |
---|
| 396 | int dem = 0, vol = 0; |
---|
| 397 | double stops = 0; |
---|
| 398 | for (int k = 0; k < this.segments.size(); k++) { |
---|
| 399 | DirectedRoadSegment seg = segments.get(k); |
---|
| 400 | |
---|
| 401 | dem = seg.computeDemand(Globals.ANALYSIS_PERIOD); |
---|
| 402 | vol = seg.computeVolume(Globals.ANALYSIS_PERIOD); |
---|
| 403 | stops = seg.noOfStops; |
---|
| 404 | int analysisPeriod = Globals.ANALYSIS_PERIOD; |
---|
| 405 | if (Globals.ANALYSIS_PERIOD < crtTime){ |
---|
| 406 | analysisPeriod = crtTime; |
---|
| 407 | } |
---|
| 408 | // if (dem > vol){ |
---|
| 409 | seg.carryOver += (int)((double)(dem - vol) * (double)(cycleLength * Globals.SECOND) / analysisPeriod); |
---|
| 410 | if (seg.carryOver < 0) |
---|
| 411 | seg.carryOver = 0; |
---|
| 412 | // }else{ |
---|
| 413 | // seg.carryOver -= (int)((double)(dem - vol) * (double)(cycleLength * Globals.SECOND) / analysisPeriod); |
---|
| 414 | // } |
---|
| 415 | |
---|
| 416 | Point px = seg.road.points.get(seg.pointIndex); |
---|
| 417 | double qs = seg.get95thQueueSize(); |
---|
| 418 | if (Globals.monitorWTL != null && this.equals(Globals.monitorWTL)){ |
---|
| 419 | Globals.pw.println(" "+ seg.segmentIndex + "" + |
---|
| 420 | " dem: "+seg.demand + " carry: "+seg.carryOver+" vol: " + seg.totalVolume + " stops: " + seg.noOfStops + " avgQueue: " + qs ); |
---|
| 421 | Globals.pw.flush(); |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 425 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 426 | |
---|
| 427 | /*if (segments.get(2).road.getName().startsWith("Iuliu")){ |
---|
| 428 | if (k == 0) |
---|
| 429 | Globals.demo.st.setDemand("sw", vol, dem, true); |
---|
| 430 | if (k == 2) |
---|
| 431 | Globals.demo.st.setDemand("se", vol, dem, true); |
---|
| 432 | if (k == 1) |
---|
| 433 | Globals.demo.st.setDemand("ne", vol, dem, true); |
---|
| 434 | if (k == 3) |
---|
| 435 | Globals.demo.st.setDemand("nw", vol, dem, true); |
---|
| 436 | } |
---|
| 437 | if (segments.get(1).road.getName().startsWith("Dr T")){ |
---|
| 438 | if (k == 0) |
---|
| 439 | Globals.demo.st.setDemand("sw", vol, dem, true); |
---|
| 440 | if (k == 2) |
---|
| 441 | Globals.demo.st.setDemand("se", vol, dem, true); |
---|
| 442 | if (k == 1) |
---|
| 443 | Globals.demo.st.setDemand("ne", vol, dem, true); |
---|
| 444 | if (k == 3) |
---|
| 445 | Globals.demo.st.setDemand("nw", vol, dem, true); |
---|
| 446 | }*/ |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | computeNextSignalPlan(crtTime); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | boolean debug = false; |
---|
| 455 | if (crtTime == lastCycleStartTime + cycleLength * Globals.SECOND){ |
---|
| 456 | debug = true; |
---|
| 457 | lastCycleStartTime = crtTime; |
---|
| 458 | cycleLength = newCycleLength; |
---|
| 459 | for( int j = 0; j < segments.size(); j++ ) { |
---|
| 460 | segments.get(j).setLightInfo(segments.get(j).getNewLightInfo()); |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | int thisSec = (int) (crtTime / (Globals.executionFPS)); |
---|
| 464 | int aux = thisSec - (lastCycleStartTime / Globals.SECOND); |
---|
| 465 | |
---|
| 466 | synchronized (this) { |
---|
| 467 | boolean b = false; |
---|
| 468 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 469 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 470 | Globals.demo.st.cycle.setText("Cycle: " + cycleLength + " s"); |
---|
| 471 | |
---|
| 472 | } |
---|
| 473 | for (int k = 0; k < this.segments.size(); k++) { |
---|
| 474 | TrafficLightInfo info = segments.get(k).getLightInfo(); |
---|
| 475 | int color = info.getColor(aux); |
---|
| 476 | if (color != currentColor.get(k) ){ |
---|
| 477 | currentColor.set(k, color); |
---|
| 478 | int dem = 0, vol = 0; |
---|
| 479 | double stops = 0; |
---|
| 480 | if (segments.get(k).pair != null |
---|
| 481 | && segments.get(k).pair.getLightInfo().getColor(aux-1) == Globals.GREEN |
---|
| 482 | && adaptive){ |
---|
| 483 | int i = segments.get(k).paths.indexOf(new IntersectionPath(segments.get(k), segments.get(k).pair)); |
---|
| 484 | if (i!= -1 && color == Globals.YELLOW){ |
---|
| 485 | int secsEx = segments.get(k).leftSignalExtension(); |
---|
| 486 | if (secsEx > 0){ |
---|
| 487 | System.out.println("Extension"); |
---|
| 488 | extendGreen(aux, secsEx, segments.get(k)); |
---|
| 489 | continue; |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | segments.get(k).changePhase(null,crtTime, color); |
---|
| 494 | if (!segments.get(k).isExitOnly()){ |
---|
| 495 | DirectedRoadSegment seg = segments.get(k); |
---|
| 496 | |
---|
| 497 | dem = seg.computeDemand(Globals.ANALYSIS_PERIOD); |
---|
| 498 | vol = seg.computeVolume(Globals.ANALYSIS_PERIOD); |
---|
| 499 | stops = seg.noOfStops; |
---|
| 500 | |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | b = true; |
---|
| 504 | if (segments.get(k).pair != null){ |
---|
| 505 | int i = segments.get(k).paths.indexOf(new IntersectionPath(segments.get(k), segments.get(k).pair)); |
---|
| 506 | if (i!= -1 && color == Globals.YELLOW){ |
---|
| 507 | IntersectionPhaseStatistics ips= segments.get(k).paths.get(i).getLastGreenPhase(); |
---|
| 508 | |
---|
| 509 | // if (ips!= null) |
---|
| 510 | // System.out.println(crtTime + ") gap ["+segments.get(k)+" "+segments.get(k).front+"] "+ ips.avgGap); |
---|
| 511 | |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 515 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 516 | /*if (segments.get(2).road.getName().startsWith("Iuliu")){ |
---|
| 517 | if (k == 0) |
---|
| 518 | Globals.demo.st.setDemand("sw", vol, dem, true); |
---|
| 519 | if (k == 2) |
---|
| 520 | Globals.demo.st.setDemand("se", vol, dem, true); |
---|
| 521 | if (k == 1) |
---|
| 522 | Globals.demo.st.setDemand("ne", vol, dem, true); |
---|
| 523 | if (k == 3) |
---|
| 524 | Globals.demo.st.setDemand("nw", vol, dem, true); |
---|
| 525 | } |
---|
| 526 | if (segments.get(0).road.getName().startsWith("Dr T")){ |
---|
| 527 | if (k == 0) |
---|
| 528 | Globals.demo.st.setDemand("sw", vol, dem, true); |
---|
| 529 | if (k == 2) |
---|
| 530 | Globals.demo.st.setDemand("se", vol, dem, true); |
---|
| 531 | if (k == 1) |
---|
| 532 | Globals.demo.st.setDemand("ne", vol, dem, true); |
---|
| 533 | if (k == 3) |
---|
| 534 | Globals.demo.st.setDemand("nw", vol, dem, true); |
---|
| 535 | }*/ |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | |
---|
| 540 | |
---|
| 541 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 542 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 543 | int x; |
---|
| 544 | x = info.greenEnd - aux; |
---|
| 545 | if (x < 0) |
---|
| 546 | x = 0; |
---|
| 547 | if (aux < info.greenStart) |
---|
| 548 | x = info.greenEnd - info.greenStart; |
---|
| 549 | // |
---|
| 550 | // if (segments.get(2).road.getName().startsWith("Iuliu")){ |
---|
| 551 | // if (k == 0) |
---|
| 552 | // Globals.demo.st.phase2.setText("Green sw: " + x +" s"); |
---|
| 553 | // if (k == 2) |
---|
| 554 | // Globals.demo.st.phase1.setText("Green se: " + x +" s"); |
---|
| 555 | // if (k == 1) |
---|
| 556 | // Globals.demo.st.phase3.setText("Green ne: " + x +" s"); |
---|
| 557 | // if (k == 3) |
---|
| 558 | // Globals.demo.st.phase4.setText("Green nw: " + x +" s"); |
---|
| 559 | // } |
---|
| 560 | // if (segments.get(0).road.getName().startsWith("Dr T")){ |
---|
| 561 | // if (k == 0) |
---|
| 562 | // Globals.demo.st.phase2.setText("Green sw: " + x +" s"); |
---|
| 563 | // if (k == 2) |
---|
| 564 | // Globals.demo.st.phase1.setText("Green se: " + x +" s"); |
---|
| 565 | // if (k == 1) |
---|
| 566 | // Globals.demo.st.phase3.setText("Green ne: " + x +" s"); |
---|
| 567 | // } |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | if (b){ |
---|
| 573 | changeTime = crtTime; |
---|
| 574 | } |
---|
| 575 | if (debug){ |
---|
| 576 | |
---|
| 577 | } |
---|
| 578 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 579 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 580 | Globals.demo.st.repaint(); |
---|
| 581 | |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | //debug |
---|
| 585 | /* if (Globals.demo.mv.currentIntersection != null && |
---|
| 586 | Globals.demo.mv.currentIntersection.equals(this) && |
---|
| 587 | changeTime != -1 && crtTime == changeTime + 200){ |
---|
| 588 | prevSec = (int) ((changeTime - 11) / (Globals.executionFPS)); |
---|
| 589 | aux = prevSec % this.totalTime; |
---|
| 590 | for (int k = 0; k < this.segments.size(); k++) { |
---|
| 591 | if (!segments.get(k).isExitOnly()){ |
---|
| 592 | |
---|
| 593 | TrafficLightInfo info = this.lightInfos.get(k); |
---|
| 594 | Road r = Globals.map.roads.get(segments.get(k).roadIndex); |
---|
| 595 | int l = segments.get(k).pointIndex; |
---|
| 596 | if (segments.get(k).direction) |
---|
| 597 | l++; |
---|
| 598 | else |
---|
| 599 | l--; |
---|
| 600 | |
---|
| 601 | if (info.getColor(aux) == 1) System.out.print("Red "); |
---|
| 602 | if (info.getColor(aux) == 2) System.out.print("Yellow "); |
---|
| 603 | if (info.getColor(aux) == 3) System.out.print("Green "); |
---|
| 604 | |
---|
| 605 | int n = 0; |
---|
| 606 | for (int i=0; i < segments.get(k).paths.size();i++){ |
---|
| 607 | IntersectionPath ip = segments.get(k).paths.get(i); |
---|
| 608 | |
---|
| 609 | IntersectionPhaseStatistics ips = ip.lastHourHistory.getLast(); |
---|
| 610 | |
---|
| 611 | if (changeTime <= ips.phaseStartFrame ){ |
---|
| 612 | ips = ip.lastHourHistory.get(ip.lastHourHistory.size() - 2); |
---|
| 613 | } |
---|
| 614 | n += ips.getCarsNo(); |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | Road road = Globals.map.roads.get(segments.get(k).roadIndex); |
---|
| 618 | Point p0 = road.points.get(segments.get(k).pointIndex); |
---|
| 619 | if (segments.get(k).cars != null) |
---|
| 620 | for (int i = 1; i < segments.get(k).cars.size(); i ++){ |
---|
| 621 | SimulatedCarInfo sc1 = segments.get(k).cars.get(i-1).car; |
---|
| 622 | SimulatedCarInfo sc2 = segments.get(k).cars.get(i-1).car; |
---|
| 623 | Point p1 = road.points.get(sc1.getPointIdx()); |
---|
| 624 | Point p2 = road.points.get(sc2.getPointIdx()); |
---|
| 625 | if (Math.pow(p1.getDistance() - p0.getDistance(),2) |
---|
| 626 | > Math.pow(p2.getDistance() - p0.getDistance(),2)) |
---|
| 627 | throw new RuntimeException(); |
---|
| 628 | |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | System.out.println(segments.get(k).roadIndex+"; "+r.getName() + " [" + l +"] : " + n); |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | System.out.println(" --------- /"+ crtTime); |
---|
| 635 | System.out.flush(); |
---|
| 636 | }*/ |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | public void onReceive(byte[] bytesReceived, Serializable message, Communicator sender) { |
---|
| 642 | int j, k = 0; |
---|
| 643 | ByteArrayInputStream byteStream = new ByteArrayInputStream(bytesReceived); |
---|
| 644 | byte numberOfRecords, recordsRead = 0; |
---|
| 645 | byte[] b = new byte[Globals.NONAGG_RECORD_SIZE]; |
---|
| 646 | int offset; |
---|
| 647 | |
---|
| 648 | byte protocol = (byte)byteStream.read(); |
---|
| 649 | switch (protocol){ |
---|
| 650 | case Globals.PROT_NEIGHBOR_DISCOVERY: |
---|
| 651 | numberOfRecords = 1; |
---|
| 652 | break; |
---|
| 653 | case Globals.PROT_SETOFCARS: |
---|
| 654 | numberOfRecords = (byte)byteStream.read(); |
---|
| 655 | break; |
---|
| 656 | default: |
---|
| 657 | return; |
---|
| 658 | } |
---|
| 659 | if (numberOfRecords == -1) |
---|
| 660 | return; |
---|
| 661 | |
---|
| 662 | int comState = (byte)byteStream.read(); |
---|
| 663 | |
---|
| 664 | |
---|
| 665 | DirectedRoadSegment messageSegment = null; |
---|
| 666 | boolean sendToNeighborWTL = false; |
---|
| 667 | |
---|
| 668 | offset = 2; |
---|
| 669 | while (recordsRead < numberOfRecords){ |
---|
| 670 | |
---|
| 671 | int n = byteStream.read(b, 0, Globals.NONAGG_RECORD_SIZE); |
---|
| 672 | if (n == -1 || n < Globals.NONAGG_RECORD_SIZE){ |
---|
| 673 | break; |
---|
| 674 | } |
---|
| 675 | offset += Globals.NONAGG_RECORD_SIZE; |
---|
| 676 | recordsRead ++; |
---|
| 677 | |
---|
| 678 | SimulatedCarInfo sc = new SimulatedCarInfo(); |
---|
| 679 | sc.wrap(b); |
---|
| 680 | |
---|
| 681 | Engine e = Globals.engine; |
---|
| 682 | |
---|
| 683 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentIntersection != null && |
---|
| 684 | Globals.demo.mv.currentIntersection.equals(this)){ |
---|
| 685 | j = 0; |
---|
| 686 | } |
---|
| 687 | IntersectionCarRecord icr = new IntersectionCarRecord(sc); |
---|
| 688 | |
---|
| 689 | int i = trafficDB.indexOf(icr); |
---|
| 690 | |
---|
| 691 | // update car |
---|
| 692 | if (i != -1){ |
---|
| 693 | IntersectionCarRecord old = trafficDB.get(i); |
---|
| 694 | DirectedRoadSegment oldseg = null; |
---|
| 695 | DirectedRoadSegment seg = null; |
---|
| 696 | |
---|
| 697 | if (old.getCar().getTimestamp() >= sc.getTimestamp() ) |
---|
| 698 | continue; |
---|
| 699 | if (!(sender instanceof WirelessTrafficLight)){ |
---|
| 700 | old.learnedFromWTL = false; |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | if (old.isPassed()){ |
---|
| 704 | if (recordsRead == 1 && comState == CRTDIR){ |
---|
| 705 | for (j=0; j<segments.size();j++){ |
---|
| 706 | seg = segments.get(j); |
---|
| 707 | if (seg.containsCar(icr)){ |
---|
| 708 | messageSegment = seg; |
---|
| 709 | sendToNeighborWTL = true; |
---|
| 710 | break; |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | continue; |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | for (j=0; j<segments.size();j++){ |
---|
| 718 | seg = segments.get(j); |
---|
| 719 | |
---|
| 720 | |
---|
| 721 | if (seg.containsCar(icr) || seg.carEntersIntersection(icr) || seg.carExitsIntersection(icr)){ |
---|
| 722 | |
---|
| 723 | synchronized(DBmutex){ |
---|
| 724 | // if the old record was on the same segment |
---|
| 725 | oldseg = old.segment; |
---|
| 726 | if (oldseg.cars != null){ |
---|
| 727 | oldseg.cars.remove(old); |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | icr = new IntersectionCarRecord(old, true); |
---|
| 731 | icr.car = sc; |
---|
| 732 | |
---|
| 733 | if (seg.cars == null) |
---|
| 734 | seg.cars = new SortedIntersectionRecordList(seg); |
---|
| 735 | |
---|
| 736 | if (seg.carEntersIntersection(icr) || seg.carExitsIntersection(icr)){ |
---|
| 737 | if (intersectionCars.indexOf(icr) == -1){ |
---|
| 738 | int lane = old.getCar().getLane(); |
---|
| 739 | if (oldseg.endOfQueue[lane].getCar() != null && oldseg.endOfQueue[lane].equals(icr)) |
---|
| 740 | oldseg.endOfQueue[lane] = new IntersectionCarRecord(null); |
---|
| 741 | icr.old = old; |
---|
| 742 | intersectionCars.add(icr); |
---|
| 743 | } |
---|
| 744 | // seg.cars.add(icr); |
---|
| 745 | trafficDB.set(i,icr); |
---|
| 746 | }else |
---|
| 747 | if (seg.isCarApproaching(icr)){ |
---|
| 748 | seg.cars.add(icr); |
---|
| 749 | icr.setPassed(false); |
---|
| 750 | |
---|
| 751 | trafficDB.set(i,icr); |
---|
| 752 | if (seg.endOfQueue[old.getCar().getLane()].getCar() != null && |
---|
| 753 | seg.endOfQueue[old.getCar().getLane()].equals(old)){ |
---|
| 754 | if (old.getCar().getLane() != icr.getCar().getLane()){ |
---|
| 755 | seg.computeEndOfQueue(old); |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | if (seg.isCarQueuing(icr)){ |
---|
| 759 | if (icr.getQueuedTime() == -1) |
---|
| 760 | icr.setQueuedTime(e.crtTime); |
---|
| 761 | if (!icr.countedForDemand){ |
---|
| 762 | icr.increaseDemand(); |
---|
| 763 | } |
---|
| 764 | }else{ |
---|
| 765 | if (!icr.countedForDemand |
---|
| 766 | && icr.distanceTillIntersection() < Globals.DEMAND_DISTANCE_ON_FREE_FLOW){ |
---|
| 767 | |
---|
| 768 | icr.increaseDemand(); |
---|
| 769 | } |
---|
| 770 | } |
---|
| 771 | // if (recordsRead == 1 && comState == OPPDIR){ |
---|
| 772 | // //first car |
---|
| 773 | // messageSegment = seg; |
---|
| 774 | // sendToNeighborWTL = true; |
---|
| 775 | // } |
---|
| 776 | }else{ |
---|
| 777 | // the car has just passed through the intersection |
---|
| 778 | int z = intersectionCars.indexOf(icr); |
---|
| 779 | if (z!= -1){ |
---|
| 780 | intersectionCars.remove(icr); |
---|
| 781 | old = icr.old; |
---|
| 782 | } |
---|
| 783 | if (oldseg != null){ |
---|
| 784 | oldseg.recordCar(old, seg); |
---|
| 785 | int lane = old.getCar().getLane(); |
---|
| 786 | if(z == -1) |
---|
| 787 | if (oldseg.endOfQueue[lane].getCar() != null && oldseg.endOfQueue[lane].equals(icr)) |
---|
| 788 | oldseg.endOfQueue[lane] = new IntersectionCarRecord(null); |
---|
| 789 | |
---|
| 790 | }else{ |
---|
| 791 | System.out.println("[DEBUG - WTL] Car passed but oldseg = null"); |
---|
| 792 | } |
---|
| 793 | if (recordsRead == 1 && comState == CRTDIR){ |
---|
| 794 | //first car |
---|
| 795 | if (Globals.map.allIntersections.size()>=2 && Globals.map.allIntersections.get(1).equals(this)){ |
---|
| 796 | messageSegment = seg; |
---|
| 797 | |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | messageSegment = seg; |
---|
| 801 | sendToNeighborWTL = true; |
---|
| 802 | } |
---|
| 803 | icr.setPassed(true); |
---|
| 804 | trafficDB.set(i,icr); |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | } |
---|
| 808 | break; |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | if (j == segments.size()){ |
---|
| 814 | System.out.println("WTL" + this.toString() + ": car not on my segments: " +sc); |
---|
| 815 | synchronized(DBmutex){ |
---|
| 816 | trafficDB.remove(i); |
---|
| 817 | } |
---|
| 818 | return; |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | } |
---|
| 822 | // new car |
---|
| 823 | else{ |
---|
| 824 | DirectedRoadSegment seg = null; |
---|
| 825 | for (j=0; j<segments.size();j++){ |
---|
| 826 | seg = segments.get(j); |
---|
| 827 | if (seg.containsCar(icr)){ |
---|
| 828 | |
---|
| 829 | if (seg.isCarApproaching(icr)){ |
---|
| 830 | synchronized(DBmutex){ |
---|
| 831 | if (seg.cars == null){ |
---|
| 832 | seg.cars = new SortedIntersectionRecordList(seg); |
---|
| 833 | } |
---|
| 834 | seg.cars.add(icr); |
---|
| 835 | trafficDB.add(icr); |
---|
| 836 | |
---|
| 837 | if (sender instanceof WirelessTrafficLight) |
---|
| 838 | icr.init(this, seg, true); |
---|
| 839 | else |
---|
| 840 | icr.init(this, seg, false); |
---|
| 841 | if (seg.get95thQueueSize() > 0.15){ |
---|
| 842 | icr.increaseDemand(); |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | // if (recordsRead == 1 && comState == OPPDIR){ |
---|
| 846 | // //first car |
---|
| 847 | // messageSegment = seg; |
---|
| 848 | // sendToNeighborWTL = true; |
---|
| 849 | // } |
---|
| 850 | }else{ |
---|
| 851 | if (recordsRead == 1 && comState == CRTDIR){ |
---|
| 852 | messageSegment = seg; |
---|
| 853 | sendToNeighborWTL = true; |
---|
| 854 | } |
---|
| 855 | } |
---|
| 856 | break; |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | } |
---|
| 860 | } |
---|
| 861 | // Got stack overflow when 2 traffic lights at the end of a segment |
---|
| 862 | // route are sending messages to each other |
---|
| 863 | //if (sendToNeighborWTL && messageSegment.nextwtl != null){ |
---|
| 864 | // messageSegment.nextwtl.onReceive(bytesReceived, this); |
---|
| 865 | //} |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | public byte[] prepareMessage(int messageType) { |
---|
| 869 | byte[] bytesToSend = null; |
---|
| 870 | |
---|
| 871 | if (messageType == Globals.PROT_TL_FEEDBACK){ |
---|
| 872 | ByteBuffer bb = ByteBuffer.allocate(Globals.TL_FEEDBACK_SIZE); |
---|
| 873 | bb.put(Globals.PROT_TL_FEEDBACK); |
---|
| 874 | |
---|
| 875 | bb.putShort((short)cycleLength); |
---|
| 876 | int thisSec = (int) (Globals.engine.crtTime / (Globals.SECOND)); |
---|
| 877 | int aux = thisSec - (lastCycleStartTime / Globals.SECOND); |
---|
| 878 | bb.put((byte)segments.size()); |
---|
| 879 | for (int k = 0; k < this.segments.size(); k++) { |
---|
| 880 | Road rx = Globals.map.roads.get(segments.get(k).roadIndex); |
---|
| 881 | Point px = rx.points.get(segments.get(k).pointIndex); |
---|
| 882 | bb.putShort((short)segments.get(k).roadIndex); |
---|
| 883 | bb.putShort((short)segments.get(k).pointIndex); |
---|
| 884 | if (segments.get(k).direction == true) |
---|
| 885 | bb.put((byte)1); |
---|
| 886 | else |
---|
| 887 | bb.put((byte)0); |
---|
| 888 | |
---|
| 889 | TrafficLightInfo info = segments.get(k).getLightInfo(); |
---|
| 890 | int color = info.getColor(aux); |
---|
| 891 | bb.put((byte)color); |
---|
| 892 | int remaining = -1; |
---|
| 893 | if (color == Globals.GREEN){ |
---|
| 894 | remaining = info.greenEnd - aux; |
---|
| 895 | } |
---|
| 896 | if (color == Globals.RED){ |
---|
| 897 | if (aux < info.greenStart) |
---|
| 898 | remaining = info.greenStart - aux; |
---|
| 899 | else{ |
---|
| 900 | remaining = info.greenStart + (cycleLength - aux); |
---|
| 901 | // remaining = - remaining; |
---|
| 902 | } |
---|
| 903 | } |
---|
| 904 | bb.putShort((short)remaining); |
---|
| 905 | bb.putShort((short)(cycleLength - (info.greenEnd - info.greenStart))); |
---|
| 906 | |
---|
| 907 | for (int i = 1; i < segments.get(k).endOfQueue.length;i++){ |
---|
| 908 | IntersectionCarRecord icr = segments.get(k).endOfQueue[i]; |
---|
| 909 | if (icr.car == null){ |
---|
| 910 | bb.putDouble(0); |
---|
| 911 | }else{ |
---|
| 912 | Road r = Globals.map.roads.get(icr.car.getRoadIdx()); |
---|
| 913 | Point p = r.points.get(icr.car.getPointIdx()); |
---|
| 914 | double distance = p.getDistance() - px.getDistance(); |
---|
| 915 | if (distance < 0) |
---|
| 916 | distance = - distance; |
---|
| 917 | bb.putDouble(distance); |
---|
| 918 | } |
---|
| 919 | } |
---|
| 920 | } |
---|
| 921 | bytesToSend = bb.array(); |
---|
| 922 | } |
---|
| 923 | return bytesToSend; |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | public int getLastMediumTransmition() { |
---|
| 927 | return lastMediumTransmission; |
---|
| 928 | } |
---|
| 929 | public void setLastMediumTransmition(int lastMediumTransmission) { |
---|
| 930 | this.lastMediumTransmission = lastMediumTransmission; |
---|
| 931 | } |
---|
| 932 | |
---|
| 933 | public boolean mediumAvailable(int crtTime){ |
---|
| 934 | int fps = Globals.engine.fps; |
---|
| 935 | if (crtTime - lastMediumTransmission >= Globals.WIRELESS_TRANSMISSION_FRAMES) |
---|
| 936 | return true; |
---|
| 937 | return false; |
---|
| 938 | } |
---|
| 939 | |
---|
| 940 | public boolean isPeriodicalMessage(int messageType){ |
---|
| 941 | return (messageType >= 0); |
---|
| 942 | } |
---|
| 943 | |
---|
| 944 | public int getPeriod(int messageType){ |
---|
| 945 | try{ |
---|
| 946 | return messagePeriods.get(new Integer(messageType)).intValue(); |
---|
| 947 | }catch(Exception e){ |
---|
| 948 | return -1; |
---|
| 949 | } |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | public boolean isPromiscuousMode() { |
---|
| 953 | return promiscuousMode; |
---|
| 954 | } |
---|
| 955 | public void setPromiscuousMode(boolean promiscuousMode) { |
---|
| 956 | this.promiscuousMode = promiscuousMode; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | /** |
---|
| 960 | * delay of the new event (in frames) |
---|
| 961 | * @return the identifier of the new message type |
---|
| 962 | */ |
---|
| 963 | public int scheduleSingleSendEvent(int delay, int messageType){ |
---|
| 964 | Engine engine = Globals.engine; |
---|
| 965 | int newType = PROT_RESERVED; |
---|
| 966 | if (messageType != -1) |
---|
| 967 | newType = Globals.PROT_TL_FEEDBACK; |
---|
| 968 | |
---|
| 969 | // if (singleMessageNo < MIN_NEG_INT) singleMessageNo = -1; |
---|
| 970 | |
---|
| 971 | engine.schedEvent(new SendEvent(engine.crtTime + delay, this, newType)); |
---|
| 972 | return newType; |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | @Override |
---|
| 976 | public String toString() { |
---|
| 977 | String s = "[ "; |
---|
| 978 | for (int i = 0; i < segments.size(); i++){ |
---|
| 979 | if (i > 0 && segments.get(i-1).road.getName().equals(segments.get(i).road.getName())){ |
---|
| 980 | continue; |
---|
| 981 | } |
---|
| 982 | s += segments.get(i).road.getName().trim()+" "; |
---|
| 983 | } |
---|
| 984 | s += "]"; |
---|
| 985 | return s; |
---|
| 986 | } |
---|
| 987 | |
---|
| 988 | public short getRoadIdx(){ |
---|
| 989 | return (short)segments.get(0).roadIndex; |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | public short getPointIdx(){ |
---|
| 993 | return (short)segments.get(0).pointIndex; |
---|
| 994 | } |
---|
| 995 | |
---|
| 996 | public RadioDev getRadio(){ |
---|
| 997 | return radio; |
---|
| 998 | } |
---|
| 999 | |
---|
| 1000 | public ReceiveEvent getReceiveEventForTime(int t){ |
---|
| 1001 | int i = receiveEvents.indexOf(new ReceiveEvent(t, null, null, null, 0)); |
---|
| 1002 | if (i == -1) |
---|
| 1003 | return null; |
---|
| 1004 | return receiveEvents.get(i); |
---|
| 1005 | } |
---|
| 1006 | |
---|
| 1007 | public void addReceiveEventForTime(ReceiveEvent re){ |
---|
| 1008 | receiveEvents.add(re); |
---|
| 1009 | } |
---|
| 1010 | public void removeReceiveEventForTime(int t){ |
---|
| 1011 | int i = receiveEvents.indexOf(new ReceiveEvent(t, null, null, null, 0)); |
---|
| 1012 | if (i == -1) |
---|
| 1013 | return; |
---|
| 1014 | receiveEvents.remove(i); |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | } |
---|