1 package org.opentrafficsim.road.gtu.lane;
2
3 import java.util.Map;
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.djunits.value.vdouble.scalar.Time;
9 import org.opentrafficsim.core.gtu.GTU;
10 import org.opentrafficsim.core.gtu.GTUDirectionality;
11 import org.opentrafficsim.core.gtu.GTUException;
12 import org.opentrafficsim.core.gtu.RelativePosition;
13 import org.opentrafficsim.core.gtu.TurnIndicatorStatus;
14 import org.opentrafficsim.core.network.LateralDirectionality;
15 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
16 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
17 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
18 import org.opentrafficsim.road.network.lane.Lane;
19
20 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
21 import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
22 import nl.tudelft.simulation.event.EventType;
23 import nl.tudelft.simulation.language.d3.DirectedPoint;
24
25 /**
26 * This interface defines a lane based GTU.
27 * <p>
28 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
30 * <p>
31 * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
32 * initial version Oct 22, 2014 <br>
33 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
35 */
36 public interface LaneBasedGTU extends GTU
37 {
38
39 /** {@inheritDoc} */
40 @Override
41 LaneBasedStrategicalPlanner getStrategicalPlanner();
42
43 /** {@inheritDoc} */
44 @Override
45 LaneBasedStrategicalPlanner getStrategicalPlanner(Time time);
46
47 /** {@inheritDoc} */
48 @Override
49 default LaneBasedTacticalPlanner getTacticalPlanner()
50 {
51 return getStrategicalPlanner().getTacticalPlanner();
52 }
53
54 /** {@inheritDoc} */
55 @Override
56 default LaneBasedTacticalPlanner getTacticalPlanner(final Time time)
57 {
58 return getStrategicalPlanner(time).getTacticalPlanner(time);
59 }
60
61 /**
62 * Return the location without a RemoteException. {@inheritDoc}
63 */
64 @Override
65 DirectedPoint getLocation();
66
67 /**
68 * insert GTU at a certain position. This can happen at setup (first initialization), and after a lane change of the GTU.
69 * The relative position that will be registered is the referencePosition (dx, dy, dz) = (0, 0, 0). Front and rear positions
70 * are relative towards this position.
71 * @param lane the lane to add to the list of lanes on which the GTU is registered.
72 * @param gtuDirection the direction of the GTU on the lane (which can be bidirectional). If the GTU has a positive speed,
73 * it is moving in this direction.
74 * @param position the position on the lane.
75 * @throws GTUException when positioning the GTU on the lane causes a problem
76 */
77 void enterLane(Lane lane, Length position, GTUDirectionality gtuDirection) throws GTUException;
78
79 /**
80 * Unregister the GTU from a lane.
81 * @param lane the lane to remove from the list of lanes on which the GTU is registered.
82 * @throws GTUException when leaveLane should not be called
83 */
84 void leaveLane(Lane lane) throws GTUException;
85
86 /**
87 * Change lanes instantaneously.
88 * @param laneChangeDirection the direction to change to
89 * @throws GTUException in case lane change fails
90 */
91 void changeLaneInstantaneously(LateralDirectionality laneChangeDirection) throws GTUException;
92
93 /**
94 * Return the longitudinal positions of a point relative to this GTU, relative to the center line of the Lanes in which the
95 * vehicle is registered. <br>
96 * <b>Note:</b> If a GTU is registered in multiple parallel lanes, the lateralLaneChangeModel is used to determine the
97 * center line of the vehicle at this point in time. Otherwise, the average of the center positions of the lines will be
98 * taken.
99 * @param relativePosition the position on the vehicle relative to the reference point.
100 * @return the lanes and the position on the lanes where the GTU is currently registered, for the given position of the GTU.
101 * @throws GTUException when the vehicle is not on one of the lanes on which it is registered.
102 */
103 Map<Lane, Length> positions(RelativePosition relativePosition) throws GTUException;
104
105 /**
106 * Return the longitudinal positions of a point relative to this GTU, relative to the center line of the Lanes in which the
107 * vehicle is registered.
108 * @param relativePosition the position on the vehicle relative to the reference point.
109 * @param when the future time for which to calculate the positions.
110 * @return the lanes and the position on the lanes where the GTU will be registered at the time, for the given position of
111 * the GTU.
112 * @throws GTUException when the vehicle is not on one of the lanes on which it is registered.
113 */
114 Map<Lane, Length> positions(RelativePosition relativePosition, Time when) throws GTUException;
115
116 /**
117 * Return the longitudinal position of a point relative to this GTU, relative to the center line of the Lane at the current
118 * simulation time. <br>
119 * @param lane the position on this lane will be returned.
120 * @param relativePosition the position on the vehicle relative to the reference point.
121 * @return DoubleScalarAbs<LengthUnit>; the position, relative to the center line of the Lane.
122 * @throws GTUException when the vehicle is not on the given lane.
123 */
124 Length position(Lane lane, RelativePosition relativePosition) throws GTUException;
125
126 /**
127 * Return the longitudinal position of a point relative to this GTU, relative to the center line of the Lane.
128 * @param lane the position on this lane will be returned.
129 * @param relativePosition the position on the vehicle relative to the reference point.
130 * @param when the future time for which to calculate the positions.
131 * @return DoubleScalarAbs<LengthUnit>; the position, relative to the center line of the Lane.
132 * @throws GTUException when the vehicle is not on the given lane.
133 */
134 Length position(Lane lane, RelativePosition relativePosition, Time when) throws GTUException;
135
136 /**
137 * Return the longitudinal positions of a point relative to this GTU, relative to the center line of the Lanes in which the
138 * vehicle is registered, as fractions of the length of the lane. This is important when we want to see if two vehicles are
139 * next to each other and we compare an 'inner' and 'outer' curve.<br>
140 * @param relativePosition the position on the vehicle relative to the reference point.
141 * @return the lanes and the position on the lanes where the GTU is currently registered, for the given position of the GTU.
142 * @throws GTUException when the vehicle is not on one of the lanes on which it is registered.
143 */
144 Map<Lane, Double> fractionalPositions(RelativePosition relativePosition) throws GTUException;
145
146 /**
147 * Return the longitudinal positions of a point relative to this GTU, relative to the center line of the Lanes in which the
148 * vehicle is registered, as fractions of the length of the lane. This is important when we want to see if two vehicles are
149 * next to each other and we compare an 'inner' and 'outer' curve.
150 * @param relativePosition the position on the vehicle relative to the reference point.
151 * @param when the future time for which to calculate the positions.
152 * @return the lanes and the position on the lanes where the GTU will be registered at the time, for the given position of
153 * the GTU.
154 * @throws GTUException when the vehicle is not on one of the lanes on which it is registered.
155 */
156 Map<Lane, Double> fractionalPositions(RelativePosition relativePosition, Time when) throws GTUException;
157
158 /**
159 * Return the longitudinal position of a point relative to this GTU, relative to the center line of the Lane, as a fraction
160 * of the length of the lane. This is important when we want to see if two vehicles are next to each other and we compare an
161 * 'inner' and 'outer' curve.
162 * @param lane the position on this lane will be returned.
163 * @param relativePosition the position on the vehicle relative to the reference point.
164 * @param when the future time for which to calculate the positions.
165 * @return the fractional relative position on the lane at the given time.
166 * @throws GTUException when the vehicle is not on the given lane.
167 */
168 double fractionalPosition(Lane lane, RelativePosition relativePosition, Time when) throws GTUException;
169
170 /**
171 * Return the longitudinal position of a point relative to this GTU, relative to the center line of the Lane, as a fraction
172 * of the length of the lane. This is important when we want to see if two vehicles are next to each other and we compare an
173 * 'inner' and 'outer' curve.<br>
174 * @param lane the position on this lane will be returned.
175 * @param relativePosition the position on the vehicle relative to the reference point.
176 * @return the fractional relative position on the lane at the given time.
177 * @throws GTUException when the vehicle is not on the given lane.
178 */
179 double fractionalPosition(Lane lane, RelativePosition relativePosition) throws GTUException;
180
181 /**
182 * Return the longitudinal position that the indicated relative position of this GTU would have if it were to change to
183 * another Lane with a / the current CrossSectionLink. This point may be before the begin or after the end of the link of
184 * the projection lane of the GTU. This preserves the length of the GTU.
185 * @param projectionLane Lane; the lane onto which the position of this GTU must be projected
186 * @param relativePosition RelativePosition; the point on this GTU that must be projected
187 * @param when Time; the time for which to project the position of this GTU
188 * @return Length; the position of this GTU in the projectionLane
189 * @throws GTUException when projectionLane it not in any of the CrossSectionLink that the GTU is on
190 */
191 Length translatedPosition(Lane projectionLane, RelativePosition relativePosition, Time when) throws GTUException;
192
193 /**
194 * Return the longitudinal position on the projection lane that has the same fractional position on one of the current lanes
195 * of the indicated relative position. This preserves the fractional positions of all relative positions of the GTU.
196 * @param projectionLane Lane; the lane onto which the position of this GTU must be projected
197 * @param relativePosition RelativePosition; the point on this GTU that must be projected
198 * @param when Time; the time for which to project the position of this GTU
199 * @return Length; the position of this GTU in the projectionLane
200 * @throws GTUException when projectionLane it not in any of the CrossSectionLink that the GTU is on
201 */
202 Length projectedPosition(Lane projectionLane, RelativePosition relativePosition, Time when) throws GTUException;
203
204 /**
205 * Return the current Lane, position and directionality of the GTU.
206 * @return DirectedLanePosition; the current Lane, position and directionality of the GTU
207 * @throws GTUException in case the reference position of the GTU cannot be found on the lanes in its current path
208 */
209 DirectedLanePosition getReferencePosition() throws GTUException;
210
211 /**
212 * Return the directionality of a lane on which the GTU is registered for its current operational plan.
213 * @param lane Lane; the lane for which we want to know the direction
214 * @return GTUDirectionality; the direction on the given lane
215 * @throws GTUException in case the GTU is not registered on the Lane
216 */
217 GTUDirectionality getDirection(Lane lane) throws GTUException;
218
219 /**
220 * Add an event to the list of lane triggers scheduled for this GTU.
221 * @param lane Lane; the lane on which the event occurs
222 * @param event SimeEvent<SimTimeDoubleUnit> the event
223 */
224 void addTrigger(Lane lane, SimEventInterface<SimTimeDoubleUnit> event);
225
226 /**
227 * Set distance over which the GTU should not change lane after being created.
228 * @param distance distance over which the GTU should not change lane after being created
229 */
230 void setNoLaneChangeDistance(Length distance);
231
232 /**
233 * Returns whether a lane change is allowed.
234 * @return whether a lane change is allowed
235 */
236 boolean laneChangeAllowed();
237
238 /**
239 * This method returns the current desired speed of the GTU. This value is required often, so implementations can cache it.
240 * @return Speed; current desired speed
241 */
242 Speed getDesiredSpeed();
243
244 /**
245 * This method returns the current car-following acceleration of the GTU. This value is required often, so implementations
246 * can cache it.
247 * @return Acceleration; current car-following acceleration
248 */
249 Acceleration getCarFollowingAcceleration();
250
251 /**
252 * Returns the vehicle model.
253 * @return VehicleModel; vehicle model
254 */
255 default VehicleModel getVehicleModel()
256 {
257 return VehicleModel.MINMAX;
258 }
259
260 /**
261 * The default implementation returns {@code true} if the deceleration is larger than a speed-dependent threshold given
262 * by:<br>
263 * <br>
264 * c0 * g(v) + c1 + c3*v^2<br>
265 * <br>
266 * where c0 = 0.2, c1 = 0.15 and c3 = 0.00025 (with c2 = 0 implicit) are empirically derived averages, and g(v) is 0 below
267 * 25 km/h or 1 otherwise, representing that the engine is disengaged at low speeds.
268 * @return boolean; whether the braking lights are on
269 */
270 default boolean isBrakingLightsOn()
271 {
272 return isBrakingLightsOn(getSimulator().getSimulatorTime());
273 }
274
275 /**
276 * The default implementation returns {@code true} if the deceleration is larger than a speed-dependent threshold given
277 * by:<br>
278 * <br>
279 * c0 * g(v) + c1 + c3*v^2<br>
280 * <br>
281 * where c0 = 0.2, c1 = 0.15 and c3 = 0.00025 (with c2 = 0 implicit) are empirically derived averages, and g(v) is 0 below
282 * 25 km/h or 1 otherwise, representing that the engine is disengaged at low speeds.
283 * @param when Time; time
284 * @return boolean; whether the braking lights are on
285 */
286 default boolean isBrakingLightsOn(final Time when)
287 {
288 double v = getSpeed(when).si;
289 double a = getAcceleration(when).si;
290 return a < (v < 6.944 ? 0.0 : -0.2) - 0.15 * v - 0.00025 * v * v;
291 }
292
293 /** @return the status of the turn indicator */
294 TurnIndicatorStatus getTurnIndicatorStatus();
295
296 /**
297 * @param time Time; time to obtain the turn indicator status at
298 * @return the status of the turn indicator at the given time
299 */
300 TurnIndicatorStatus getTurnIndicatorStatus(Time time);
301
302 /**
303 * Set the status of the turn indicator.
304 * @param turnIndicatorStatus the new status of the turn indicator.
305 * @throws GTUException when GTUType does not have a turn indicator
306 */
307 void setTurnIndicatorStatus(TurnIndicatorStatus turnIndicatorStatus) throws GTUException;
308
309 /**
310 * The lane-based event type for pub/sub indicating the initialization of a new GTU. <br>
311 * Payload: [String gtuId, DirectedPoint initialPosition, Length length, Length width, Color gtuBaseColor, Lane
312 * referenceLane, Length positionOnReferenceLane, GTUDirectionality direction, GTUType gtuType]
313 */
314 EventType LANEBASED_INIT_EVENT = new EventType("LANEBASEDGTU.INIT");
315
316 /**
317 * The lane-based event type for pub/sub indicating a move. <br>
318 * Payload: [String gtuId, DirectedPoint position, Speed speed, Acceleration acceleration, TurnIndicatorStatus
319 * turnIndicatorStatus, Length odometer, Lane referenceLane, Length positionOnReferenceLane, GTUDirectionality direction]
320 */
321 EventType LANEBASED_MOVE_EVENT = new EventType("LANEBASEDGTU.MOVE");
322
323 /**
324 * The lane-based event type for pub/sub indicating destruction of the GTU. <br>
325 * Payload: [String gtuId, DirectedPoint lastPosition, Length odometer, Lane referenceLane, Length positionOnReferenceLane,
326 * GTUDirectionality direction]
327 */
328 EventType LANEBASED_DESTROY_EVENT = new EventType("LANEBASEDGTU.DESTROY");
329
330 /**
331 * The event type for pub/sub indicating that the GTU entered a new link (with the FRONT position if driving forward; REAR
332 * if driving backward). <br>
333 * Payload: [String gtuId, Link link]
334 */
335 EventType LINK_ENTER_EVENT = new EventType("LINK.ENTER");
336
337 /**
338 * The event type for pub/sub indicating that the GTU exited a link (with the REAR position if driving forward; FRONT if
339 * driving backward). <br>
340 * Payload: [String gtuId, Link link]
341 */
342 EventType LINK_EXIT_EVENT = new EventType("LINK.EXIT");
343
344 /**
345 * The event type for pub/sub indicating that the GTU entered a new lane (with the FRONT position if driving forward; REAR
346 * if driving backward). <br>
347 * Payload: [String gtuId, Lane lane]
348 */
349 EventType LANE_ENTER_EVENT = new EventType("LANE.ENTER");
350
351 /**
352 * The event type for pub/sub indicating that the GTU exited a lane (with the REAR position if driving forward; FRONT if
353 * driving backward). <br>
354 * Payload: [String gtuId, Lane lane]
355 */
356 EventType LANE_EXIT_EVENT = new EventType("LANE.EXIT");
357
358 /**
359 * The event type for pub/sub indicating that the GTU change lane. <br>
360 * Payload: [String gtuId, LateralDirectionality direction, DirectedLanePosition from]
361 */
362 EventType LANE_CHANGE_EVENT = new EventType("LANE.CHANGE");
363
364 }