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
46
47
48
49
50
51
52
53
54 public class SensorTest implements UNITS
55 {
56
57
58
59
60 @Test
61 public final void sensorTest() throws Exception
62 {
63 OTSRoadNetwork network = new OTSRoadNetwork("sensor test network", true);
64
65
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));
69 GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
70 LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
71
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
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
93 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
94
95 Length carLength = new Length(4, METER);
96
97 Length carWidth = new Length(1.8, METER);
98
99 Speed maximumSpeed = new Speed(100, KM_PER_HOUR);
100
101 String carID = "theCar";
102
103 FixedAccelerationModel fas =
104 new FixedAccelerationModel(new Acceleration(0.5, METER_PER_SECOND_2), new Duration(100, SECOND));
105
106 Parameters parameters = DefaultTestParameters.create();
107
108
109
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;
130 }
131 }
132
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
144
145
146
147
148
149
150
151
152 }
153 }
154
155
156 class TriggerSensor extends AbstractSensor
157 {
158
159 private static final long serialVersionUID = 1L;
160
161
162
163
164
165
166
167
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
176 @Override
177 public void triggerResponse(final LaneBasedGTU gtu)
178 {
179
180 }
181
182
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
194
195
196
197
198
199
200
201
202 class DummyModelForSensorTest extends AbstractOTSModel
203 {
204
205 private static final long serialVersionUID = 20150114L;
206
207
208
209
210 DummyModelForSensorTest(final OTSSimulatorInterface simulator)
211 {
212 super(simulator);
213 }
214
215
216 @Override
217 public final void constructModel() throws SimRuntimeException
218 {
219
220 }
221
222
223 @Override
224 public final OTSRoadNetwork getNetwork()
225 {
226 return null;
227 }
228 }