AccPerception.java

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

  2. import org.djunits.value.vdouble.scalar.Length;
  3. import org.opentrafficsim.base.parameters.ParameterException;
  4. import org.opentrafficsim.core.gtu.GTUException;
  5. import org.opentrafficsim.core.gtu.RelativePosition;
  6. import org.opentrafficsim.core.network.NetworkException;
  7. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  8. import org.opentrafficsim.road.gtu.lane.control.ControlTacticalPlanner;
  9. import org.opentrafficsim.road.gtu.lane.perception.DownstreamNeighborsIterable;
  10. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  11. import org.opentrafficsim.road.gtu.lane.perception.LaneRecord;
  12. import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
  13. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
  14. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  15. import org.opentrafficsim.road.gtu.lane.perception.categories.LaneBasedAbstractPerceptionCategory;
  16. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;

  17. /**
  18.  * ACC perception.
  19.  * <p>
  20.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  21.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  22.  * <p>
  23.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 12, 2019 <br>
  24.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  25.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  26.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  27.  */
  28. public class AccPerception extends LaneBasedAbstractPerceptionCategory implements LongitudinalControllerPerception
  29. {

  30.     /** */
  31.     private static final long serialVersionUID = 20190312L;

  32.     /** Onboard sensors in the form of a headway GTU type. */
  33.     private final HeadwayGtuType sensors;

  34.     /**
  35.      * Constructor using default sensors with zero delay.
  36.      * @param perception LanePerception; perception
  37.      */
  38.     public AccPerception(final LanePerception perception)
  39.     {
  40.         this(perception, new DefaultCaccSensors());
  41.     }

  42.     /**
  43.      * Constructor using specified sensors.
  44.      * @param perception LanePerception; perception
  45.      * @param sensors HeadwayGtuType; headway GTU type that defines the onboard sensor information
  46.      */
  47.     public AccPerception(final LanePerception perception, final HeadwayGtuType sensors)
  48.     {
  49.         super(perception);
  50.         this.sensors = sensors;
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public void updateAll() throws GTUException, NetworkException, ParameterException
  55.     {
  56.         // lazy evaluation
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public PerceptionCollectable<HeadwayGTU, LaneBasedGTU> getLeaders()
  61.     {
  62.         return computeIfAbsent("leaders", () -> computeLeaders());
  63.     }

  64.     /**
  65.      * Computes leaders.
  66.      * @return perception iterable for leaders
  67.      */
  68.     private PerceptionCollectable<HeadwayGTU, LaneBasedGTU> computeLeaders()
  69.     {
  70.         try
  71.         {
  72.             LaneStructureRecord record = getPerception().getLaneStructure().getRootRecord();
  73.             Length pos = record.getStartDistance().neg();
  74.             pos = record.getDirection().isPlus() ? pos.plus(getGtu().getFront().getDx())
  75.                     : pos.minus(getGtu().getFront().getDx());
  76.             boolean ignoreIfUpstream = true;
  77.             return new DownstreamNeighboursIterableACC(getGtu(), record, Length.max(Length.ZERO, pos),
  78.                     ((ControlTacticalPlanner) getGtu().getTacticalPlanner()).getSettings()
  79.                             .getParameter(LongitudinalControllerPerception.RANGE),
  80.                     getGtu().getFront(), this.sensors, RelativeLane.CURRENT, ignoreIfUpstream);
  81.         }
  82.         catch (ParameterException | GTUException exception)
  83.         {
  84.             throw new RuntimeException("Unexpected exception while computing gtu alongside.", exception);
  85.         }
  86.     }

  87.     /**
  88.      * Extends the regular {@code DownstreamNeighborsIterable} class with skipping leaders that are not sensed by the system.
  89.      * <p>
  90.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  91.      * <br>
  92.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  93.      * <p>
  94.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 13, 2019 <br>
  95.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  96.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  97.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  98.      */
  99.     private static class DownstreamNeighboursIterableACC extends DownstreamNeighborsIterable
  100.     {

  101.         /**
  102.          * Constructor.
  103.          * @param perceivingGtu LaneBasedGTU; perceiving GTU
  104.          * @param root LaneRecord&lt;?&gt;; root record
  105.          * @param initialPosition Length; position on the root record
  106.          * @param maxDistance Length; maximum distance to search
  107.          * @param relativePosition RelativePosition; position to which distance are calculated by subclasses
  108.          * @param headwayGtuType HeadwayGtuType; type of HeadwayGTU to return
  109.          * @param lane RelativeLane; relative lane (used for a left/right distinction to prevent dead-locks)
  110.          * @param ignoreIfUpstream boolean; whether to ignore GTU that are partially upstream of a record
  111.          */
  112.         @SuppressWarnings("checkstyle:parameternumber")
  113.         DownstreamNeighboursIterableACC(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root,
  114.                 final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
  115.                 final HeadwayGtuType headwayGtuType, final RelativeLane lane, final boolean ignoreIfUpstream)
  116.         {
  117.             super(perceivingGtu, root, initialPosition, maxDistance, relativePosition, headwayGtuType, lane, ignoreIfUpstream);
  118.         }

  119.         /** {@inheritDoc} */
  120.         @Override
  121.         protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
  122.         {
  123.             Entry next;
  124.             do
  125.             {
  126.                 next = super.getNext(record, position, counter);
  127.             }
  128.             // skip leaders that are not the direct leader and that are not CACC
  129.             while (next != null && first() != null);
  130.             return next;
  131.         }

  132.     }

  133. }