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.base.parameters.Parameters;
9 import org.opentrafficsim.core.gtu.GtuException;
10 import org.opentrafficsim.core.gtu.GtuType;
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 public class HeadwayGtuReal extends AbstractHeadway implements HeadwayGtu
42 {
43
44 private static final long serialVersionUID = 20170324L;
45
46
47 private SpeedLimitInfo speedLimitInfo;
48
49
50 private final LaneBasedGtu gtu;
51
52
53 private final boolean facingSameDirection;
54
55
56
57
58
59
60
61
62 public HeadwayGtuReal(final LaneBasedGtu gtu, final Length distance, final boolean facingSameDirection) throws GtuException
63 {
64 super(distance);
65 this.gtu = gtu;
66 this.facingSameDirection = facingSameDirection;
67 }
68
69
70
71
72
73
74
75
76
77
78 public HeadwayGtuReal(final LaneBasedGtu gtu, final Length overlapFront, final Length overlap, final Length overlapRear,
79 final boolean facingSameDirection) throws GtuException
80 {
81 super(overlapFront, overlap, overlapRear);
82 this.gtu = gtu;
83 this.facingSameDirection = facingSameDirection;
84 }
85
86
87
88
89
90
91 private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu wrappedGtu)
92 {
93 SpeedLimitInfo sli = new SpeedLimitInfo();
94 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
95 try
96 {
97 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
98 wrappedGtu.getReferencePosition().lane().getSpeedLimit(wrappedGtu.getType()));
99 }
100 catch (NetworkException | GtuException exception)
101 {
102 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
103 }
104 return sli;
105 }
106
107 @Override
108 public final CarFollowingModel getCarFollowingModel()
109 {
110 return this.gtu.getTacticalPlanner().getCarFollowingModel();
111 }
112
113 @Override
114 public final Parameters getParameters()
115 {
116 return this.gtu.getParameters();
117 }
118
119 @Override
120 public final SpeedLimitInfo getSpeedLimitInfo()
121 {
122 if (this.speedLimitInfo == null)
123 {
124 this.speedLimitInfo = getSpeedLimitInfo(this.gtu);
125 }
126 return this.speedLimitInfo;
127 }
128
129 @Override
130 public final Route getRoute()
131 {
132 return this.gtu.getStrategicalPlanner().getRoute();
133 }
134
135
136
137
138
139
140
141 @Override
142 public final HeadwayGtu moved(final Length headway, final Speed speed, final Acceleration acceleration)
143 {
144 try
145 {
146 return new HeadwayGtuRealCopy(getId(), getGtuType(), headway, getLength(), getWidth(), speed, acceleration,
147 getCarFollowingModel(), getParameters(), getSpeedLimitInfo(), getRoute(), getDesiredSpeed(),
148 getGtuStatus());
149 }
150 catch (GtuException exception)
151 {
152
153 throw new RuntimeException("Exception while copying Headway GTU.", exception);
154 }
155 }
156
157
158
159
160
161 private GtuStatus[] getGtuStatus()
162 {
163 EnumSet<GtuStatus> gtuStatus = EnumSet.noneOf(GtuStatus.class);
164 if (isLeftTurnIndicatorOn())
165 {
166 gtuStatus.add(GtuStatus.LEFT_TURNINDICATOR);
167 }
168 if (isRightTurnIndicatorOn())
169 {
170 gtuStatus.add(GtuStatus.RIGHT_TURNINDICATOR);
171 }
172 if (isBrakingLightsOn())
173 {
174 gtuStatus.add(GtuStatus.BRAKING_LIGHTS);
175 }
176 if (isEmergencyLightsOn())
177 {
178 gtuStatus.add(GtuStatus.EMERGENCY_LIGHTS);
179 }
180 if (isHonking())
181 {
182 gtuStatus.add(GtuStatus.HONK);
183 }
184 return gtuStatus.toArray(new GtuStatus[gtuStatus.size()]);
185 }
186
187
188
189
190
191 public LaneBasedGtu getGtu()
192 {
193 return this.gtu;
194 }
195
196 @Override
197 public final String getId()
198 {
199 return this.gtu.getId();
200 }
201
202 @Override
203 public final Length getLength()
204 {
205 return this.gtu.getLength();
206 }
207
208 @Override
209 public Length getWidth()
210 {
211 return this.gtu.getWidth();
212 }
213
214 @Override
215 public final Speed getSpeed()
216 {
217 return this.gtu.getSpeed();
218 }
219
220 @Override
221 public Speed getDesiredSpeed()
222 {
223 return this.gtu.getDesiredSpeed();
224 }
225
226 @Override
227 public final ObjectType getObjectType()
228 {
229 return ObjectType.GTU;
230 }
231
232 @Override
233 public final Acceleration getAcceleration()
234 {
235 return this.gtu.getAcceleration();
236 }
237
238 @Override
239 public final GtuType getGtuType()
240 {
241 return this.gtu.getType();
242 }
243
244 @Override
245 public final boolean isFacingSameDirection()
246 {
247 return this.facingSameDirection;
248 }
249
250 @Override
251 public final boolean isBrakingLightsOn()
252 {
253
254 return false;
255 }
256
257 @Override
258 public final boolean isLeftTurnIndicatorOn()
259 {
260 return this.gtu.getTurnIndicatorStatus().isLeft();
261 }
262
263 @Override
264 public final boolean isRightTurnIndicatorOn()
265 {
266 return this.gtu.getTurnIndicatorStatus().isRight();
267 }
268
269 @Override
270 public final boolean isEmergencyLightsOn()
271 {
272 return this.gtu.getTurnIndicatorStatus().isHazard();
273 }
274
275 @Override
276 public final boolean isHonking()
277 {
278
279 return false;
280 }
281
282 @Override
283 public final String toString()
284 {
285 return "HeadwayGtuReal [speedLimitInfo=" + this.speedLimitInfo + ", gtu=" + this.gtu + ", facingSameDirection="
286 + this.facingSameDirection + "]";
287 }
288
289 }