1 package org.opentrafficsim.road.gtu.lane.tactical;
2
3 import org.djunits.value.vdouble.scalar.Duration;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Time;
6 import org.djutils.draw.point.OrientedPoint2d;
7 import org.opentrafficsim.base.parameters.ParameterException;
8 import org.opentrafficsim.core.geometry.OtsLine2d;
9 import org.opentrafficsim.core.gtu.GtuException;
10 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
11 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
12 import org.opentrafficsim.core.gtu.plan.operational.Segments;
13 import org.opentrafficsim.core.network.NetworkException;
14 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
15 import org.opentrafficsim.road.gtu.lane.perception.CategoricalLanePerception;
16 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
17 import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
18 import org.opentrafficsim.road.gtu.lane.perception.categories.DirectDefaultSimplePerception;
19 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
20 import org.opentrafficsim.road.gtu.lane.tactical.following.AccelerationStep;
21 import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class LaneBasedGtuFollowingTacticalPlanner extends AbstractLaneBasedTacticalPlanner
37 {
38
39 private static final long serialVersionUID = 20151125L;
40
41
42
43
44
45
46 public LaneBasedGtuFollowingTacticalPlanner(final GtuFollowingModelOld carFollowingModel, final LaneBasedGtu gtu)
47 {
48 super(carFollowingModel, gtu, new CategoricalLanePerception(gtu));
49 getPerception().addPerceptionCategory(new DirectDefaultSimplePerception(getPerception()));
50 }
51
52
53 @Override
54 public final OperationalPlan generateOperationalPlan(final Time startTime,
55 final OrientedPoint2d locationAtStartTime)
56 throws OperationalPlanException, NetworkException, GtuException, ParameterException
57 {
58
59 LaneBasedGtu laneBasedGTU = getGtu();
60 LanePerception perception = getPerception();
61
62
63 if (laneBasedGTU.getMaximumSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
64 {
65 return OperationalPlan.standStill(getGtu(), getGtu().getLocation(), startTime, Duration.ONE);
66 }
67
68
69 Length maxDistance = laneBasedGTU.getParameters().getParameter(LOOKAHEAD);
70 LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);
71
72
73 DefaultSimplePerception simplePerception = perception.getPerceptionCategory(DefaultSimplePerception.class);
74 simplePerception.updateForwardHeadwayGtu();
75 simplePerception.updateSpeedLimit();
76 simplePerception.updateForwardHeadwayObject();
77 Headway headwayGTU = simplePerception.getForwardHeadwayGtu();
78 AccelerationStep accelerationStepGTU = null;
79 if (headwayGTU.getDistance().ge(maxDistance))
80 {
81
82 accelerationStepGTU = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
83 laneBasedGTU, lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
84 }
85 else
86 {
87 accelerationStepGTU =
88 ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headwayGTU.getSpeed(),
89 headwayGTU.getDistance(), lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
90 }
91
92
93 Headway headwayObject = simplePerception.getForwardHeadwayObject();
94 AccelerationStep accelerationStepObject = null;
95 if (headwayObject.getDistance().ge(maxDistance))
96 {
97 accelerationStepObject = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
98 laneBasedGTU, lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
99 }
100 else
101 {
102 accelerationStepObject = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
103 headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.path().getLength(),
104 simplePerception.getSpeedLimit());
105 }
106
107
108 AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
109 ? accelerationStepGTU : accelerationStepObject;
110
111
112 if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
113 {
114 return OperationalPlan.standStill(getGtu(), getGtu().getLocation(), startTime, Duration.ONE);
115 }
116 OtsLine2d path = lanePathInfo.path();
117 OperationalPlan op = new OperationalPlan(getGtu(), path, startTime,
118 Segments.off(getGtu().getSpeed(), accelerationStep.getDuration(), accelerationStep.getAcceleration()));
119 return op;
120 }
121
122
123 @Override
124 public final String toString()
125 {
126 return "LaneBasedGtuFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
127 }
128 }