View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djunits.value.vdouble.scalar.Time;
5   import org.opentrafficsim.base.parameters.ParameterException;
6   import org.opentrafficsim.base.parameters.ParameterTypeLength;
7   import org.opentrafficsim.base.parameters.ParameterTypes;
8   import org.opentrafficsim.core.gtu.GTUException;
9   import org.opentrafficsim.core.gtu.perception.AbstractPerception;
10  import org.opentrafficsim.core.network.NetworkException;
11  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
12  import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
13  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
14  
15  /**
16   * The perception module of a GTU based on lanes. It is responsible for perceiving (sensing) the environment of the GTU, which
17   * includes the locations of other GTUs. Perception is done at a certain time, and the perceived information might have a
18   * limited validity. In that sense, Perception is stateful. Information can be requested as often as needed, but will only be
19   * recalculated when asked explicitly. This abstract class provides the building blocks for lane-based perception. <br>
20   * Perception for lane-based GTUs involves information about GTUs in front of the owner GTU on the same lane (the 'leader' GTU),
21   * parallel vehicles (important if we want to change lanes), distance to other vehicles on parallel lanes, as well in front as
22   * to the back (important if we want to change lanes), and information about obstacles, traffic lights, speed signs, and ending
23   * lanes.
24   * <p>
25   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
29   * initial version Nov 15, 2015 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33   */
34  public abstract class AbstractLanePerception extends AbstractPerception<LaneBasedGTU> implements LanePerception
35  {
36  
37      /** */
38      private static final long serialVersionUID = 20151128L;
39  
40      /** Perception parameter type. */
41      protected static final ParameterTypeLength PERCEPTION = ParameterTypes.PERCEPTION;
42  
43      /** Look ahead parameter type. */
44      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
45  
46      /** Look back parameter type. */
47      protected static final ParameterTypeLength LOOKBACK = ParameterTypes.LOOKBACK;
48  
49      /** Lane structure to perform the perception with. */
50      private LaneStructure laneStructure = null;
51  
52      /** Most recent update time of lane structure. */
53      private Time updateTime = null;
54  
55      /** Mental module. */
56      private Mental mental;
57  
58      /**
59       * Create a new LanePerception module without mental module.
60       * @param gtu GTU
61       */
62      public AbstractLanePerception(final LaneBasedGTU gtu)
63      {
64          super(gtu);
65          this.mental = null;
66      }
67  
68      /**
69       * Create a new LanePerception module with mental module.
70       * @param gtu GTU
71       * @param mental Mental; mental module
72       */
73      public AbstractLanePerception(final LaneBasedGTU gtu, final Mental mental)
74      {
75          super(gtu);
76          this.mental = mental;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final LaneStructure getLaneStructure() throws ParameterException
82      {
83  
84          if (this.laneStructure == null || this.updateTime.lt(getGtu().getSimulator().getSimulatorTime()))
85          {
86              if (this.laneStructure == null)
87              {
88                  // downstream structure length
89                  Length down = getGtu().getParameters().getParameter(PERCEPTION);
90                  // upstream structure length
91                  Length up = getGtu().getParameters().getParameter(LOOKBACK);
92                  // structure length downstream of split on link not on route
93                  Length lookAhead = getGtu().getParameters().getParameter(LOOKAHEAD);
94                  // structure length upstream of merge on link not on route
95                  Length upMerge = Length.max(up, lookAhead);
96                  // negative values for upstream
97                  up = up.neg();
98                  upMerge = upMerge.neg();
99                  this.laneStructure = new RollingLaneStructure(lookAhead, down, up, lookAhead, upMerge, getGtu());
100             }
101             DirectedLanePosition dlp;
102             try
103             {
104                 dlp = getGtu().getReferencePosition();
105                 this.laneStructure.update(dlp, getGtu().getStrategicalPlanner().getRoute(), getGtu().getGTUType());
106             }
107             catch (GTUException exception)
108             {
109                 exception.printStackTrace();
110                 throw new RuntimeException("Error while updating the lane map.", exception);
111             }
112             this.updateTime = getGtu().getSimulator().getSimulatorTime();
113         }
114         return this.laneStructure;
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public Mental getMental()
120     {
121         return this.mental;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public void perceive() throws GTUException, NetworkException, ParameterException
127     {
128         if (this.mental != null)
129         {
130             this.mental.apply(this);
131         }
132         super.perceive();
133     }
134 
135 }