1 package org.opentrafficsim.road.car;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import java.util.Map;
6
7 import javax.naming.NamingException;
8
9 import org.djunits.unit.DurationUnit;
10 import org.djunits.unit.util.UNITS;
11 import org.djunits.value.vdouble.scalar.Direction;
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.djutils.draw.point.Point2d;
17 import org.junit.jupiter.api.Test;
18 import org.opentrafficsim.base.geometry.OtsLine2d;
19 import org.opentrafficsim.base.parameters.Parameters;
20 import org.opentrafficsim.core.definitions.DefaultsNl;
21 import org.opentrafficsim.core.dsol.AbstractOtsModel;
22 import org.opentrafficsim.core.dsol.OtsSimulator;
23 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
24 import org.opentrafficsim.core.gtu.GtuException;
25 import org.opentrafficsim.core.gtu.GtuType;
26 import org.opentrafficsim.core.network.NetworkException;
27 import org.opentrafficsim.core.network.Node;
28 import org.opentrafficsim.core.perception.HistoryManagerDevs;
29 import org.opentrafficsim.road.DefaultTestParameters;
30 import org.opentrafficsim.road.FixedCarFollowing;
31 import org.opentrafficsim.road.definitions.DefaultsRoadNl;
32 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
33 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.Lmrs;
34 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
35 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory.Setting;
36 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
37 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
38 import org.opentrafficsim.road.network.LaneKeepingPolicy;
39 import org.opentrafficsim.road.network.RoadNetwork;
40 import org.opentrafficsim.road.network.lane.CrossSectionLink;
41 import org.opentrafficsim.road.network.lane.Lane;
42 import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
43 import org.opentrafficsim.road.network.lane.LanePosition;
44 import org.opentrafficsim.road.network.lane.LaneType;
45
46 import nl.tudelft.simulation.dsol.SimRuntimeException;
47
48
49
50
51
52
53
54
55 public final class CarTest implements UNITS
56 {
57
58
59 private CarTest()
60 {
61
62 }
63
64
65
66
67
68
69
70
71 @SuppressWarnings("static-method")
72 @Test
73 public void carTest() throws NetworkException, SimRuntimeException, NamingException, GtuException
74 {
75 Duration initialTime = new Duration(0, DurationUnit.SI);
76 OtsSimulatorInterface simulator = makeSimulator();
77 RoadNetwork network = new RoadNetwork("network", simulator);
78 GtuType gtuType = DefaultsNl.CAR;
79 LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
80 Lane lane = makeLane(network, laneType, simulator);
81 Length initialPosition = new Length(12, METER);
82 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
83 LaneBasedGtu referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, network);
84 assertEquals("12345", referenceCar.getId(), "The car should store it's ID");
85 assertEquals(initialPosition.getSI(), referenceCar.getPosition(lane, referenceCar.getReference(), initialTime).getSI(),
86 0.0001, "At t=initialTime the car should be at it's initial position");
87 assertEquals(initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001, "The car should store it's initial speed");
88 assertEquals(0, referenceCar.getAcceleration().getSI(), 0.0001,
89 "The car should have an initial acceleration equal to 0");
90
91
92
93
94 }
95
96
97
98
99
100
101
102 public static OtsSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
103 {
104 OtsSimulatorInterface simulator = new OtsSimulator("CarTest");
105 Model model = new Model(simulator);
106 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model,
107 HistoryManagerDevs.noHistory(simulator));
108 return simulator;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public static LaneBasedGtu makeReferenceCar(final String id, final GtuType gtuType, final Lane lane,
126 final Length initialPosition, final Speed initialSpeed, final RoadNetwork network)
127 throws NamingException, NetworkException, SimRuntimeException, GtuException
128 {
129 Length length = new Length(5.0, METER);
130 Length width = new Length(2.0, METER);
131 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
132 Parameters parameters = DefaultTestParameters.create();
133 LaneBasedGtu gtu = new LaneBasedGtu(id, gtuType, length, width, maxSpeed, length.times(0.5), network);
134 gtu.setParameters(parameters);
135 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(new LmrsFactory<>(Lmrs::new)
136 .set(Setting.CAR_FOLLOWING_MODEL, (h, v) -> new FixedCarFollowing().get()).create(gtu), gtu);
137 gtu.init(strategicalPlanner, new LanePosition(lane, initialPosition).getLocation(), initialSpeed);
138
139 return gtu;
140 }
141
142
143
144
145
146
147
148
149
150 public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OtsSimulatorInterface simulator)
151 throws NetworkException
152 {
153 Node n1 = new Node(network, "n1", new Point2d(0, 0), Direction.ZERO);
154 Node n2 = new Node(network, "n2", new Point2d(100000.0, 0.0), Direction.ZERO);
155 Point2d[] coordinates = new Point2d[] {new Point2d(0.0, 0.0), new Point2d(100000.0, 0.0)};
156 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, DefaultsNl.ROAD, new OtsLine2d(coordinates),
157 null, LaneKeepingPolicy.KEEPRIGHT);
158 Length latPos = new Length(0.0, METER);
159 Length width = new Length(4.0, METER);
160 return LaneGeometryUtil.createStraightLane(link12, "lane.1", latPos, latPos, width, width, laneType,
161 Map.of(DefaultsNl.VEHICLE, new Speed(100, KM_PER_HOUR)));
162 }
163
164
165 protected static class Model extends AbstractOtsModel
166 {
167
168
169
170
171 public Model(final OtsSimulatorInterface simulator)
172 {
173 super(simulator);
174 }
175
176 @Override
177 public final void constructModel() throws SimRuntimeException
178 {
179
180 }
181
182 @Override
183 public final RoadNetwork getNetwork()
184 {
185 return null;
186 }
187 }
188 }