View Javadoc
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.route.Route;
12  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
13  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14  
15  /**
16   * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
17   * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
18   * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
19   * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
20   * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
21   * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
22   * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
23   * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
24   * 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
25   * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
26   * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
27   * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
28   * positions. See examples in
29   * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
30   * <p>
31   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
33   * <p>
34   * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
35   *          initial version 11 feb. 2015 <br>
36   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
37   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
38   */
39  public abstract class AbstractHeadwayGTU extends AbstractHeadway
40  {
41      /** */
42      private static final long serialVersionUID = 20160410L;
43  
44      /** The perceived GTU Type, or null if unknown. */
45      private final GTUType gtuType;
46  
47      /** Whether the GTU is facing the same direction. */
48      private final boolean facingSameDirection;
49  
50      /** Observable characteristics of a GTU. */
51      public enum GTUStatus
52      {
53          /** Braking lights are on when observing the headway. */
54          BRAKING_LIGHTS,
55  
56          /** Left turn indicator was on when observing the headway. */
57          LEFT_TURNINDICATOR,
58  
59          /** Right turn indicator was on when observing the headway. */
60          RIGHT_TURNINDICATOR,
61  
62          /** Alarm lights are on. */
63          EMERGENCY_LIGHTS,
64  
65          /** GTU was honking (car) or ringing a bell (cyclist) when observing the headway. */
66          HONK;
67      }
68  
69      /** The observable characteristics of the GTU. */
70      private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
71  
72      /**
73       * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
74       * @param id the id of the GTU for comparison purposes, can not be null.
75       * @param gtuType the perceived GTU Type, or null if unknown.
76       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
77       * @param facingSameDirection whether the GTU is facing the same direction.
78       * @param length the (perceived) length of the other object; can not be null.
79       * @param speed the (perceived) speed of the other object; can be null if unknown.
80       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
81       * @param gtuStatus the observable characteristics of the GTU.
82       * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
83       */
84      @SuppressWarnings("checkstyle:parameternumber")
85      public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
86              final Length length, final Speed speed, final Acceleration acceleration, final GTUStatus... gtuStatus)
87              throws GTUException
88      {
89          super(ObjectType.GTU, id, distance, length, speed, acceleration);
90          this.facingSameDirection = facingSameDirection;
91          this.gtuType = gtuType;
92          for (GTUStatus status : gtuStatus)
93          {
94              this.gtuStatus.add(status);
95          }
96      }
97  
98      /**
99       * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
100      * @param id String; the id of the GTU for comparison purposes, can not be null.
101      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
102      * @param distance Length; the distance to the other GTU; if this constructor is used, distance cannot be null.
103      * @param facingSameDirection whether the GTU is facing the same direction.
104      * @param length the (perceived) length of the other object; can not be null.
105      * @param gtuStatus the observable characteristics of the GTU.
106      * @throws GTUException when id is null, or parameters are inconsistent
107      */
108     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
109             final Length length, final GTUStatus... gtuStatus) throws GTUException
110     {
111         super(ObjectType.GTU, id, distance, length);
112         this.facingSameDirection = facingSameDirection;
113         this.gtuType = gtuType;
114         for (GTUStatus status : gtuStatus)
115         {
116             this.gtuStatus.add(status);
117         }
118     }
119 
120     /**
121      * Construct a new Headway information object, for a moving GTU parallel with us.
122      * @param id the id of the GTU for comparison purposes, can not be null.
123      * @param gtuType the perceived GTU Type, or null if unknown.
124      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
125      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
126      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
127      * @param facingSameDirection whether the GTU is facing the same direction.
128      * @param length the (perceived) length of the other object; can not be null.
129      * @param speed the (perceived) speed of the other GTU; can be null if unknown.
130      * @param acceleration the (perceived) acceleration of the other GTU; can be null if unknown.
131      * @param gtuStatus the observable characteristics of the GTU.
132      * @throws GTUException when id is null, or parameters are inconsistent
133      */
134     @SuppressWarnings("checkstyle:parameternumber")
135     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
136             final Length overlapRear, final boolean facingSameDirection, final Length length, final Speed speed,
137             final Acceleration acceleration, final GTUStatus... gtuStatus) throws GTUException
138     {
139         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
140         this.facingSameDirection = facingSameDirection;
141         this.gtuType = gtuType;
142         for (GTUStatus status : gtuStatus)
143         {
144             this.gtuStatus.add(status);
145         }
146     }
147 
148     /**
149      * Construct a new Headway information object, for a non-moving GTU parallel with us.
150      * @param id the id of the GTU for comparison purposes, can not be null.
151      * @param gtuType the perceived GTU Type, or null if unknown.
152      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
153      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
154      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
155      * @param facingSameDirection whether the GTU is facing the same direction.
156      * @param length the (perceived) length of the other object; can not be null.
157      * @param gtuStatus the observable characteristics of the GTU.
158      * @throws GTUException when id is null, or parameters are inconsistent
159      */
160     @SuppressWarnings("checkstyle:parameternumber")
161     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
162             final Length overlapRear, final boolean facingSameDirection, final Length length, final GTUStatus... gtuStatus)
163             throws GTUException
164     {
165         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
166         this.facingSameDirection = facingSameDirection;
167         this.gtuType = gtuType;
168         for (GTUStatus status : gtuStatus)
169         {
170             this.gtuStatus.add(status);
171         }
172     }
173 
174     /**
175      * @return gtuType
176      */
177     public final GTUType getGtuType()
178     {
179         return this.gtuType;
180     }
181 
182     /**
183      * @return facingSameDirection
184      */
185     public final boolean isFacingSameDirection()
186     {
187         return this.facingSameDirection;
188     }
189 
190     /** @return were the braking lights on? */
191     public final boolean isBrakingLightsOn()
192     {
193         return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
194     }
195 
196     /** @return was the left turn indicator on? */
197     public final boolean isLeftTurnIndicatorOn()
198     {
199         return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
200     }
201 
202     /** @return was the right turn indicator on? */
203     public final boolean isRightTurnIndicatorOn()
204     {
205         return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
206     }
207 
208     /** @return were the emergency lights on? */
209     public final boolean isEmergencyLightsOn()
210     {
211         return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
212     }
213 
214     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
215     public final boolean isHonking()
216     {
217         return this.gtuStatus.contains(GTUStatus.HONK);
218     }
219 
220     /**
221      * Many models that observe a GTU need to predict the imminent behavior of that GTU. Having a car following model of the
222      * observed GTU can help with that. The car following model that is returned can be on a continuum between the actual car
223      * following model of the observed GTU and the own car following model of the observing GTU, not making any assumptions
224      * about the observed GTU. When successive observations of the GTU take place, parameters about its behavior can be
225      * estimated more accurately. Another interesting easy-to-implement solution is to return a car following model per GTU
226      * type, where the following model of a truck can differ from that of a car.
227      * @return a car following model that represents the expected behavior of the observed GTU
228      */
229     public abstract CarFollowingModel getCarFollowingModel();
230 
231     /**
232      * Many models that observe a GTU need to predict the imminent behavior of that GTU. Having an estimate of the behavioral
233      * characteristics of the observed GTU can help with that. The behavioral characteristics that are returned can be on a
234      * continuum between the actual behavioral characteristics of the observed GTU and the own behavioral characteristics of the
235      * observing GTU, not making any assumptions about the observed GTU. When successive observations of the GTU take place,
236      * parameters about its behavior can be estimated more accurately. Another interesting easy-to-implement solution is to
237      * return a set of behavioral characteristics per GTU type, where the behavioral characteristics of a truck can differ from
238      * that of a car.
239      * @return the behavioral characteristics that represent the expected behavior of the observed GTU
240      */
241     public abstract BehavioralCharacteristics getBehavioralCharacteristics();
242 
243     /**
244      * Many models that observe a GTU need to predict the imminent behavior of that GTU. Having a model of the speed info
245      * profile for the observed GTU can help with predicting its future behavior. The speed limit info that is returned can be
246      * on a continuum between the actual speed limit model of the observed GTU and the own speed limit model of the observing
247      * GTU, not making any assumptions about the observed GTU. When successive observations of the GTU take place, parameters
248      * about its behavior, such as the maximum speed it accepts, can be estimated more accurately. Another interesting
249      * easy-to-implement solution is to return a speed limit info object per GTU type, where the returned information of a truck
250      * -- with a maximum allowed speed on 80 km/h -- can differ from that of a car -- which can have a maximum allowed speed of
251      * 100 km/h on the same road.
252      * @return a speed limit model that helps in determining the expected behavior of the observed GTU
253      */
254     public abstract SpeedLimitInfo getSpeedLimitInfo();
255 
256     /**
257      * Models responding to other GTU may assume a route of the vehicle, for instance at intersections. The route may be short,
258      * i.e. only over the next intersection. Implementations may return anything from the actual route, a route based on
259      * indicators and other assumptions, or {@code null} if simply not known/estimated.
260      * @return route of gtu
261      */
262     public abstract Route getRoute();
263 
264     /** {@inheritDoc} */
265     @Override
266     @SuppressWarnings("checkstyle:designforextension")
267     public String toString()
268     {
269         return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
270                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
271                 + "]";
272     }
273 
274 }