View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2   
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.network.NetworkException;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
9   import org.opentrafficsim.road.gtu.lane.control.CACC;
10  import org.opentrafficsim.road.gtu.lane.control.ControlTacticalPlanner;
11  import org.opentrafficsim.road.gtu.lane.perception.DownstreamNeighborsIterable;
12  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
13  import org.opentrafficsim.road.gtu.lane.perception.LaneRecord;
14  import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
15  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
16  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
17  import org.opentrafficsim.road.gtu.lane.perception.categories.LaneBasedAbstractPerceptionCategory;
18  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
19  
20  /**
21   * CACC perception.
22   * <p>
23   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 12, 2019 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public class CaccPerception extends LaneBasedAbstractPerceptionCategory implements LongitudinalControllerPerception
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20190312L;
36  
37      /** Onboard sensors in the form of a headway GTU type. */
38      private final HeadwayGtuType sensors;
39  
40      /**
41       * Constructor using default sensors with zero delay.
42       * @param perception LanePerception; perception
43       */
44      public CaccPerception(final LanePerception perception)
45      {
46          this(perception, new DefaultCaccSensors());
47      }
48  
49      /**
50       * Constructor using specified sensors.
51       * @param perception LanePerception; perception
52       * @param sensors HeadwayGtuType; onboard sensor information
53       */
54      public CaccPerception(final LanePerception perception, final HeadwayGtuType sensors)
55      {
56          super(perception);
57          this.sensors = sensors;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public void updateAll() throws GTUException, NetworkException, ParameterException
63      {
64          // lazy evaluation
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public PerceptionCollectable<HeadwayGTU, LaneBasedGTU> getLeaders()
70      {
71          return computeIfAbsent("leaders", () -> computeLeaders());
72      }
73  
74      /**
75       * Computes leaders.
76       * @return perception iterable for leaders
77       */
78      private PerceptionCollectable<HeadwayGTU, LaneBasedGTU> computeLeaders()
79      {
80          try
81          {
82              LaneStructureRecord record = getPerception().getLaneStructure().getRootRecord();
83              Length pos = record.getStartDistance().neg();
84              pos = record.getDirection().isPlus() ? pos.plus(getGtu().getFront().getDx())
85                      : pos.minus(getGtu().getFront().getDx());
86              boolean ignoreIfUpstream = true;
87              return new DownstreamNeighboursIterableCACC(getGtu(), record, Length.max(Length.ZERO, pos),
88                      ((ControlTacticalPlanner) getGtu().getTacticalPlanner()).getSettings()
89                              .getParameter(LongitudinalControllerPerception.RANGE),
90                      getGtu().getFront(), this.sensors, RelativeLane.CURRENT, ignoreIfUpstream);
91          }
92          catch (ParameterException | GTUException exception)
93          {
94              throw new RuntimeException("Unexpected exception while computing gtu alongside.", exception);
95          }
96      }
97  
98      /**
99       * Extends the regular {@code DownstreamNeighborsIterable} class with skipping leaders that are not sensed by the system.
100      * <p>
101      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
102      * <br>
103      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
104      * <p>
105      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 13, 2019 <br>
106      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
107      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
108      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
109      */
110     private static class DownstreamNeighboursIterableCACC extends DownstreamNeighborsIterable
111     {
112 
113         /**
114          * Constructor.
115          * @param perceivingGtu LaneBasedGTU; perceiving GTU
116          * @param root LaneRecord&lt;?&gt;; root record
117          * @param initialPosition Length; position on the root record
118          * @param maxDistance Length; maximum distance to search
119          * @param relativePosition RelativePosition; position to which distance are calculated by subclasses
120          * @param headwayGtuType HeadwayGtuType; type of HeadwayGTU to return
121          * @param lane RelativeLane; relative lane (used for a left/right distinction to prevent dead-locks)
122          * @param ignoreIfUpstream boolean; whether to ignore GTU that are partially upstream of a record
123          */
124         @SuppressWarnings("checkstyle:parameternumber")
125         DownstreamNeighboursIterableCACC(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root,
126                 final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
127                 final HeadwayGtuType headwayGtuType, final RelativeLane lane, final boolean ignoreIfUpstream)
128         {
129             super(perceivingGtu, root, initialPosition, maxDistance, relativePosition, headwayGtuType, lane, ignoreIfUpstream);
130         }
131 
132         /** {@inheritDoc} */
133         @Override
134         protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
135         {
136             Entry next;
137             do
138             {
139                 next = super.getNext(record, position, counter);
140             }
141             // skip leaders that are not the direct leader and that are not CACC
142             while (next != null && first() != null && !(next.getObject().getTacticalPlanner() instanceof CACC));
143             return next;
144         }
145 
146     }
147 
148 }