AbstractLanePerception.java

  1. package org.opentrafficsim.road.gtu.lane.perception;

  2. import org.djunits.value.vdouble.scalar.Length;
  3. import org.djunits.value.vdouble.scalar.Time;
  4. import org.opentrafficsim.base.parameters.ParameterException;
  5. import org.opentrafficsim.base.parameters.ParameterTypeLength;
  6. import org.opentrafficsim.base.parameters.ParameterTypes;
  7. import org.opentrafficsim.core.gtu.GTUException;
  8. import org.opentrafficsim.core.gtu.perception.AbstractPerception;
  9. import org.opentrafficsim.core.network.NetworkException;
  10. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  11. import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
  12. import org.opentrafficsim.road.network.lane.DirectedLanePosition;

  13. /**
  14.  * The perception module of a GTU based on lanes. It is responsible for perceiving (sensing) the environment of the GTU, which
  15.  * includes the locations of other GTUs. Perception is done at a certain time, and the perceived information might have a
  16.  * limited validity. In that sense, Perception is stateful. Information can be requested as often as needed, but will only be
  17.  * recalculated when asked explicitly. This abstract class provides the building blocks for lane-based perception. <br>
  18.  * Perception for lane-based GTUs involves information about GTUs in front of the owner GTU on the same lane (the 'leader' GTU),
  19.  * parallel vehicles (important if we want to change lanes), distance to other vehicles on parallel lanes, as well in front as
  20.  * to the back (important if we want to change lanes), and information about obstacles, traffic lights, speed signs, and ending
  21.  * lanes.
  22.  * <p>
  23.  * This class allows {@code PerceptionCategory}s that are either eager or lazy. All categories will have the {@code updateAll}
  24.  * method invoked prior to an operational plan being determined. Categories may ignore this and instead evaluate results only
  25.  * when the tactical planner requests them.
  26.  * <p>
  27.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  28.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  29.  * </p>
  30.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  31.  * initial version Nov 15, 2015 <br>
  32.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  33.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  34.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  35.  */
  36. public abstract class AbstractLanePerception extends AbstractPerception<LaneBasedGTU> implements LanePerception
  37. {

  38.     /** */
  39.     private static final long serialVersionUID = 20151128L;

  40.     /** Perception parameter type. */
  41.     protected static final ParameterTypeLength PERCEPTION = ParameterTypes.PERCEPTION;

  42.     /** Look ahead parameter type. */
  43.     protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;

  44.     /** Look back parameter type. */
  45.     protected static final ParameterTypeLength LOOKBACK = ParameterTypes.LOOKBACK;

  46.     /** Lane structure to perform the perception with. */
  47.     private LaneStructure laneStructure = null;

  48.     /** Most recent update time of lane structure. */
  49.     private Time updateTime = null;

  50.     /** Mental module. */
  51.     private Mental mental;

  52.     /**
  53.      * Create a new LanePerception module without mental module.
  54.      * @param gtu LaneBasedGTU; GTU
  55.      */
  56.     public AbstractLanePerception(final LaneBasedGTU gtu)
  57.     {
  58.         super(gtu);
  59.         this.mental = null;
  60.     }

  61.     /**
  62.      * Create a new LanePerception module with mental module.
  63.      * @param gtu LaneBasedGTU; GTU
  64.      * @param mental Mental; mental module
  65.      */
  66.     public AbstractLanePerception(final LaneBasedGTU gtu, final Mental mental)
  67.     {
  68.         super(gtu);
  69.         this.mental = mental;
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public final LaneStructure getLaneStructure() throws ParameterException
  74.     {

  75.         if (this.laneStructure == null || this.updateTime.lt(getGtu().getSimulator().getSimulatorTime()))
  76.         {
  77.             if (this.laneStructure == null)
  78.             {
  79.                 // downstream structure length
  80.                 Length down = getGtu().getParameters().getParameter(PERCEPTION);
  81.                 // upstream structure length
  82.                 Length up = getGtu().getParameters().getParameter(LOOKBACK);
  83.                 // structure length downstream of split on link not on route
  84.                 Length lookAhead = getGtu().getParameters().getParameter(LOOKAHEAD);
  85.                 // structure length upstream of merge on link not on route
  86.                 Length upMerge = Length.max(up, lookAhead);
  87.                 // negative values for upstream
  88.                 up = up.neg();
  89.                 upMerge = upMerge.neg();
  90.                 this.laneStructure = new RollingLaneStructure(lookAhead, down, up, lookAhead, upMerge, getGtu());
  91.             }
  92.             DirectedLanePosition dlp;
  93.             try
  94.             {
  95.                 dlp = getGtu().getReferencePosition();
  96.                 this.laneStructure.update(dlp, getGtu().getStrategicalPlanner().getRoute(), getGtu().getGTUType());
  97.             }
  98.             catch (GTUException exception)
  99.             {
  100.                 exception.printStackTrace();
  101.                 throw new RuntimeException("Error while updating the lane map.", exception);
  102.             }
  103.             this.updateTime = getGtu().getSimulator().getSimulatorTime();
  104.         }
  105.         return this.laneStructure;
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public Mental getMental()
  110.     {
  111.         return this.mental;
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public void perceive() throws GTUException, NetworkException, ParameterException
  116.     {
  117.         if (this.mental != null)
  118.         {
  119.             this.mental.apply(this);
  120.         }
  121.         super.perceive();
  122.     }

  123. }