CaccPerception.java

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

  2. import java.util.Iterator;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.opentrafficsim.base.parameters.ParameterException;
  5. import org.opentrafficsim.core.gtu.GtuException;
  6. import org.opentrafficsim.core.gtu.RelativePosition;
  7. import org.opentrafficsim.core.gtu.perception.AbstractPerceptionCategory;
  8. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  9. import org.opentrafficsim.road.gtu.lane.control.Cacc;
  10. import org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable;
  11. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  12. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
  13. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  14. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
  15. import org.opentrafficsim.road.gtu.lane.perception.structure.NavigatingIterable.Entry;

  16. /**
  17.  * CACC perception.
  18.  * <p>
  19.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  21.  * </p>
  22.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  23.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  24.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  25.  */
  26. public class CaccPerception extends AbstractPerceptionCategory<LaneBasedGtu, LanePerception>
  27.         implements LongitudinalControllerPerception
  28. {

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

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

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

  41.     /**
  42.      * Constructor using specified sensors.
  43.      * @param perception perception
  44.      * @param sensors onboard sensor information
  45.      */
  46.     public CaccPerception(final LanePerception perception, final HeadwayGtuType sensors)
  47.     {
  48.         super(perception);
  49.         this.sensors = sensors;
  50.     }

  51.     @Override
  52.     public PerceptionCollectable<HeadwayGtu, LaneBasedGtu> getLeaders()
  53.     {
  54.         return computeIfAbsent("leaders", () -> computeLeaders());
  55.     }

  56.     /**
  57.      * Computes leaders.
  58.      * @return perception iterable for leaders
  59.      */
  60.     private PerceptionCollectable<HeadwayGtu, LaneBasedGtu> computeLeaders()
  61.     {
  62.         try
  63.         {
  64.             Iterator<Entry<LaneBasedGtu>> leaders = getPerception().getLaneStructure().getDownstreamGtus(RelativeLane.CURRENT,
  65.                     RelativePosition.FRONT, RelativePosition.REAR, RelativePosition.FRONT, RelativePosition.REAR).iterator();
  66.             return new AbstractPerceptionReiterable<LaneBasedGtu, HeadwayGtu, LaneBasedGtu>(getGtu())
  67.             {
  68.                 @Override
  69.                 protected Iterator<AbstractPerceptionReiterable<LaneBasedGtu, HeadwayGtu,
  70.                         LaneBasedGtu>.PrimaryIteratorEntry> primaryIterator()
  71.                 {
  72.                     return new Iterator<>()
  73.                     {
  74.                         /** Next entry which is the first vehicle, or a further CACC vehicle (defined by tactical planner). */
  75.                         private PrimaryIteratorEntry next;

  76.                         /** To include the first vehicle always. */
  77.                         private boolean first = true;

  78.                         @Override
  79.                         public boolean hasNext()
  80.                         {
  81.                             while (leaders.hasNext() && this.next == null)
  82.                             {
  83.                                 Entry<LaneBasedGtu> entry = leaders.next();
  84.                                 if (this.first || entry.object().getTacticalPlanner() instanceof Cacc)
  85.                                 {
  86.                                     this.next = new PrimaryIteratorEntry(entry.object(), entry.distance());
  87.                                 }
  88.                                 this.first = false;
  89.                             }
  90.                             return this.next != null;
  91.                         }

  92.                         @Override
  93.                         public AbstractPerceptionReiterable<LaneBasedGtu, HeadwayGtu, LaneBasedGtu>.PrimaryIteratorEntry next()
  94.                         {
  95.                             hasNext();
  96.                             return this.next;
  97.                         }
  98.                     };
  99.                 }

  100.                 @Override
  101.                 protected HeadwayGtu perceive(final LaneBasedGtu object, final Length distance)
  102.                         throws GtuException, ParameterException
  103.                 {
  104.                     return CaccPerception.this.sensors.createDownstreamGtu(getObject(), object, distance);
  105.                 }
  106.             };
  107.         }
  108.         catch (ParameterException exception)
  109.         {
  110.             throw new RuntimeException("Unexpected exception while computing gtu alongside.", exception);
  111.         }
  112.     }

  113. }