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 Headway headwayGTU = simplePerception.getForwardHeadwayGtu();
75 AccelerationStep accelerationStepGTU = null;
76 if (headwayGTU.getDistance().ge(maxDistance))
77 {
78
79 accelerationStepGTU = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
80 laneBasedGTU, lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
81 }
82 else
83 {
84 accelerationStepGTU =
85 ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headwayGTU.getSpeed(),
86 headwayGTU.getDistance(), lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
87 }
88
89
90 Headway headwayObject = simplePerception.getForwardHeadwayObject();
91 AccelerationStep accelerationStepObject = null;
92 if (headwayObject.getDistance().ge(maxDistance))
93 {
94 accelerationStepObject = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
95 laneBasedGTU, lanePathInfo.path().getLength(), simplePerception.getSpeedLimit());
96 }
97 else
98 {
99 accelerationStepObject = ((GtuFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
100 headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.path().getLength(),
101 simplePerception.getSpeedLimit());
102 }
103
104
105 AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
106 ? accelerationStepGTU : accelerationStepObject;
107
108
109 if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
110 {
111 return OperationalPlan.standStill(getGtu(), getGtu().getLocation(), startTime, Duration.ONE);
112 }
113 OtsLine2d path = lanePathInfo.path();
114 OperationalPlan op = new OperationalPlan(getGtu(), path, startTime,
115 Segments.off(getGtu().getSpeed(), accelerationStep.getDuration(), accelerationStep.getAcceleration()));
116 return op;
117 }
118
119
120 @Override
121 public final String toString()
122 {
123 return "LaneBasedGtuFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
124 }
125 }