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