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.TimeUnit;
9 import org.djunits.unit.UNITS;
10 import org.djunits.value.vdouble.scalar.Acceleration;
11 import org.djunits.value.vdouble.scalar.Duration;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.djunits.value.vdouble.scalar.Speed;
14 import org.djunits.value.vdouble.scalar.Time;
15 import org.junit.Test;
16 import org.opentrafficsim.base.parameters.Parameters;
17 import org.opentrafficsim.core.compatibility.Compatible;
18 import org.opentrafficsim.core.dsol.OTSModelInterface;
19 import org.opentrafficsim.core.geometry.OTSPoint3D;
20 import org.opentrafficsim.core.gtu.GTUDirectionality;
21 import org.opentrafficsim.core.gtu.GTUType;
22 import org.opentrafficsim.core.gtu.RelativePosition;
23 import org.opentrafficsim.core.network.Network;
24 import org.opentrafficsim.core.network.NetworkException;
25 import org.opentrafficsim.core.network.OTSNetwork;
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.factory.LaneFactory;
35 import org.opentrafficsim.road.network.lane.object.sensor.AbstractSensor;
36 import org.opentrafficsim.simulationengine.SimpleSimulator;
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 Network network = new OTSNetwork("sensor test network");
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 = CAR;
70 LaneType laneType = LaneType.TWO_WAY_LANE;
71
72 OTSModelInterface model = new DummyModelForSensorTest();
73 final SimpleSimulator simulator = new SimpleSimulator(Time.ZERO, Duration.ZERO, new Duration(3600.0, SECOND), model);
74 Lane[] lanesA = LaneFactory.makeMultiLane(network, "A", nodeAFrom, nodeATo, null, 3, laneType,
75 new Speed(100, KM_PER_HOUR), simulator);
76 Lane[] lanesB = LaneFactory.makeMultiLane(network, "B", nodeATo, nodeBTo, null, 3, laneType,
77 new Speed(100, KM_PER_HOUR), simulator);
78
79
80 for (Lane lane : lanesA)
81 {
82 Length longitudinalPosition = new Length(999.9999, METER);
83 TriggerSensor sensor = new TriggerSensor(lane, longitudinalPosition, RelativePosition.REFERENCE,
84 "Trigger@" + lane.toString(), simulator);
85 }
86
87 Length positionA = new Length(100, METER);
88 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
89 initialLongitudinalPositions.add(new DirectedLanePosition(lanesA[1], positionA, GTUDirectionality.DIR_PLUS));
90
91
92 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
93
94 Length carLength = new Length(4, METER);
95
96 Length carWidth = new Length(1.8, METER);
97
98 Speed maximumSpeed = new Speed(100, KM_PER_HOUR);
99
100 String carID = "theCar";
101
102 FixedAccelerationModel fas =
103 new FixedAccelerationModel(new Acceleration(0.5, METER_PER_SECOND_2), new Duration(100, SECOND));
104
105 Parameters parameters = DefaultTestParameters.create();
106
107
108
109 LaneBasedIndividualGTU car = new LaneBasedIndividualGTU(carID, gtuType, carLength, carWidth, maximumSpeed,
110 carLength.multiplyBy(0.5), simulator, (OTSNetwork) network);
111 LaneBasedStrategicalPlanner strategicalPlanner =
112 new LaneBasedStrategicalRoutePlanner(new LaneBasedGTUFollowingTacticalPlanner(fas, car), car);
113 car.setParameters(parameters);
114 car.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
115 simulator.runUpTo(new Time(1, TimeUnit.BASE_SECOND));
116 if (!simulator.isRunning())
117 {
118 simulator.start();
119 }
120 while (simulator.isRunning())
121 {
122 try
123 {
124 Thread.sleep(1);
125 }
126 catch (InterruptedException ie)
127 {
128 ie = null;
129 }
130 }
131
132 Set<SimEventInterface<SimTimeDoubleUnit>> eventList = simulator.getEventList();
133 SimEventInterface<SimTimeDoubleUnit> triggerEvent = null;
134 for (SimEventInterface<SimTimeDoubleUnit> event : eventList)
135 {
136 System.out.println("Scheduled Event " + event);
137 if (event.toString().contains("trigger"))
138 {
139 triggerEvent = event;
140 }
141 }
142
143
144
145
146
147
148
149
150
151 }
152 }
153
154
155 class TriggerSensor extends AbstractSensor
156 {
157
158 private static final long serialVersionUID = 1L;
159
160
161
162
163
164
165
166
167
168 TriggerSensor(final Lane lane, final Length longitudinalPosition, final RelativePosition.TYPE positionType,
169 final String name, final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
170 {
171 super(name, lane, longitudinalPosition, positionType, simulator, Compatible.EVERYTHING);
172 }
173
174
175 @Override
176 public void triggerResponse(final LaneBasedGTU gtu)
177 {
178
179 }
180
181
182 @Override
183 public AbstractSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator,
184 final boolean animation) throws NetworkException
185 {
186 return null;
187 }
188
189 }
190
191
192
193
194
195
196
197
198
199
200
201 class DummyModelForSensorTest implements OTSModelInterface
202 {
203
204 private static final long serialVersionUID = 20150114L;
205
206
207 private SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator;
208
209
210
211
212
213 public final void setSimulator(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator)
214 {
215 this.simulator = simulator;
216 }
217
218
219 @Override
220 public final void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0) throws SimRuntimeException
221 {
222
223 }
224
225
226 @Override
227 public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
228
229 {
230 if (null == this.simulator)
231 {
232 throw new Error("getSimulator called, but simulator field is null");
233 }
234 return this.simulator;
235 }
236
237
238 @Override
239 public OTSNetwork getNetwork()
240 {
241 return null;
242 }
243
244 }