scenario generation from recorded vehicle data -凯发k8网页登录
this example shows how to generate a virtual driving scenario from recorded vehicle data. the scenario is generated from position information recorded from a gps sensor and recorded object lists processed from a lidar sensor.
overview
virtual driving scenarios can be used to recreate a real scenario from recorded vehicle data. these virtual scenarios enable you to visualize and study the original scenario. because you can programmatically modify virtual scenarios, you can also use them to synthesize scenario variations when designing and evaluating autonomous driving systems.
in this example, you create a virtual driving scenario by generating a
object from data that was recorded from a test (ego) vehicle and an asam opendrive® file. the asam opendrive file describes the road network of the area where the data was recorded. the recorded vehicle data includes:
gps data: text file containing the latitude and longitude coordinates of the ego vehicle at each timestamp.
lidar object list data: text file containing the number of non-ego actors and the positions of their centers, relative to the ego vehicle, at each timestamp.
video data: mp4 file recorded from a forward-facing monocular camera mounted on the ego vehicle.
to generate and simulate the driving scenario, you follow these steps:
explore recorded vehicle data.
import asam opendrive road network into driving scenario.
add ego vehicle data from gps to driving scenario.
add non-ego actors from lidar object list to driving scenario.
simulate and visualize generated scenario.
the following diagram shows how you use the recorded data in this example. notice that you create the driving scenario from the gps, lidar object lists, and asam opendrive files. you use the camera data to visualize the original scenario and can compare this data with the scenario you generate. you also visualize the scenario route on a map using .
explore recorded vehicle data
the positions of the ego vehicle were captured using a ublox gps neo m8n sensor. the gps sensor was placed on the center of the roof of the ego vehicle. this data is saved in the text file egourban.txt
.
the positions of the non-ego actors were captured using a velodyne® vlp-16 lidar sensor with a range of 30 meters. the vlp-16 sensor was placed on the roof of the ego vehicle at a position and height that avoids having the sensor collide with the body of the ego vehicle. the point cloud from the lidar sensor was processed on the vehicle to detect objects and their positions relative to the ego vehicle. this data is saved in the text file nonegourban.txt
.
to help understand the original scenario, video from a monocular camera was recorded as a reference. this video can also be used to visually compare the original and generated scenarios. a preview of this recorded video is saved in the video file urbanpreview.mp4
. you can download the full recorded video file from .
generate a preview of the urban traffic scenario used in this example.
vidobj = videoreader("urbanpreview.mp4"); fig = figure; set(fig,position=[0, 0, 800, 600]); movegui(fig,"center"); pnl = uipanel(fig,position=[0 0 1 1],... title="urban traffic scenario"); plt = axes(pnl); while hasframe(vidobj) vidframe = readframe(vidobj); image(vidframe,parent=plt); plt.visible = "off"; pause(1/vidobj.framerate); end
though the sensor coverage area can be defined around the entire ego vehicle, this example shows only the forward-looking scenario.
import asam opendrive road network into driving scenario
the road network data for generating the virtual scenario is obtained from ®. the openstreetmap data files are converted to asam opendrive files and saved with extension .xodr
. use the function to import this road network data to a driving scenario.
create a driving scenario object and import the desired asam opendrive road network into the generated scenario.
scenario = drivingscenario; opendrivefile = "opendriveurban.xodr"; roadnetwork(scenario,"opendrive",opendrivefile);
add ego vehicle data from gps to generated scenario
the ego vehicle data is collected from the gps sensor and stored as a text file. the text file consists of three columns that store the latitude, longitude, and timestamp values for the ego vehicle. use the helpergetegodata
function to import the ego vehicle data from the text file into a structure in the matlab® workspace. the structure contains three fields specifying the latitude, longitude and timestamps.
egofile = "egourban.txt";
egodata = helpergetegodata(egofile);
compute the trajectory waypoints of the ego vehicle from the recorded gps coordinates. use the latlon2local
function to convert the raw gps coordinates to the local east-north-up cartesian coordinates. the transformed coordinates define the trajectory waypoints of the ego vehicle.
% specify latitude and longitude at origin of data from asam opendrive file. this point will also define the origin of the local coordinate system. alt = 540.0 % average altitude in hyderabad, india
alt = 540.0000e 000
origin = [17.425853702697903, 78.44939480188313, alt]; % [lat, lon, altitude] % specify latitude and longitude of ego vehicle lat = egodata.lat; lon = egodata.lon; % compute waypoints of ego vehicle [x,y,~] = latlon2local(lat,lon,alt,origin); egowaypoints(:,1) = x; egowaypoints(:,2) = y;
visualize the gps path of the ego vehicle using the object.
zoomlevel = 17; player = geoplayer(lat(1),lon(1),zoomlevel); plotroute(player,lat,lon); for i = 1:size(lat,1) plotposition(player,lat(i),lon(i)); end
use helpercomputeegodata
to compute the speed and the heading angle values of the ego vehicle at each sensor data timestamp.
[egospeed,egoangle] = helpercomputeegodata(egodata,x,y);
add the ego vehicle to the driving scenario.
ego = vehicle(scenario,classid=1,name="ego",... length=3.6,width=1.55,height=1.6,... mesh=driving.scenario.carmesh);
create a trajectory for the ego vehicle from the computed set of ego waypoints and the speed. the ego vehicle follows the trajectory at the specified speed.
trajectory(ego,egowaypoints,egospeed);
add non-ego actors from lidar object lists to generated scenario
the non-ego actor data is collected from the lidar sensor and stored as a text file. the text file consists of five columns that store the actor ids, x
-positions, y
-positions, z
-positions and timestamp values, respectively. use the helpergetnonegodata
function to import the non-ego actor data from the text file into a structure in the matlab® workspace. the output is a structure with three fields:
actorid
- scenario-defined actor identifier, specified as a positive integer.position -
position of actor, specified as an [x y z] real vector. units are in meters.time
- timestamp of the sensor recording.
nonegoposfile = "nonegourban.txt"; nonegopropertiesfile = "nonegoproperties.txt"; [nonegodata, nonegoproperties] = ... helpergetnonegodata(nonegoposfile, nonegopropertiesfile);
use helpercomputenonegodata
to compute the trajectory waypoints and the speed of each non-ego actor at each timestamp. the trajectory waypoints are computed relative to the ego vehicle.
actors = unique(nonegodata(1).actorid); [nonegospeed, nonegowaypoints] = ... helpercomputenonegodata(egodata,... egowaypoints,nonegodata,egoangle);
determine the mesh for non-ego actor according to their class id.
for i = 1:size(nonegoproperties.classid,1) switch nonegoproperties.classid(i) case 3 nonegoproperties.mesh(i,1) = driving.scenario.bicyclemesh; case 2 nonegoproperties.mesh(i,1) = driving.scenario.truckmesh; otherwise nonegoproperties.mesh(i,1) = driving.scenario.carmesh; end end
add the non-ego actors to the driving scenario.you can populate the non-ego actors with appropriate class id, dimension, colour and mesh. create trajectories for the non-ego actors from the computed set of actor waypoints and the speed.
for i = 1:size(actors,1) actor = vehicle(scenario,classid=1,... length=nonegoproperties.length(i),... width=nonegoproperties.width(i),... height=nonegoproperties.height(i),... plotcolor=nonegoproperties.color(i,:),... mesh=nonegoproperties.mesh(i)); trajectory(actor,nonegowaypoints{i},nonegospeed{i}); end
visualize the ego vehicle and non-ego actors that you imported into the generated scenario. also visualize the corresponding trajectory waypoints of the ego vehicle and non-ego actors.
% create a custom figure window and define an axes object fig = figure; set(fig,position=[0, 0, 800, 600]); movegui(fig,"center"); hviewpnl = uipanel(fig,position=[0 0 1 1],... title="ego vehicle and actors"); hcarplt = axes(hviewpnl); % plot the generated driving scenario. plot(scenario,"parent",hcarplt); axis([270 320 80 120]); legend("imported road network","lanes","ego vehicle",... "actor 1","actor 2","actor 3","actor 4","actor 5") legend(hcarplt,"boxoff");
figure, plot(egowaypoints(:,1),egowaypoints(:,2),... color=[0 0.447 0.741],linewidth=2) hold on for i =1:size(actors,1) plot(nonegowaypoints{i}(:,1),... nonegowaypoints{i}(:,2),... color=nonegoproperties.color(i,:),linewidth=2) end axis("tight") xlabel("x (m)") ylabel("y (m)") title("computed ego vehicle and actor trajectories") legend("ego vehicle", "actor 1", "actor 2", "actor 3",... "actor 4","actor 5","location","best") hold off
simulate and visualize generated scenario
plot the scenario and the corresponding chase plot. run the simulation to visualize the generated driving scenario. the ego vehicle and the non-ego actors follow their respective trajectories.
% create a custom figure window to show the scenario and chase plot figscene = figure(name="driving scenario",... tag="scenariogenerationdemodisplay"); set(figscene,position=[0, 0, 1000, 400]); movegui(figscene,"center"); % add the chase plot hcarviewpanel = uipanel(figscene,... position=[0.5 0 0.5 1],title="chase camera view"); hcarplot = axes(hcarviewpanel); chaseplot(ego,parent=hcarplot, meshes="on"); % add the top view of the generated scenario hviewpanel = uipanel(figscene,... position=[0 0 0.5 1],title="top view"); hcarplot = axes(hviewpanel); chaseplot(ego,parent=hcarplot,meshes="on",... viewheight=120, viewpitch=90, viewlocation=[0, 0]); % run the simulation while advance(scenario) pause(0.01) end
export to asam openscenario
you can also export the scenario to asam openscenario file.
export(scenario, "openscenario", "playbackscenarioexample.xosc");
the asam openscenario file can be imported into other tools to visualise add use the same scenario.
summary
this example shows how to automatically generate a virtual driving scenario from vehicle data recorded using the gps and lidar sensors.
helper functions
helpergetegodata
this function reads the ego vehicle data from a text file and converts into a structure.
function [egodata] = helpergetegodata(egofile) %read the ego vehicle data from text file fileid = fopen(egofile); content = textscan(fileid,'%f %f %f'); fields = {'lat','lon','time'}; egodata = cell2struct(content,fields,2); fclose(fileid); end
helpergetnonegodata
this function reads the processed lidar data and non-ego actor properties in the from text files. you can convert it into a structure. the processed lidar data contains information about the position of non-ego actors, were the non-ego actor properties contains type, dimention and colour information about respective non-ego actors.
function [nonegopos, nonegoproperties] = ... helpergetnonegodata(nonegoposfile, nonegopropertiesfile) % read the processed lidar data of non-ego actors from text file. fileid1 = fopen(nonegoposfile); content = textscan(fileid1,'%d %f %f %f %f'); newcontent{1} = content{1}; newcontent{2} = [content{2} content{3} content{4}]; newcontent{3} = content{5}; fields = {'actorid','position','time'}; nonegopos = cell2struct(newcontent,fields,2); fclose(fileid1); fileid2 = fopen(nonegopropertiesfile); content = textscan(fileid2,'%d %f %f %f %f %f %f'); newcontent{1} = content{1}; newcontent{2} = content{2}; newcontent{3} = content{3}; newcontent{4} = content{4}; newcontent{5} = [content{5} content{6} content{7}]; fields = {'classid','length','width','height','color'}; nonegoproperties = cell2struct(newcontent,fields,2); fclose(fileid2); end
helpercomputeegodata
this function calculates the speed and heading angle of the ego vehicle based on the trajectory waypoints and the timestamps.
function [egospeed, egoangle] = ... helpercomputeegodata(egodata, x, y) egotime = egodata.time; timediff = diff(egotime); points = [x y]; difference = diff(points, 1); distance = sqrt(sum(difference .* difference, 2)); egospeed = distance./timediff; egoangle = atan(diff(y)./diff(x)); egoangle(end 1) = egoangle(end); egospeed(end 1) = egospeed(end); end
helpercomputenonegodata
this function calculates the speed and heading angle of each non-ego actor based on the trajectory waypoints and timestamps. the speed and heading angle are calculated relative to the ego vehicle.
function [nonegospeed, nonegowaypoints] = ... helpercomputenonegodata(... egodata, egowaypoints, nonegodata, egoangle) actors = unique(nonegodata.actorid); numactors = size(actors,1); nonegowaypoints = cell(numactors, 1); nonegospeed = cell(numactors, 1); for i = 1:numactors id = actors(i); idx = find([nonegodata.actorid] == id); actorxdata = nonegodata.position(idx,1); actorydata = nonegodata.position(idx,2); actortime = nonegodata.time(idx); actorwaypoints = [0 0]; % compute the trajectory waypoints of non-ego actor [sharedtimestamps,nonegoidx,egoidx] = ... intersect(actortime,egodata.time,"stable"); tempx = actorxdata(nonegoidx); tempy = actorydata(nonegoidx); relativex = -tempx .* cos(egoangle(egoidx)) tempy .* sin(egoangle(egoidx)); relativey = -tempx .* sin(egoangle(egoidx)) - tempy .* cos(egoangle(egoidx)); actorwaypoints(nonegoidx,1) = egowaypoints(egoidx,1) relativex; actorwaypoints(nonegoidx,2) = egowaypoints(egoidx,2) relativey; % compute the speed values of non-ego actor timediff = diff(sharedtimestamps); difference = diff(actorwaypoints, 1); distance = sqrt(sum(difference .* difference, 2)); actorspeed = distance./timediff; actorspeed(end 1) = actorspeed(end); % smooth the trajectory waypoints of non-ego actor actorwaypoints = smoothdata(actorwaypoints,"sgolay"); % store the values of trajectory waypoints and speed computed of each non-ego actor nonegowaypoints(i) = {actorwaypoints}; nonegospeed(i) = {actorspeed'}; end end
see also
apps
functions
- | |
objects
- | |