1 package org.opentrafficsim.road.gtu.lane.perception.headway;
2
3 import java.util.EnumSet;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.opentrafficsim.core.gtu.GTUException;
9 import org.opentrafficsim.core.gtu.GTUType;
10 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
11 import org.opentrafficsim.core.network.NetworkException;
12 import org.opentrafficsim.core.network.route.Route;
13 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
14 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
15 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
16 import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class HeadwayGTUReal extends AbstractHeadway implements HeadwayGTU
45 {
46
47 private static final long serialVersionUID = 20170324L;
48
49
50 private final SpeedLimitInfo speedLimitInfo;
51
52
53 private final LaneBasedGTU gtu;
54
55
56 private final boolean facingSameDirection;
57
58
59
60
61
62
63
64
65 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length distance, final boolean facingSameDirection) throws GTUException
66 {
67 super(distance);
68 this.gtu = gtu;
69 this.facingSameDirection = facingSameDirection;
70 this.speedLimitInfo = getSpeedLimitInfo(gtu);
71 }
72
73
74
75
76
77
78
79
80
81
82 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length overlapFront, final Length overlap, final Length overlapRear,
83 final boolean facingSameDirection) throws GTUException
84 {
85 super(overlapFront, overlap, overlapRear);
86 this.gtu = gtu;
87 this.facingSameDirection = facingSameDirection;
88 this.speedLimitInfo = getSpeedLimitInfo(gtu);
89 }
90
91
92
93
94
95
96
97 private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU wrappedGtu)
98 {
99 SpeedLimitInfo sli = new SpeedLimitInfo();
100 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
101 try
102 {
103 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
104 wrappedGtu.getReferencePosition().getLane().getSpeedLimit(wrappedGtu.getGTUType()));
105 }
106 catch (NetworkException | GTUException exception)
107 {
108 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
109 }
110 return sli;
111 }
112
113
114 @Override
115 public final CarFollowingModel getCarFollowingModel()
116 {
117 return this.gtu.getTacticalPlanner().getCarFollowingModel();
118 }
119
120
121 @Override
122 public final BehavioralCharacteristics getBehavioralCharacteristics()
123 {
124 return this.gtu.getBehavioralCharacteristics();
125 }
126
127
128 @Override
129 public final SpeedLimitInfo getSpeedLimitInfo()
130 {
131 return this.speedLimitInfo;
132 }
133
134
135 @Override
136 public final Route getRoute()
137 {
138 return this.gtu.getStrategicalPlanner().getRoute();
139 }
140
141
142
143
144
145
146
147 @Override
148 public HeadwayGTU moved(final Length headway, final Speed speed, final Acceleration acceleration)
149 {
150 try
151 {
152 return new HeadwayGTURealCopy(getId(), getGtuType(), headway, getLength(), speed, acceleration,
153 getCarFollowingModel(), getBehavioralCharacteristics(), getSpeedLimitInfo(), getRoute(), getGtuStatus());
154 }
155 catch (GTUException exception)
156 {
157
158 throw new RuntimeException("Exception while copying Headway GTU.", exception);
159 }
160 }
161
162
163
164
165
166 private GTUStatus[] getGtuStatus()
167 {
168 EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
169 if (isLeftTurnIndicatorOn())
170 {
171 gtuStatus.add(GTUStatus.LEFT_TURNINDICATOR);
172 }
173 if (isRightTurnIndicatorOn())
174 {
175 gtuStatus.add(GTUStatus.RIGHT_TURNINDICATOR);
176 }
177 if (isBrakingLightsOn())
178 {
179 gtuStatus.add(GTUStatus.BRAKING_LIGHTS);
180 }
181 if (isEmergencyLightsOn())
182 {
183 gtuStatus.add(GTUStatus.EMERGENCY_LIGHTS);
184 }
185 if (isHonking())
186 {
187 gtuStatus.add(GTUStatus.HONK);
188 }
189 return gtuStatus.toArray(new GTUStatus[gtuStatus.size()]);
190 }
191
192
193 @Override
194 public String getId()
195 {
196 return this.gtu.getId();
197 }
198
199
200 @Override
201 public Length getLength()
202 {
203 return this.gtu.getLength();
204 }
205
206
207 @Override
208 public Speed getSpeed()
209 {
210 return this.gtu.getSpeed();
211 }
212
213
214 @Override
215 public ObjectType getObjectType()
216 {
217 return ObjectType.GTU;
218 }
219
220
221 @Override
222 public Acceleration getAcceleration()
223 {
224 return this.gtu.getAcceleration();
225 }
226
227
228 @Override
229 public GTUType getGtuType()
230 {
231 return this.gtu.getGTUType();
232 }
233
234
235 @Override
236 public boolean isFacingSameDirection()
237 {
238 return this.facingSameDirection;
239 }
240
241
242 @Override
243 public boolean isBrakingLightsOn()
244 {
245
246 return false;
247 }
248
249
250 @Override
251 public boolean isLeftTurnIndicatorOn()
252 {
253 return this.gtu.getTurnIndicatorStatus().isLeft();
254 }
255
256
257 @Override
258 public boolean isRightTurnIndicatorOn()
259 {
260 return this.gtu.getTurnIndicatorStatus().isRight();
261 }
262
263
264 @Override
265 public boolean isEmergencyLightsOn()
266 {
267 return this.gtu.getTurnIndicatorStatus().isHazard();
268 }
269
270
271 @Override
272 public boolean isHonking()
273 {
274
275 return false;
276 }
277
278 }