View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.djunits.value.vdouble.scalar.Acceleration;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djunits.value.vdouble.scalar.Speed;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.base.parameters.ParameterException;
11  import org.opentrafficsim.core.gtu.GTUException;
12  import org.opentrafficsim.core.gtu.GTUType;
13  import org.opentrafficsim.core.gtu.RelativePosition;
14  import org.opentrafficsim.core.network.NetworkException;
15  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16  import org.opentrafficsim.road.gtu.lane.control.CACC;
17  import org.opentrafficsim.road.gtu.lane.perception.DownstreamNeighborsIterable;
18  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
19  import org.opentrafficsim.road.gtu.lane.perception.LaneRecord;
20  import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
21  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
22  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
23  import org.opentrafficsim.road.gtu.lane.perception.categories.LaneBasedAbstractPerceptionCategory;
24  import org.opentrafficsim.road.gtu.lane.perception.headway.GTUStatus;
25  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
26  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUSimple;
27  
28  /**
29   * ACC perception.
30   * <p>
31   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
33   * <p>
34   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 12, 2019 <br>
35   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
36   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
37   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
38   */
39  public class AccPerception extends LaneBasedAbstractPerceptionCategory implements LongitudinalControllerPerception
40  {
41  
42      /** */
43      private static final long serialVersionUID = 20190312L;
44  
45      /** Onboard sensors in the form of a headway GTU type. */
46      private final HeadwayGtuType sensors;
47  
48      /**
49       * Constructor using default sensors with zero delay.
50       * @param perception LanePerception; perception
51       */
52      public AccPerception(final LanePerception perception)
53      {
54          this(perception, new OnboardSensors());
55      }
56  
57      /**
58       * Constructor using specified sensors.
59       * @param perception LanePerception; perception
60       * @param sensors HeadwayGtuType; headway GTU type that defines the onboard sensor information
61       */
62      public AccPerception(final LanePerception perception, final HeadwayGtuType sensors)
63      {
64          super(perception);
65          this.sensors = sensors;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void updateAll() throws GTUException, NetworkException, ParameterException
71      {
72          // lazy evaluation
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public PerceptionCollectable<HeadwayGTU, LaneBasedGTU> getLeaders()
78      {
79          return computeIfAbsent("leaders", () -> computeLeaders());
80      }
81  
82      /**
83       * Computes leaders.
84       * @return perception iterable for leaders
85       */
86      private PerceptionCollectable<HeadwayGTU, LaneBasedGTU> computeLeaders()
87      {
88          try
89          {
90              LaneStructureRecord record = getPerception().getLaneStructure().getRootRecord();
91              Length pos = record.getStartDistance().neg();
92              pos = record.getDirection().isPlus() ? pos.plus(getGtu().getFront().getDx())
93                      : pos.minus(getGtu().getFront().getDx());
94              boolean ignoreIfUpstream = true;
95              return new DownstreamNeighboursIterableACC(getGtu(), record, Length.max(Length.ZERO, pos),
96                      getGtu().getParameters().getParameter(RANGE), getGtu().getFront(), this.sensors, RelativeLane.CURRENT,
97                      ignoreIfUpstream);
98          }
99          catch (ParameterException | GTUException exception)
100         {
101             throw new RuntimeException("Unexpected exception while computing gtu alongside.", exception);
102         }
103     }
104 
105     /**
106      * Extends the regular {@code DownstreamNeighborsIterable} class with skipping leaders that are not sensed by the system.
107      * <p>
108      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
109      * <br>
110      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
111      * <p>
112      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 13, 2019 <br>
113      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
114      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
115      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
116      */
117     private static class DownstreamNeighboursIterableACC extends DownstreamNeighborsIterable
118     {
119 
120         /**
121          * Constructor.
122          * @param perceivingGtu LaneBasedGTU; perceiving GTU
123          * @param root LaneRecord&lt;?&gt;; root record
124          * @param initialPosition Length; position on the root record
125          * @param maxDistance Length; maximum distance to search
126          * @param relativePosition RelativePosition; position to which distance are calculated by subclasses
127          * @param headwayGtuType HeadwayGtuType; type of HeadwayGTU to return
128          * @param lane RelativeLane; relative lane (used for a left/right distinction to prevent dead-locks)
129          * @param ignoreIfUpstream boolean; whether to ignore GTU that are partially upstream of a record
130          */
131         @SuppressWarnings("checkstyle:parameternumber")
132         DownstreamNeighboursIterableACC(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root,
133                 final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
134                 final HeadwayGtuType headwayGtuType, final RelativeLane lane, final boolean ignoreIfUpstream)
135         {
136             super(perceivingGtu, root, initialPosition, maxDistance, relativePosition, headwayGtuType, lane, ignoreIfUpstream);
137         }
138 
139         /** {@inheritDoc} */
140         @Override
141         protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
142         {
143             Entry next;
144             do
145             {
146                 next = super.getNext(record, position, counter);
147             }
148             // skip leaders that are not the direct leader and that are not CACC
149             while (next != null && counter > 1 && !(next.getObject().getTacticalPlanner() instanceof CACC));
150             return next;
151         }
152 
153     }
154 
155     /**
156      * Default CACC sensors. This returns all information except desired speed for the first leader and CACC leaders. Remaining
157      * leaders are provided null information.
158      * <p>
159      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
160      * <br>
161      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
162      * <p>
163      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 13, 2019 <br>
164      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
165      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
166      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
167      */
168     private static class OnboardSensors implements HeadwayGtuType
169     {
170 
171         /**
172          * Constructor.
173          */
174         OnboardSensors()
175         {
176             //
177         }
178 
179         /** {@inheritDoc} */
180         @Override
181         public HeadwayGTU createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
182                 final Length distance) throws GTUException, ParameterException
183         {
184             Time t = perceivingGtu.getSimulator().getSimulatorTime().minus(perceivingGtu.getParameters().getParameter(DELAY));
185             String id = perceivedGtu.getId();
186             GTUType gtuType = perceivedGtu.getGTUType();
187             Length length = perceivedGtu.getLength();
188             Length width = perceivedGtu.getWidth();
189             Speed v = perceivedGtu.getSpeed(t);
190             Acceleration a = perceivedGtu.getAcceleration(t);
191             Speed desiredSpeed = null;
192             List<GTUStatus> status = new ArrayList<>();
193             if (perceivedGtu.isBrakingLightsOn(t))
194             {
195                 status.add(GTUStatus.BRAKING_LIGHTS);
196             }
197             switch (perceivedGtu.getTurnIndicatorStatus(t))
198             {
199                 case HAZARD:
200                     status.add(GTUStatus.EMERGENCY_LIGHTS);
201                     break;
202                 case LEFT:
203                     status.add(GTUStatus.LEFT_TURNINDICATOR);
204                     break;
205                 case RIGHT:
206                     status.add(GTUStatus.RIGHT_TURNINDICATOR);
207                     break;
208                 default:
209                     break;
210             }
211             return new HeadwayGTUSimple(id, gtuType, distance, length, width, v, a, desiredSpeed,
212                     status.toArray(new GTUStatus[status.size()]));
213         }
214 
215         /** {@inheritDoc} */
216         @Override
217         public HeadwayGTU createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
218                 final Length distance) throws GTUException, ParameterException
219         {
220             throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
221         }
222 
223         /** {@inheritDoc} */
224         @Override
225         public HeadwayGTU createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
226                 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
227         {
228             throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
229         }
230 
231     }
232 
233 }