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