%% Export Driving Scenario to ASAM OpenSCENARIO File
% Create a driving scenario.

scenario = drivingScenario(StopTime=6);

% Import the road network from an ASAM OpenDRIVE® file into the scenario.

fileName = "parking.xodr";  
roadNetwork(scenario,"OpenDRIVE",fileName);

% Add an ego vehicle to the scenario. Set a trajectory in which the vehicle drives along the curve at varying speed.

egoVehicle = vehicle(scenario,ClassID=1);
waypoints = [-80 43; -34 29; -18 15; -10 -2; 4 -17; 38 -24; 52 -20];
speed = [50 20 20 20 20 50 50];
trajectory(egoVehicle,waypoints,speed);

% Add a non-ego actor and set it to spawn during the simulation by specifying an entry time value. Generate a trajectory for the non-ego actor.

truck = vehicle(scenario,ClassID=2,Position=[4 -17 0],EntryTime=3);
waypoints = [4 -17; 20 -24; 38 -24; 60 -16];
speed = [40 40 40 40];
trajectory(truck,waypoints,speed);

% Plot the scenario and run the simulation. Observe how the vehicle slows down as it drives along the curve.

plot(scenario,Waypoints="on");
while advance(scenario)
    pause(0.01)
end

% Export the scenario to an ASAM OpenSCENARIO® file.

export(scenario,"OpenSCENARIO","parking.xosc");

% Copyright 2020 The MathWorks, Inc.