1 package org.opentrafficsim.demo.carFollowing;
2
3 import org.djunits.unit.LengthUnit;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Speed;
6 import org.opentrafficsim.core.network.NetworkException;
7 import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator;
8 import org.opentrafficsim.road.gtu.lane.LaneBasedGTUCharacteristics;
9 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
10 import org.opentrafficsim.road.network.lane.Lane;
11
12
13
14
15
16
17
18
19
20
21 public class CanPlaceDemoCode implements LaneBasedGTUGenerator.RoomChecker
22 {
23
24 private static Length.Rel maxDistance = new Length.Rel(Double.MAX_VALUE, LengthUnit.SI);
25
26
27 private static Length.Rel precision = new Length.Rel(0.1, LengthUnit.METER);
28
29
30 @Override
31 public Speed canPlace(Speed leaderSpeed, org.djunits.value.vdouble.scalar.Length.Rel headway,
32 LaneBasedGTUCharacteristics laneBasedGTUCharacteristics) throws NetworkException
33 {
34
35
36 Lane lane = null;
37 for (DirectedLanePosition dlp : laneBasedGTUCharacteristics.getInitialLongitudinalPositions())
38 {
39 if (dlp.getLane().getLaneType().isCompatible(laneBasedGTUCharacteristics.getGTUType()))
40 {
41 lane = dlp.getLane();
42 break;
43 }
44 }
45 if (null == lane)
46 {
47 throw new NetworkException("No " + laneBasedGTUCharacteristics.getGTUType()
48 + "-compatible lane in initial longitudinal positions");
49 }
50
51 Speed speedLimit = lane.getSpeedLimit(laneBasedGTUCharacteristics.getGTUType());
52 Speed maximumVelocity = laneBasedGTUCharacteristics.getMaximumVelocity();
53 if (headway.lt(laneBasedGTUCharacteristics.getStrategicalPlanner().getDrivingCharacteristics().getGTUFollowingModel()
54 .minimumHeadway(leaderSpeed, leaderSpeed, precision, maxDistance, speedLimit, maximumVelocity)))
55 {
56 return null;
57 }
58 return leaderSpeed;
59 }
60
61 }