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)
66 throws GTUException
67 {
68 super(distance);
69 this.gtu = gtu;
70 this.facingSameDirection = facingSameDirection;
71 this.speedLimitInfo = getSpeedLimitInfo(gtu);
72 }
73
74
75
76
77
78
79
80
81
82
83 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length overlapFront, final Length overlap,
84 final Length overlapRear, final boolean facingSameDirection) throws GTUException
85 {
86 super(overlapFront, overlap, overlapRear);
87 this.gtu = gtu;
88 this.facingSameDirection = facingSameDirection;
89 this.speedLimitInfo = getSpeedLimitInfo(gtu);
90 }
91
92
93
94
95
96
97
98 private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU wrappedGtu)
99 {
100 SpeedLimitInfo sli = new SpeedLimitInfo();
101 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
102 try
103 {
104 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
105 wrappedGtu.getReferencePosition().getLane().getSpeedLimit(wrappedGtu.getGTUType()));
106 }
107 catch (NetworkException | GTUException exception)
108 {
109 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
110 }
111 return sli;
112 }
113
114
115 @Override
116 public final CarFollowingModel getCarFollowingModel()
117 {
118 return this.gtu.getTacticalPlanner().getCarFollowingModel();
119 }
120
121
122 @Override
123 public final BehavioralCharacteristics getBehavioralCharacteristics()
124 {
125 return this.gtu.getBehavioralCharacteristics();
126 }
127
128
129 @Override
130 public final SpeedLimitInfo getSpeedLimitInfo()
131 {
132 return this.speedLimitInfo;
133 }
134
135
136 @Override
137 public final Route getRoute()
138 {
139 return this.gtu.getStrategicalPlanner().getRoute();
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, getCarFollowingModel(),
153 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 }