View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import java.util.SortedSet;
4   
5   import org.djunits.unit.DurationUnit;
6   import org.djunits.unit.LengthUnit;
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.opentrafficsim.core.gtu.GtuException;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.Placement;
13  import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.RoomChecker;
14  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuCharacteristics;
15  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
16  import org.opentrafficsim.road.network.lane.LanePosition;
17  
18  /**
19   * Room checker based on time-to-collision. The room is considered ok if:
20   * <ol>
21   * <li>The headway is larger than speed*1.0s + 3m</li>
22   * <li>time-to-collision &lt; value
23   * </ol>
24   * where 'value' is a given value in the constructor.
25   * <p>
26   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
27   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
28   * </p>
29   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
31   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
32   */
33  public class TtcRoomChecker implements RoomChecker
34  {
35  
36      /** Time to collision. */
37      private final Duration ttc;
38  
39      /**
40       * Constructor.
41       * @param ttc time to collision
42       */
43      public TtcRoomChecker(final Duration ttc)
44      {
45          this.ttc = ttc;
46      }
47  
48      @Override
49      public final Placement canPlace(final SortedSet<HeadwayGtu> leaders, final LaneBasedGtuCharacteristics characteristics,
50              final Duration since, final LanePosition initialPosition) throws NetworkException, GtuException
51      {
52          Speed speedLimit = initialPosition.lane().getSpeedLimit(characteristics.getGtuType());
53          Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
54          if (leaders.isEmpty())
55          {
56              return new Placement(desiredSpeedProxy, initialPosition);
57          }
58          HeadwayGtu leader = leaders.first();
59          Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
60          if ((speed.le(leader.getSpeed()) || leader.getDistance().divide(speed.minus(leader.getSpeed())).gt(this.ttc)) && leader
61                  .getDistance().gt(speed.times(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
62          {
63              return new Placement(speed, initialPosition);
64          }
65          return Placement.NO;
66      }
67  
68      /**
69       * Returns the TTC value.
70       * @return TTC value
71       */
72      public final Duration getTtc()
73      {
74          return this.ttc;
75      }
76  
77      @Override
78      public final String toString()
79      {
80          return "TTCRoomChecker [ttc=" + this.ttc + "]";
81      }
82  
83  }