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.perception.object.PerceivedGtu;
16  import org.opentrafficsim.road.gtu.tactical.util.SpeedLimitUtil;
17  import org.opentrafficsim.road.network.LanePosition;
18  
19  /**
20   * Room checker based on time-to-collision. The room is considered ok if:
21   * <ol>
22   * <li>The headway is larger than speed*1.0s + 3m</li>
23   * <li>time-to-collision &lt; value
24   * </ol>
25   * where 'value' is a given value in the constructor.
26   * <p>
27   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * </p>
30   * @author Alexander Verbraeck
31   * @author Peter Knoppers
32   * @author Wouter Schakel
33   */
34  public class TtcRoomChecker implements RoomChecker
35  {
36  
37      /** Time to collision. */
38      private final Duration ttc;
39  
40      /**
41       * Constructor.
42       * @param ttc time to collision
43       */
44      public TtcRoomChecker(final Duration ttc)
45      {
46          this.ttc = ttc;
47      }
48  
49      @Override
50      public final Placement canPlace(final SortedSet<PerceivedGtu> leaders, final LaneBasedGtuCharacteristics characteristics,
51              final Duration since, final LanePosition initialPosition) throws NetworkException, GtuException
52      {
53          Speed desiredSpeedProxy = SpeedLimitUtil.getDesiredSpeedProxy(
54                  initialPosition.lane().getSpeedLimits(characteristics.getGtuType()), characteristics.getMaximumSpeed());
55          if (leaders.isEmpty())
56          {
57              return new Placement(desiredSpeedProxy, initialPosition);
58          }
59          PerceivedGtu 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 TTC value
72       */
73      public final Duration getTtc()
74      {
75          return this.ttc;
76      }
77  
78      @Override
79      public final String toString()
80      {
81          return "TTCRoomChecker [ttc=" + this.ttc + "]";
82      }
83  
84  }