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
49
50
51
52
53
54
55
56
57 public class SensorTest implements UNITS
58 {
59
60
61
62
63 @Test
64 public final void sensorTest() throws Exception
65 {
66 Network network = new OTSNetwork("sensor test network");
67
68
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));
72 GTUType gtuType = CAR;
73 LaneType laneType = LaneType.TWO_WAY_LANE;
74
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
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
96 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
97
98 Length carLength = new Length(4, METER);
99
100 Length carWidth = new Length(1.8, METER);
101
102 Speed maximumSpeed = new Speed(100, KM_PER_HOUR);
103
104 String carID = "theCar";
105
106 FixedAccelerationModel fas =
107 new FixedAccelerationModel(new Acceleration(0.5, METER_PER_SECOND_2), new Duration(100, SECOND));
108
109 Parameters parameters = DefaultTestParameters.create();
110
111
112
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;
133 }
134 }
135
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
147
148
149
150
151
152
153
154
155 }
156 }
157
158
159 class TriggerSensor extends AbstractSensor
160 {
161
162 private static final long serialVersionUID = 1L;
163
164
165
166
167
168
169
170
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
179 @Override
180 public void triggerResponse(final LaneBasedGTU gtu)
181 {
182
183 }
184
185
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
197
198
199
200
201
202
203
204
205 class DummyModelForSensorTest extends AbstractOTSModel
206 {
207
208 private static final long serialVersionUID = 20150114L;
209
210
211
212
213 DummyModelForSensorTest(final OTSSimulatorInterface simulator)
214 {
215 super(simulator);
216 }
217
218
219 @Override
220 public final void constructModel() throws SimRuntimeException
221 {
222
223 }
224
225
226 @Override
227 public final OTSNetwork getNetwork()
228 {
229 return null;
230 }
231 }