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://tudelft.nl/staff/p.knoppers-1">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 Duration; time to collision
42       */
43      public TtcRoomChecker(final Duration ttc)
44      {
45          this.ttc = ttc;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public final Placement canPlace(final SortedSet<HeadwayGtu> leaders, final LaneBasedGtuCharacteristics characteristics,
51              final Duration since, final LanePosition initialPosition) throws NetworkException, GtuException
52      {
53          Speed speedLimit = initialPosition.lane().getSpeedLimit(characteristics.getGtuType());
54          Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
55          if (leaders.isEmpty())
56          {
57              return new Placement(desiredSpeedProxy, initialPosition);
58          }
59          HeadwayGtu leader = leaders.first();
60          Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
61          if ((speed.le(leader.getSpeed()) || leader.getDistance().divide(speed.minus(leader.getSpeed())).gt(this.ttc)) && leader
62                  .getDistance().gt(speed.times(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
63          {
64              return new Placement(speed, initialPosition);
65          }
66          return Placement.NO;
67      }
68  
69      /**
70       * Returns the TTC value.
71       * @return Duration; TTC value
72       */
73      public final Duration getTtc()
74      {
75          return this.ttc;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final String toString()
81      {
82          return "TTCRoomChecker [ttc=" + this.ttc + "]";
83      }
84  
85  }