View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import org.djunits.unit.LengthUnit;
4   import org.djunits.value.vdouble.scalar.Acceleration;
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.djunits.value.vdouble.scalar.Time;
8   import org.opentrafficsim.base.parameters.ParameterException;
9   import org.opentrafficsim.base.parameters.Parameters;
10  import org.opentrafficsim.core.gtu.GTUException;
11  import org.opentrafficsim.core.gtu.perception.EgoPerception;
12  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
13  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
14  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
16  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
17  import org.opentrafficsim.road.gtu.lane.perception.categories.BusStopPerception;
18  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayBusStop;
19  import org.opentrafficsim.road.gtu.lane.plan.operational.SimpleOperationalPlan;
20  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
21  import org.opentrafficsim.road.gtu.lane.tactical.pt.BusSchedule;
22  import org.opentrafficsim.road.gtu.lane.tactical.util.CarFollowingUtil;
23  import org.opentrafficsim.road.network.lane.object.BusStop;
24  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
25  
26  /**
27   * <p>
28   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
30   * <p>
31   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 27 jan. 2017 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
35   */
36  public class AccelerationBusStop implements AccelerationIncentive
37  {
38  
39      /** Distance within which the bus can open the doors. */
40      // TODO this is much more complex: tail blocking other traffic? other bus in front? many people at bus stop?
41      private static final Length STOP_DISTANCE = new Length(15.0, LengthUnit.SI);
42  
43      /** {@inheritDoc} */
44      @Override
45      @SuppressWarnings("checkstyle:parameternumber")
46      public final void accelerate(final SimpleOperationalPlan simplePlan, final RelativeLane lane, final LaneBasedGTU gtu,
47              final LanePerception perception, final CarFollowingModel carFollowingModel, final Speed speed,
48              final Parameters params, final SpeedLimitInfo speedLimitInfo)
49              throws OperationalPlanException, ParameterException, GTUException
50      {
51          PerceptionCollectable<HeadwayBusStop, BusStop> stops =
52                  perception.getPerceptionCategory(BusStopPerception.class).getBusStops();
53          if (stops.isEmpty())
54          {
55              return;
56          }
57          BusSchedule busSchedule = (BusSchedule) gtu.getStrategicalPlanner().getRoute();
58          Time now = gtu.getSimulator().getSimulatorTime();
59          for (HeadwayBusStop stop : stops)
60          {
61              String busStopId = stop.getId();
62              if (busSchedule.isLineStop(busStopId, now))
63              {
64  
65                  // check when to leave
66                  boolean stoppedAtStop = stop.getRelativeLane().isCurrent() && stop.getDistance().le(STOP_DISTANCE)
67                          && perception.getPerceptionCategory(EgoPerception.class).getSpeed().eq0();
68                  if (busSchedule.getActualDepartureBusStop(busStopId) == null)
69                  {
70                      if (stoppedAtStop)
71                      {
72                          // bus just stopped
73                          Time departureTime = now.plus(busSchedule.getDwellTime(busStopId));
74                          if (busSchedule.isForceSchedule(busStopId))
75                          {
76                              departureTime = Time.max(departureTime, busSchedule.getDepartureTime(busStopId));
77                          }
78                          busSchedule.setActualDeparture(busStopId, stop.getConflictIds(), departureTime);
79                      }
80                  }
81  
82                  // stop if not known yet, or before departure time
83                  if (busSchedule.getActualDepartureBusStop(busStopId) == null
84                          || now.lt(busSchedule.getActualDepartureBusStop(busStopId)))
85                  {
86                      if (stoppedAtStop)
87                      {
88                          // stand still at location where stop was initiated
89                          simplePlan.minimizeAcceleration(Acceleration.ZERO);
90                      }
91                      else
92                      {
93                          // decelerate to initiate stop
94                          simplePlan.minimizeAcceleration(
95                                  CarFollowingUtil.stop(carFollowingModel, params, speed, speedLimitInfo, stop.getDistance()));
96                      }
97                  }
98              }
99          }
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final String toString()
105     {
106         return "AccelerationBusStop";
107     }
108 
109 }