AutomotiveDrivingModels is templated to efficiently run different types of simulations. Entities are parameterized by their:
Int, which uniquely identifies that entity.In addition to these types, the actions, environment and the driver models can also be parameterized.
This notebook demonstrates a 2D driving simulation where cars drive in a multi-lane stadium. The types are:
VehicleState, containing both the global and lane-relative position, and speedVehicleDef, containing length, width, and classIntWe use a Roadway as our environment. The Roadway type is based on the RNDF format.
In [3]:
using AutomotiveDrivingModels
using AutoViz
We generate a 3-lane stadium roadway:
In [4]:
roadway = gen_stadium_roadway(3)
Out[4]:
Let's populate a scene.
In [5]:
scene = Scene()
push!(scene,Vehicle(VehicleState(VecSE2(10.0,-DEFAULT_LANE_WIDTH,0.0), roadway, 29.0), VehicleDef(), 1))
push!(scene,Vehicle(VehicleState(VecSE2(40.0,0.0,0.0), roadway, 22.0), VehicleDef(), 2))
push!(scene,Vehicle(VehicleState(VecSE2(70.0,-DEFAULT_LANE_WIDTH,0.0), roadway, 27.0), VehicleDef(), 3))
car_colors = get_pastel_car_colors(scene)
cam = FitToContentCamera()
render(scene, roadway, cam=cam, car_colors=car_colors)
Out[5]:
Let's assign driver models.
In [6]:
timestep = 0.1
models = Dict{Int, DriverModel}()
models[1] = LatLonSeparableDriver( # produces LatLonAccels
ProportionalLaneTracker(), # lateral model
IntelligentDriverModel(), # longitudinal model
)
models[2] = Tim2DDriver(timestep,
mlane = MOBIL(timestep),
)
models[3] = StaticDriver{AccelTurnrate, MvNormal}(MvNormal([0.0,0.0], [1.0,0.1]))
set_desired_speed!(models[1], 12.0)
set_desired_speed!(models[2], 10.0)
set_desired_speed!(models[3], 8.0)
nticks = 100
rec = SceneRecord(nticks+1, timestep)
simulate!(rec, scene, roadway, models, nticks)
render(rec[0], roadway, cam=cam, car_colors=car_colors)
Out[6]:
We can use interact to inspect the simulation record. Note that the static driver just drives off the road.
In [ ]:
using Interact
@manipulate for frame_index in 1 : nframes(rec)
render(rec[frame_index-nframes(rec)], roadway, cam=cam, car_colors=car_colors)
end
We can save the run to a text file. We achieve this by first converting the the Trajdata type and then exporting that.
In [ ]:
listrec = convert(Trajdata, rec)
open("2Dstadium_listrec.txt", "w") do io
write(io, MIME"text/plain"(), listrec)
end
The file can be loaded in a similar way.
In [ ]:
listrec2 = open("2Dstadium_listrec.txt", "r") do io
read(io, MIME"text/plain"(), Trajdata)
end
In [ ]: