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.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.
33   * <p>
34   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
35   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
36   * </p>
37   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
38   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
39   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
40   */
41  public class HeadwayGtuReal extends AbstractHeadway implements HeadwayGtu
42  {
43      /** */
44      private static final long serialVersionUID = 20170324L;
45  
46      /** Stored speed limit info of the observed GTU. */
47      private SpeedLimitInfo speedLimitInfo;
48  
49      /** Wrapped GTU. */
50      private final LaneBasedGtu gtu;
51  
52      /** Whether the GTU is facing the same direction. */
53      private final boolean facingSameDirection;
54  
55      /**
56       * Construct a new Headway information object, for a GTU ahead of us or behind us.
57       * @param gtu the observed GTU, can not be null.
58       * @param distance if this constructor is used, distance cannot be null.
59       * @param facingSameDirection whether the GTU is facing the same direction.
60       * @throws GtuException when id is null, objectType is null, or parameters are inconsistent
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       * Construct a new Headway information object, for a GTU parallel with us.
71       * @param gtu the observed GTU, can not be null.
72       * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
73       * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
74       * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
75       * @param facingSameDirection whether the GTU is facing the same direction.
76       * @throws GtuException when id is null, or parameters are inconsistent
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       * Creates speed limit prospect for given GTU.
88       * @param wrappedGtu gtu to the the speed limit prospect for
89       * @return speed limit prospect for given GTU
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      * {@inheritDoc} <br>
137      * <br>
138      * <b>Note: when moving a {@code HeadwayGtuRealDirect}, only headway, speed and acceleration may be considered to be delayed
139      * and anticipated. Other information is taken from the actual GTU at the time {@code moved()} is called.</b>
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             // input should be consistent
153             throw new RuntimeException("Exception while copying Headway GTU.", exception);
154         }
155     }
156 
157     /**
158      * Returns an array with GTU status.
159      * @return array with GTU status
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      * Returns the wrapped GTU.
189      * @return wrapped GTU.
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         // TODO
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         // TODO
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 }