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   * This class allows {@code PerceptionCategory}s that are either eager or lazy. All categories will have the {@code updateAll}
26   * method invoked prior to an operational plan being determined. Categories may ignore this and instead evaluate results only
27   * when the tactical planner requests them.
28   * <p>
29   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
33   * initial version Nov 15, 2015 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
37   */
38  public abstract class AbstractLanePerception extends AbstractPerception<LaneBasedGTU> implements LanePerception
39  {
40  
41      /** */
42      private static final long serialVersionUID = 20151128L;
43  
44      /** Perception parameter type. */
45      protected static final ParameterTypeLength PERCEPTION = ParameterTypes.PERCEPTION;
46  
47      /** Look ahead parameter type. */
48      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
49  
50      /** Look back parameter type. */
51      protected static final ParameterTypeLength LOOKBACK = ParameterTypes.LOOKBACK;
52  
53      /** Lane structure to perform the perception with. */
54      private LaneStructure laneStructure = null;
55  
56      /** Most recent update time of lane structure. */
57      private Time updateTime = null;
58  
59      /** Mental module. */
60      private Mental mental;
61  
62      /**
63       * Create a new LanePerception module without mental module.
64       * @param gtu LaneBasedGTU; GTU
65       */
66      public AbstractLanePerception(final LaneBasedGTU gtu)
67      {
68          super(gtu);
69          this.mental = null;
70      }
71  
72      /**
73       * Create a new LanePerception module with mental module.
74       * @param gtu LaneBasedGTU; GTU
75       * @param mental Mental; mental module
76       */
77      public AbstractLanePerception(final LaneBasedGTU gtu, final Mental mental)
78      {
79          super(gtu);
80          this.mental = mental;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final LaneStructure getLaneStructure() throws ParameterException
86      {
87  
88          if (this.laneStructure == null || this.updateTime.lt(getGtu().getSimulator().getSimulatorTime()))
89          {
90              if (this.laneStructure == null)
91              {
92                  // downstream structure length
93                  Length down = getGtu().getParameters().getParameter(PERCEPTION);
94                  // upstream structure length
95                  Length up = getGtu().getParameters().getParameter(LOOKBACK);
96                  // structure length downstream of split on link not on route
97                  Length lookAhead = getGtu().getParameters().getParameter(LOOKAHEAD);
98                  // structure length upstream of merge on link not on route
99                  Length upMerge = Length.max(up, lookAhead);
100                 // negative values for upstream
101                 up = up.neg();
102                 upMerge = upMerge.neg();
103                 this.laneStructure = new RollingLaneStructure(lookAhead, down, up, lookAhead, upMerge, getGtu());
104             }
105             DirectedLanePosition dlp;
106             try
107             {
108                 dlp = getGtu().getReferencePosition();
109                 this.laneStructure.update(dlp, getGtu().getStrategicalPlanner().getRoute(), getGtu().getGTUType());
110             }
111             catch (GTUException exception)
112             {
113                 exception.printStackTrace();
114                 throw new RuntimeException("Error while updating the lane map.", exception);
115             }
116             this.updateTime = getGtu().getSimulator().getSimulatorTime();
117         }
118         return this.laneStructure;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public Mental getMental()
124     {
125         return this.mental;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public void perceive() throws GTUException, NetworkException, ParameterException
131     {
132         if (this.mental != null)
133         {
134             this.mental.apply(this);
135         }
136         super.perceive();
137     }
138 
139 }