View Javadoc
1   package org.opentrafficsim.core.network.lane;
2   
3   import java.rmi.RemoteException;
4   
5   import org.opentrafficsim.core.gtu.RelativePosition;
6   import org.opentrafficsim.core.gtu.lane.LaneBasedGTU;
7   import org.opentrafficsim.core.network.NetworkException;
8   import org.opentrafficsim.core.unit.LengthUnit;
9   import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
10  
11  /**
12   * This is a sensor that is placed at the start of a Lane to register a GTU on the lane, and register the lane with the
13   * GTU when the front of the vehicle passes over the sensor.
14   * <p>
15   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
16   * reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version Jan 1, 2015 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   */
23  public class SensorLaneStart extends AbstractSensor
24  {
25      /** */
26      private static final long serialVersionUID = 20141231L;
27  
28      /**
29       * Place a sensor that is triggered with the front of the GTU at the start of the lane.
30       * @param lane The lane for which this is a sensor.
31       */
32      public SensorLaneStart(final Lane lane)
33      {
34          super(lane, new DoubleScalar.Rel<LengthUnit>(Math.ulp(0.0), LengthUnit.METER), RelativePosition.FRONT);
35      }
36  
37      /**
38       * {@inheritDoc} <br>
39       * For this method, we assume that the right sensor triggered this method. In this case the sensor that indicates
40       * the front of the GTU. The code triggering the sensor therefore has to do the checking for sensor type.
41       * @throws RemoteException on communications failure
42       */
43      @Override
44      public final void trigger(final LaneBasedGTU<?> gtu) throws RemoteException
45      {
46          try
47          {
48              // if the GTU has the front as its reference point: it enters with its front.
49              // otherwise, negatively displaced by the difference between the front and the reference position.
50              getLane().addGTU(gtu, new DoubleScalar.Rel<LengthUnit>(-gtu.getFront().getDx().getSI(), LengthUnit.SI));
51              gtu.addFrontToSubsequentLane(getLane());
52          }
53          catch (NetworkException exception)
54          {
55              // Cannot happen
56              exception.printStackTrace();
57          }
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      @SuppressWarnings("checkstyle:designforextension")
63      public String toString()
64      {
65          return "SensorLaneStart [getLane()=" + this.getLane() + ", getLongitudinalPosition()="
66                  + this.getLongitudinalPosition() + ", getPositionType()=" + this.getPositionType() + "]";
67      }
68  
69  }