View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.CAR;
4   
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   
8   import org.djunits.unit.DurationUnit;
9   import org.djunits.unit.TimeUnit;
10  import org.djunits.unit.UNITS;
11  import org.djunits.value.vdouble.scalar.Acceleration;
12  import org.djunits.value.vdouble.scalar.Duration;
13  import org.djunits.value.vdouble.scalar.Length;
14  import org.djunits.value.vdouble.scalar.Speed;
15  import org.djunits.value.vdouble.scalar.Time;
16  import org.junit.Test;
17  import org.opentrafficsim.base.parameters.Parameters;
18  import org.opentrafficsim.core.compatibility.Compatible;
19  import org.opentrafficsim.core.dsol.AbstractOTSModel;
20  import org.opentrafficsim.core.dsol.OTSModelInterface;
21  import org.opentrafficsim.core.dsol.OTSSimulator;
22  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
23  import org.opentrafficsim.core.geometry.OTSPoint3D;
24  import org.opentrafficsim.core.gtu.GTUDirectionality;
25  import org.opentrafficsim.core.gtu.GTUType;
26  import org.opentrafficsim.core.gtu.RelativePosition;
27  import org.opentrafficsim.core.network.Network;
28  import org.opentrafficsim.core.network.NetworkException;
29  import org.opentrafficsim.core.network.OTSNetwork;
30  import org.opentrafficsim.core.network.OTSNode;
31  import org.opentrafficsim.road.DefaultTestParameters;
32  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
33  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
34  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner;
35  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
37  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
38  import org.opentrafficsim.road.network.factory.LaneFactory;
39  import org.opentrafficsim.road.network.lane.object.sensor.AbstractSensor;
40  
41  import nl.tudelft.simulation.dsol.SimRuntimeException;
42  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
43  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
44  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
45  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
46  
47  /**
48   * Test sensors and scheduling of trigger.
49   * <p>
50   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
51   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
52   * <p>
53   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
54   * initial version 16 jan. 2015 <br>
55   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
56   */
57  public class SensorTest implements UNITS
58  {
59      /**
60       * Test the constructors of SensorLaneEnd and SensorLaneStart.
61       * @throws Exception when something goes wrong (should not happen)
62       */
63      @Test
64      public final void sensorTest() throws Exception
65      {
66          Network network = new OTSNetwork("sensor test network");
67          // First we need a set of Lanes
68          // To create Lanes we need Nodes and a LaneType
69          OTSNode nodeAFrom = new OTSNode(network, "AFrom", new OTSPoint3D(0, 0, 0));
70          OTSNode nodeATo = new OTSNode(network, "ATo", new OTSPoint3D(1000, 0, 0));
71          OTSNode nodeBTo = new OTSNode(network, "BTo", new OTSPoint3D(20000, 0, 0)); // so car won't run off lane B in 100 s.
72          GTUType gtuType = CAR;
73          LaneType laneType = LaneType.TWO_WAY_LANE;
74          // And a simulator, but for that we first need something that implements OTSModelInterface
75          OTSSimulatorInterface simulator = new OTSSimulator();
76          OTSModelInterface model = new DummyModelForSensorTest(simulator);
77          simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
78          Lane[] lanesA = LaneFactory.makeMultiLane(network, "A", nodeAFrom, nodeATo, null, 3, laneType,
79                  new Speed(100, KM_PER_HOUR), simulator);
80          Lane[] lanesB = LaneFactory.makeMultiLane(network, "B", nodeATo, nodeBTo, null, 3, laneType,
81                  new Speed(100, KM_PER_HOUR), simulator);
82  
83          // put a sensor on each of the lanes at the end of LaneA
84          for (Lane lane : lanesA)
85          {
86              Length longitudinalPosition = new Length(999.9999, METER);
87              TriggerSensor sensor = new TriggerSensor(lane, longitudinalPosition, RelativePosition.REFERENCE,
88                      "Trigger@" + lane.toString(), simulator);
89          }
90  
91          Length positionA = new Length(100, METER);
92          Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
93          initialLongitudinalPositions.add(new DirectedLanePosition(lanesA[1], positionA, GTUDirectionality.DIR_PLUS));
94  
95          // A Car needs an initial speed
96          Speed initialSpeed = new Speed(50, KM_PER_HOUR);
97          // Length of the Car
98          Length carLength = new Length(4, METER);
99          // Width of the Car
100         Length carWidth = new Length(1.8, METER);
101         // Maximum speed of the Car
102         Speed maximumSpeed = new Speed(100, KM_PER_HOUR);
103         // ID of the Car
104         String carID = "theCar";
105         // Create an acceleration profile for the car
106         FixedAccelerationModel fas =
107                 new FixedAccelerationModel(new Acceleration(0.5, METER_PER_SECOND_2), new Duration(100, SECOND));
108         // Now we can make a car (GTU) (and we don't even have to hold a pointer to it)
109         Parameters parameters = DefaultTestParameters.create();
110 
111         // LaneBasedBehavioralCharacteristics drivingCharacteristics =
112         // new LaneBasedBehavioralCharacteristics(fas, null);
113         LaneBasedIndividualGTU car = new LaneBasedIndividualGTU(carID, gtuType, carLength, carWidth, maximumSpeed,
114                 carLength.multiplyBy(0.5), simulator, (OTSNetwork) network);
115         LaneBasedStrategicalPlanner strategicalPlanner =
116                 new LaneBasedStrategicalRoutePlanner(new LaneBasedGTUFollowingTacticalPlanner(fas, car), car);
117         car.setParameters(parameters);
118         car.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
119         simulator.runUpTo(new Time(1, TimeUnit.BASE_SECOND));
120         if (!simulator.isRunning())
121         {
122             simulator.start();
123         }
124         while (simulator.isRunning())
125         {
126             try
127             {
128                 Thread.sleep(1);
129             }
130             catch (InterruptedException ie)
131             {
132                 ie = null; // ignore
133             }
134         }
135         // Construction of the car scheduled a car move event at t=0
136         Set<SimEventInterface<SimTimeDoubleUnit>> eventList = simulator.getEventList();
137         SimEventInterface<SimTimeDoubleUnit> triggerEvent = null;
138         for (SimEventInterface<SimTimeDoubleUnit> event : eventList)
139         {
140             System.out.println("Scheduled Event " + event);
141             if (event.toString().contains("trigger"))
142             {
143                 triggerEvent = event;
144             }
145         }
146         // XXX this is not true anymore with OperationalPlans, Perception, etc =>
147         // XXX the number of events that should be scheduled can vary per models chosen
148         // XXX assertEquals("There should be three scheduled events (trigger, leaveLane,
149         // XXX car.move, terminate)", 4, eventList.size());
150         // The sensor should be triggered around t=38.3403 (exact value: 10 / 9 * (sqrt(3541) - 25))
151         // System.out.println("trigger event is " + triggerEvent);
152         // / TODO not triggered in next half second.
153         // XXX assertEquals("Trigger event should be around 38.3403", 38.3403,
154         // XXX triggerEvent.getAbsoluteExecutionTime().get().getSI(), 0.0001);
155     }
156 }
157 
158 /** */
159 class TriggerSensor extends AbstractSensor
160 {
161     /** */
162     private static final long serialVersionUID = 1L;
163 
164     /**
165      * @param lane lane of the sensor
166      * @param longitudinalPosition position of the sensor on the lane
167      * @param positionType trigger position of the GTU
168      * @param name name of the sensor
169      * @param simulator the simulator
170      * @throws NetworkException in case position is out of bounds
171      */
172     TriggerSensor(final Lane lane, final Length longitudinalPosition, final RelativePosition.TYPE positionType,
173             final String name, final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
174     {
175         super(name, lane, longitudinalPosition, positionType, simulator, Compatible.EVERYTHING);
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public void triggerResponse(final LaneBasedGTU gtu)
181     {
182         // TODO check that the sensor is triggered at the right time.
183     }
184 
185     /** {@inheritDoc} */
186     @Override
187     public AbstractSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator)
188             throws NetworkException
189     {
190         return null;
191     }
192 
193 }
194 
195 /**
196  * Dummy OTSModelInterface.
197  * <p>
198  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
199  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
200  * <p>
201  * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
202  * initial version 4 jan. 2015 <br>
203  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
204  */
205 class DummyModelForSensorTest extends AbstractOTSModel
206 {
207     /** */
208     private static final long serialVersionUID = 20150114L;
209 
210     /**
211      * @param simulator the simulator to use
212      */
213     DummyModelForSensorTest(final OTSSimulatorInterface simulator)
214     {
215         super(simulator);
216     }
217 
218     /** {@inheritDoc} */
219     @Override
220     public final void constructModel() throws SimRuntimeException
221     {
222         //
223     }
224 
225     /** {@inheritDoc} */
226     @Override
227     public final OTSNetwork getNetwork()
228     {
229         return null;
230     }
231 }