1 package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.djunits.value.vdouble.scalar.Acceleration;
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.djunits.value.vdouble.scalar.Speed;
9 import org.djunits.value.vdouble.scalar.Time;
10 import org.opentrafficsim.base.parameters.ParameterException;
11 import org.opentrafficsim.core.gtu.GTUException;
12 import org.opentrafficsim.core.gtu.GTUType;
13 import org.opentrafficsim.core.gtu.RelativePosition;
14 import org.opentrafficsim.core.network.NetworkException;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16 import org.opentrafficsim.road.gtu.lane.control.CACC;
17 import org.opentrafficsim.road.gtu.lane.perception.DownstreamNeighborsIterable;
18 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
19 import org.opentrafficsim.road.gtu.lane.perception.LaneRecord;
20 import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
21 import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
22 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
23 import org.opentrafficsim.road.gtu.lane.perception.categories.LaneBasedAbstractPerceptionCategory;
24 import org.opentrafficsim.road.gtu.lane.perception.headway.GTUStatus;
25 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
26 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUSimple;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class AccPerception extends LaneBasedAbstractPerceptionCategory implements LongitudinalControllerPerception
40 {
41
42
43 private static final long serialVersionUID = 20190312L;
44
45
46 private final HeadwayGtuType sensors;
47
48
49
50
51
52 public AccPerception(final LanePerception perception)
53 {
54 this(perception, new OnboardSensors());
55 }
56
57
58
59
60
61
62 public AccPerception(final LanePerception perception, final HeadwayGtuType sensors)
63 {
64 super(perception);
65 this.sensors = sensors;
66 }
67
68
69 @Override
70 public void updateAll() throws GTUException, NetworkException, ParameterException
71 {
72
73 }
74
75
76 @Override
77 public PerceptionCollectable<HeadwayGTU, LaneBasedGTU> getLeaders()
78 {
79 return computeIfAbsent("leaders", () -> computeLeaders());
80 }
81
82
83
84
85
86 private PerceptionCollectable<HeadwayGTU, LaneBasedGTU> computeLeaders()
87 {
88 try
89 {
90 LaneStructureRecord record = getPerception().getLaneStructure().getRootRecord();
91 Length pos = record.getStartDistance().neg();
92 pos = record.getDirection().isPlus() ? pos.plus(getGtu().getFront().getDx())
93 : pos.minus(getGtu().getFront().getDx());
94 boolean ignoreIfUpstream = true;
95 return new DownstreamNeighboursIterableACC(getGtu(), record, Length.max(Length.ZERO, pos),
96 getGtu().getParameters().getParameter(RANGE), getGtu().getFront(), this.sensors, RelativeLane.CURRENT,
97 ignoreIfUpstream);
98 }
99 catch (ParameterException | GTUException exception)
100 {
101 throw new RuntimeException("Unexpected exception while computing gtu alongside.", exception);
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117 private static class DownstreamNeighboursIterableACC extends DownstreamNeighborsIterable
118 {
119
120
121
122
123
124
125
126
127
128
129
130
131 @SuppressWarnings("checkstyle:parameternumber")
132 DownstreamNeighboursIterableACC(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root,
133 final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
134 final HeadwayGtuType headwayGtuType, final RelativeLane lane, final boolean ignoreIfUpstream)
135 {
136 super(perceivingGtu, root, initialPosition, maxDistance, relativePosition, headwayGtuType, lane, ignoreIfUpstream);
137 }
138
139
140 @Override
141 protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
142 {
143 Entry next;
144 do
145 {
146 next = super.getNext(record, position, counter);
147 }
148
149 while (next != null && counter > 1 && !(next.getObject().getTacticalPlanner() instanceof CACC));
150 return next;
151 }
152
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 private static class OnboardSensors implements HeadwayGtuType
169 {
170
171
172
173
174 OnboardSensors()
175 {
176
177 }
178
179
180 @Override
181 public HeadwayGTU createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
182 final Length distance) throws GTUException, ParameterException
183 {
184 Time t = perceivingGtu.getSimulator().getSimulatorTime().minus(perceivingGtu.getParameters().getParameter(DELAY));
185 String id = perceivedGtu.getId();
186 GTUType gtuType = perceivedGtu.getGTUType();
187 Length length = perceivedGtu.getLength();
188 Length width = perceivedGtu.getWidth();
189 Speed v = perceivedGtu.getSpeed(t);
190 Acceleration a = perceivedGtu.getAcceleration(t);
191 Speed desiredSpeed = null;
192 List<GTUStatus> status = new ArrayList<>();
193 if (perceivedGtu.isBrakingLightsOn(t))
194 {
195 status.add(GTUStatus.BRAKING_LIGHTS);
196 }
197 switch (perceivedGtu.getTurnIndicatorStatus(t))
198 {
199 case HAZARD:
200 status.add(GTUStatus.EMERGENCY_LIGHTS);
201 break;
202 case LEFT:
203 status.add(GTUStatus.LEFT_TURNINDICATOR);
204 break;
205 case RIGHT:
206 status.add(GTUStatus.RIGHT_TURNINDICATOR);
207 break;
208 default:
209 break;
210 }
211 return new HeadwayGTUSimple(id, gtuType, distance, length, width, v, a, desiredSpeed,
212 status.toArray(new GTUStatus[status.size()]));
213 }
214
215
216 @Override
217 public HeadwayGTU createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
218 final Length distance) throws GTUException, ParameterException
219 {
220 throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
221 }
222
223
224 @Override
225 public HeadwayGTU createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
226 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
227 {
228 throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
229 }
230
231 }
232
233 }