How to plot two driving scenarios togather
2 views (last 30 days)
Show older comments
Greetings,
if i have two driving scenarios:
scenario1 = drivingScenario('SampleTime', 0.05);
scenario2 = drivingScenario('SampleTime', 0.05);
after creating roads and vehicles, how do i plot them in the same figure? becuase plot(scenario1) generates an independent figure with the plot and road in it
for example, i tried this but it doesnt work:
figure
plot(scenario1)
hold on
plot(scenario2)
and matlab actually generates two new plots beside figure itself and hold on has no meaning too
3 Comments
Greg Dionne
on 29 Nov 2023
Have you tried using the same handle?
% Create new scenarios
scenario1 = drivingScenario;
scenario2 = drivingScenario;
% make a shared axes object
hSharedAxes = axes;
% add a plot for debug
plot(scenario1,Parent=hSharedAxes);
plot(scenario2,Parent=hSharedAxes);
% add a straight road segment 25 m in length. cross-wise in each
% scenario
road(scenario1, [0 0 0; 25 0 0]);
road(scenario2, [0 0 0; 0 25 0]);
% add a vehicle to each one
v1 = vehicle(scenario1);
v2 = vehicle(scenario2);
% tell them to follow a trajectory along the road at 20 m/s.
smoothTrajectory(v1,[v1.RearOverhang 0 0; 25-v1.Length+v1.RearOverhang 0 0], 20)
smoothTrajectory(v2,[0 v2.RearOverhang 0; 0 25-v1.Length+v1.RearOverhang 0], 20)
% Start the simulation loop
while advance(scenario1) && advance(scenario2)
% let plot update
pause(0.1)
end
Granted, the scenarios will be distinct, so the roads won't automatically merge into intersections and whatnot.
Were you hoping to merge two scenarios into one or something like that?
Answers (1)
Harimurali
on 8 Nov 2023
Hi Abdullah,
I understand that you want to plot two driving scenarios on the same plot to implement a collision avoidance simulation.
In this case, MATLAB does not support plotting two or more driving scenarios on the same plot. A collision avoidance simulation can be done using a single driving scenario by creating two vehicles in the same scenario and simulating their collision.
Please refer to the “Create Driving Scenario with Multiple Actors and Roads” section of the following documentation for more information about creating a driving scenario with multiple actors: https://www.mathworks.com/help/driving/ref/drivingscenario.vehicle.html
Here is a sample MATLAB code that implements a simple collision avoidance simulation:
% Create a driving scenario
scenario = drivingScenario(SampleTime=0.1, StopTime=10);
% Add a straight road
roadCenters = [0 0; 100 0];
road(scenario, roadCenters);
% Add the ego vehicle
ego = vehicle(scenario, 'Position', [0 -2 0]);
% Add another vehicle
other = vehicle(scenario, 'Position', [20 -2 0]);
% Specify the trajectories of the vehicles
trajectory(ego, [0 -2 0; 100 -2 0], [0 30]);
trajectory(other, [20 -2 0; 100 -2 0], [0 15]);
safeDistance = 5;
plot(scenario,'Waypoints','on')
while advance(scenario)
pause(0.2)
% Get the positions of the ego vehicle and the other vehicle
egoPos = ego.Position;
otherPos = other.Position;
% Calculate the distance between the vehicles
distance = norm(egoPos - otherPos);
% Change the trajectory of the ego vehicle depending on the current position
if egoPos(1) < otherPos(1) && distance < safeDistance && ego.Velocity(1) ~= 0
trajectory(ego, [egoPos(1), egoPos(2), egoPos(3); otherPos(1), egoPos(2), egoPos(3)], [ego.Velocity(1) 0]);
end
end
Please refer to the following documentation for more information about the “trajectory” function:
Hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!