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 * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
20 * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
21 * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
22 * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
23 * This particular version returns behavioral information about the observed GTU objects based on their real state.<br>
24 * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
25 * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
26 * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
27 * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
28 * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with
29 * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
30 * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
31 * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
32 * positions. See examples in
33 * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
34 * <p>
35 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37 * </p>
38 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
39 * initial version May 27, 2016 <br>
40 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
42 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
43 */
44 public class HeadwayGTUReal extends AbstractHeadway implements HeadwayGTU
45 {
46 /** */
47 private static final long serialVersionUID = 20170324L;
48
49 /** Stored speed limit info of the observed GTU. */
50 private SpeedLimitInfo speedLimitInfo;
51
52 /** Wrapped GTU. */
53 private final LaneBasedGTU gtu;
54
55 /** Whether the GTU is facing the same direction. */
56 private final boolean facingSameDirection;
57
58 /**
59 * Construct a new Headway information object, for a GTU ahead of us or behind us.
60 * @param gtu LaneBasedGTU; the observed GTU, can not be null.
61 * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
62 * @param facingSameDirection boolean; whether the GTU is facing the same direction.
63 * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
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 }
71
72 /**
73 * Construct a new Headway information object, for a GTU parallel with us.
74 * @param gtu LaneBasedGTU; the observed GTU, can not be null.
75 * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
76 * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
77 * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
78 * @param facingSameDirection boolean; whether the GTU is facing the same direction.
79 * @throws GTUException when id is null, or parameters are inconsistent
80 */
81 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length overlapFront, final Length overlap, final Length overlapRear,
82 final boolean facingSameDirection) throws GTUException
83 {
84 super(overlapFront, overlap, overlapRear);
85 this.gtu = gtu;
86 this.facingSameDirection = facingSameDirection;
87 }
88
89 /**
90 * Creates speed limit prospect for given GTU.
91 * @param wrappedGtu LaneBasedGTU; gtu to the the speed limit prospect for
92 * @return speed limit prospect for given GTU
93 */
94 private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU wrappedGtu)
95 {
96 SpeedLimitInfonetwork/speed/SpeedLimitInfo.html#SpeedLimitInfo">SpeedLimitInfo sli = new SpeedLimitInfo();
97 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
98 try
99 {
100 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
101 wrappedGtu.getReferencePosition().getLane().getSpeedLimit(wrappedGtu.getGTUType()));
102 }
103 catch (NetworkException | GTUException exception)
104 {
105 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
106 }
107 return sli;
108 }
109
110 /** {@inheritDoc} */
111 @Override
112 public final CarFollowingModel getCarFollowingModel()
113 {
114 return this.gtu.getTacticalPlanner().getCarFollowingModel();
115 }
116
117 /** {@inheritDoc} */
118 @Override
119 public final Parameters getParameters()
120 {
121 return this.gtu.getParameters();
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public final SpeedLimitInfo getSpeedLimitInfo()
127 {
128 if (this.speedLimitInfo == null)
129 {
130 this.speedLimitInfo = getSpeedLimitInfo(this.gtu);
131 }
132 return this.speedLimitInfo;
133 }
134
135 /** {@inheritDoc} */
136 @Override
137 public final Route getRoute()
138 {
139 return this.gtu.getStrategicalPlanner().getRoute();
140 }
141
142 /**
143 * {@inheritDoc} <br>
144 * <br>
145 * <b>Note: when moving a {@code HeadwayGTURealDirect}, only headway, speed and acceleration may be considered to be delayed
146 * and anticipated. Other information is taken from the actual GTU at the time {@code moved()} is called.</b>
147 */
148 @Override
149 public final HeadwayGTU moved(final Length headway, final Speed speed, final Acceleration acceleration)
150 {
151 try
152 {
153 return new HeadwayGTURealCopy(getId(), getGtuType(), headway, getLength(), getWidth(), speed, acceleration,
154 getCarFollowingModel(), getParameters(), getSpeedLimitInfo(), getRoute(), getDesiredSpeed(),
155 getGtuStatus());
156 }
157 catch (GTUException exception)
158 {
159 // input should be consistent
160 throw new RuntimeException("Exception while copying Headway GTU.", exception);
161 }
162 }
163
164 /**
165 * Returns an array with GTU status.
166 * @return array with GTU status
167 */
168 private GTUStatus[] getGtuStatus()
169 {
170 EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
171 if (isLeftTurnIndicatorOn())
172 {
173 gtuStatus.add(GTUStatus.LEFT_TURNINDICATOR);
174 }
175 if (isRightTurnIndicatorOn())
176 {
177 gtuStatus.add(GTUStatus.RIGHT_TURNINDICATOR);
178 }
179 if (isBrakingLightsOn())
180 {
181 gtuStatus.add(GTUStatus.BRAKING_LIGHTS);
182 }
183 if (isEmergencyLightsOn())
184 {
185 gtuStatus.add(GTUStatus.EMERGENCY_LIGHTS);
186 }
187 if (isHonking())
188 {
189 gtuStatus.add(GTUStatus.HONK);
190 }
191 return gtuStatus.toArray(new GTUStatus[gtuStatus.size()]);
192 }
193
194 /** {@inheritDoc} */
195 @Override
196 public final String getId()
197 {
198 return this.gtu.getId();
199 }
200
201 /** {@inheritDoc} */
202 @Override
203 public final Length getLength()
204 {
205 return this.gtu.getLength();
206 }
207
208 /** {@inheritDoc} */
209 @Override
210 public Length getWidth()
211 {
212 return this.gtu.getWidth();
213 }
214
215 /** {@inheritDoc} */
216 @Override
217 public final Speed getSpeed()
218 {
219 return this.gtu.getSpeed();
220 }
221
222 /** {@inheritDoc} */
223 @Override
224 public Speed getDesiredSpeed()
225 {
226 return this.gtu.getDesiredSpeed();
227 }
228
229 /** {@inheritDoc} */
230 @Override
231 public final ObjectType getObjectType()
232 {
233 return ObjectType.GTU;
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public final Acceleration getAcceleration()
239 {
240 return this.gtu.getAcceleration();
241 }
242
243 /** {@inheritDoc} */
244 @Override
245 public final GTUType getGtuType()
246 {
247 return this.gtu.getGTUType();
248 }
249
250 /** {@inheritDoc} */
251 @Override
252 public final boolean isFacingSameDirection()
253 {
254 return this.facingSameDirection;
255 }
256
257 /** {@inheritDoc} */
258 @Override
259 public final boolean isBrakingLightsOn()
260 {
261 // TODO
262 return false;
263 }
264
265 /** {@inheritDoc} */
266 @Override
267 public final boolean isLeftTurnIndicatorOn()
268 {
269 return this.gtu.getTurnIndicatorStatus().isLeft();
270 }
271
272 /** {@inheritDoc} */
273 @Override
274 public final boolean isRightTurnIndicatorOn()
275 {
276 return this.gtu.getTurnIndicatorStatus().isRight();
277 }
278
279 /** {@inheritDoc} */
280 @Override
281 public final boolean isEmergencyLightsOn()
282 {
283 return this.gtu.getTurnIndicatorStatus().isHazard();
284 }
285
286 /** {@inheritDoc} */
287 @Override
288 public final boolean isHonking()
289 {
290 // TODO
291 return false;
292 }
293
294 /** {@inheritDoc} */
295 @Override
296 public final String toString()
297 {
298 return "HeadwayGTUReal [speedLimitInfo=" + this.speedLimitInfo + ", gtu=" + this.gtu + ", facingSameDirection="
299 + this.facingSameDirection + "]";
300 }
301
302 }