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.TimeUnit;
11 import org.djunits.unit.util.UNITS;
12 import org.djunits.value.vdouble.scalar.Acceleration;
13 import org.djunits.value.vdouble.scalar.Direction;
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Speed;
17 import org.djunits.value.vdouble.scalar.Time;
18 import org.djutils.draw.point.Point2d;
19 import org.junit.jupiter.api.Test;
20 import org.opentrafficsim.base.geometry.OtsLine2d;
21 import org.opentrafficsim.base.parameters.Parameters;
22 import org.opentrafficsim.core.definitions.DefaultsNl;
23 import org.opentrafficsim.core.dsol.AbstractOtsModel;
24 import org.opentrafficsim.core.dsol.OtsSimulator;
25 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
26 import org.opentrafficsim.core.gtu.GtuException;
27 import org.opentrafficsim.core.gtu.GtuType;
28 import org.opentrafficsim.core.network.NetworkException;
29 import org.opentrafficsim.core.network.Node;
30 import org.opentrafficsim.core.perception.HistoryManagerDevs;
31 import org.opentrafficsim.road.DefaultTestParameters;
32 import org.opentrafficsim.road.definitions.DefaultsRoadNl;
33 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
34 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCfLcTacticalPlanner;
35 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36 import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
37 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
38 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
39 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
40 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
41 import org.opentrafficsim.road.network.RoadNetwork;
42 import org.opentrafficsim.road.network.lane.CrossSectionLink;
43 import org.opentrafficsim.road.network.lane.Lane;
44 import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
45 import org.opentrafficsim.road.network.lane.LanePosition;
46 import org.opentrafficsim.road.network.lane.LaneType;
47 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
48
49 import nl.tudelft.simulation.dsol.SimRuntimeException;
50
51
52
53
54
55
56
57
58 public class CarTest implements UNITS
59 {
60
61
62
63
64
65
66
67 @SuppressWarnings("static-method")
68 @Test
69 public final void carTest() throws NetworkException, SimRuntimeException, NamingException, GtuException
70 {
71 Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
72 OtsSimulatorInterface simulator = makeSimulator();
73 RoadNetwork network = new RoadNetwork("network", simulator);
74 GtuType gtuType = DefaultsNl.CAR;
75 LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
76 Lane lane = makeLane(network, laneType, simulator);
77 Length initialPosition = new Length(12, METER);
78 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
79 GtuFollowingModelOld gtuFollowingModel =
80 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
81 LaneChangeModel laneChangeModel = new Egoistic();
82 LaneBasedGtu referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, gtuFollowingModel,
83 laneChangeModel, network);
84 assertEquals("12345", referenceCar.getId(), "The car should store it's ID");
85 assertEquals(initialPosition.getSI(), referenceCar.position(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
126
127 public static LaneBasedGtu makeReferenceCar(final String id, final GtuType gtuType, final Lane lane,
128 final Length initialPosition, final Speed initialSpeed, final GtuFollowingModelOld gtuFollowingModel,
129 final LaneChangeModel laneChangeModel, final RoadNetwork network)
130 throws NamingException, NetworkException, SimRuntimeException, GtuException
131 {
132 Length length = new Length(5.0, METER);
133 Length width = new Length(2.0, METER);
134 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
135 Parameters parameters = DefaultTestParameters.create();
136 LaneBasedGtu gtu = new LaneBasedGtu(id, gtuType, length, width, maxSpeed, length.times(0.5), network);
137 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
138 new LaneBasedCfLcTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
139 gtu.setParameters(parameters);
140 gtu.init(strategicalPlanner, new LanePosition(lane, initialPosition), initialSpeed);
141
142 return gtu;
143 }
144
145
146
147
148
149
150
151
152 public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OtsSimulatorInterface simulator)
153 throws NetworkException
154 {
155 Node n1 = new Node(network, "n1", new Point2d(0, 0), Direction.ZERO);
156 Node n2 = new Node(network, "n2", new Point2d(100000.0, 0.0), Direction.ZERO);
157 Point2d[] coordinates = new Point2d[] {new Point2d(0.0, 0.0), new Point2d(100000.0, 0.0)};
158 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, DefaultsNl.ROAD, new OtsLine2d(coordinates),
159 null, LaneKeepingPolicy.KEEPRIGHT);
160 Length latPos = new Length(0.0, METER);
161 Length width = new Length(4.0, METER);
162 return LaneGeometryUtil.createStraightLane(link12, "lane.1", latPos, latPos, width, width, laneType,
163 Map.of(DefaultsNl.VEHICLE, new Speed(100, KM_PER_HOUR)));
164 }
165
166
167 protected static class Model extends AbstractOtsModel
168 {
169
170 private static final long serialVersionUID = 20141027L;
171
172
173
174
175 public Model(final OtsSimulatorInterface simulator)
176 {
177 super(simulator);
178 }
179
180 @Override
181 public final void constructModel() throws SimRuntimeException
182 {
183
184 }
185
186 @Override
187 public final RoadNetwork getNetwork()
188 {
189 return null;
190 }
191 }
192 }