Clear Filters
Clear Filters

Vehicle actor not despawning when specified an exit time

2 views (last 30 days)
Hello,
I have written a code to ensure that the vehicle actor defined in am Automated Driving ToolBox scenario despawns as it reaches the end of the road and then reppears on the satrt of the road to show a continuous traffci flow. Unfortuntely, when I sepcify the vehicle exit Time as carLane3(ii).ExitTime = scenario.SimulationTime; it doesn't seem to work and the vehicle does not despawn. Here is a snippet of the code which moves the vehicle and despawns it if it reaches the end of the road
for ii = 1:length(carLane1)
closestGridPoint = norm(abs(carLane1(ii).Position(2))-x_grid_aug);
[Min,idx] = min(closestGridPoint);
carSpeed = VCurrent(1,idx);
carLane1(ii).Position = carLane1(ii).Position-[0 carSpeed 0]*del_T*iter_ct;
if((norm(carLane1(ii).Position(2)-endPointLane1(2))<0.3||...
carLane1(ii).Position(2)<-Length))
carLane1(ii).ExitTime = scenario.SimulationTime;
despawnIDLane1 = [despawnIDLane1,ii];
end
end
  3 Comments
Abhishek Kashyap
Abhishek Kashyap on 11 Apr 2024
It seems that the despawning only works when you specify the trajectory of the vehicle. But I want to assign an instantaneous velocity to the cars at each sample time so I can't assign a trajectory to it. Is there a work around for it?

Sign in to comment.

Answers (1)

Ayush
Ayush on 22 May 2024
Hi Abhishek,
From the code that you have written using the Automated Driving Toolbox in MATLAB R2024a, it seems that you have focused on updating the vehicle positions while tracking their exit times when they are reaching the end of the road as per your expectations.
Although this approach is in the right direction, an additional emphasis on the vehicle simulation environment (basically the looping mechanism) as well as the despawning and respawning mechanisms to ensure the desired result. Here are some possible modifications to the provided code for the same:
1. Identifying the vehicles that reach the end of the road and marking their index in the “despawnIDLane1” array to allow for batch processing of vehicles that are to be respawned for efficiency within simulation. Here is a code snippet for your reference:
% Check if the car has reached the end of the lane and update ExitTime
if carLane1(ii).Position(2) <= endPointLane(2) && isinf(carLane1(ii).ExitTime)
carLane1(ii).ExitTime = scenarioSimulationTime;
despawnIDLane1 = [despawnIDLane1, ii]; % Mark for despawn
end
2. Ensuring dynamic respawn logic where instead of instantaneous update of position and exit time, the respawning mechanism is separated in a distinct phase (new loop) to offer finer control over simulation. Each vehicle in despawnIDLane1 is reset to its starting position and “ExitTime” is set to “Inf”. Here is a code snippet for your reference:
% Process despawning and respawning
for idx = despawnIDLane1
% Respawn logic: reset position and ExitTime for despawned cars
carLane1(idx).Position = startPointLane; % Reset position to start
carLane1(idx).ExitTime = Inf; % Reset ExitTime indicating they are back in the simulation
end
3. Resetting the state after each despawn and respawn cycle is crucial to avoid any duplication in handling vehicle states and its visualisations. This is done by clearing the “despawnIDLane1” array. Here is a code snippet for your reference:
despawnIDLane1 = []; % Clear despawn list for the next iteration
% Update simulation time
scenarioSimulationTime = scenarioSimulationTime + del_T;
Additionally, I have attached an example video that shows the result captured in my system from the above modifications.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!