How can I find the distance travelled by the actor at each Sample Time in Driving Scenario Designer?

3 visualizzazioni (ultimi 30 giorni)
As I am new to matlab, its taking a while to get the hang of it. Below is the code in which I have added a egoCar and I want to get the distance travelled by the egoCar at each Sample Time until the car reaches its destination waypoint. As I'm a beginner can anyone explain the solution.PLEASE
function [scenario, egoCar] = createDrivingScenario()
scenario = drivingScenario('StopTime', 10);
roadCenters = [7.1 14.5 0;
31.2 3.1 0;
42.2 -19.5 0];
bankAngle = 1;
laneSpecification = lanespec(2, 'Width', 3.925);
road(scenario, roadCenters, bankAngle, 'Lanes', laneSpecification);
egoCar = vehicle(scenario, ...
'ClassID', 1, ...
'Position', [11.7 15 0]);
waypoints = [11.7 15 0;
20.3 12 0;
25.5 9.2 0;
29.5 6.2 0;
33.6 2.3 0;
37.7 -3.1 0;
40.2 -7.7 0;
42.6 -13.7 0];
speed = [28;32;35;27;38;32;30;30];
trajectory(egoCar, waypoints, speed);
rec = record(scenario);
rec(end).SimulationTime(1)

Risposte (1)

Rahul
Rahul il 26 Giu 2023
According to the information shared by you, a possible way to find the distance travelled by the car can be done using the velocity of the egoCar and leveraging the sample time to find the distance travelled at each time interval and take it's cumulative sum to get the total distance travelled.
Below is the change you can make to your code
function [scenario, egoCar] = createDrivingScenario()
scenario = drivingScenario('StopTime', 10);
roadCenters = [7.1 14.5 0;
31.2 3.1 0;
42.2 -19.5 0];
bankAngle = 1;
laneSpecification = lanespec(2, 'Width', 3.925);
road(scenario, roadCenters, bankAngle, 'Lanes', laneSpecification);
egoCar = vehicle(scenario, ...
'ClassID', 1, ...
'Position', [11.7 15 0]);
waypoints = [11.7 15 0;
20.3 12 0;
25.5 9.2 0;
29.5 6.2 0;
33.6 2.3 0;
37.7 -3.1 0;
40.2 -7.7 0;
42.6 -13.7 0];
speed = [28;32;35;27;38;32;30;30];
trajectory(egoCar, waypoints, speed);
rec = record(scenario);
simulationTime = [rec.SimulationTime];
distanceTraveled = zeros(size(simulationTime));
for i = 2:length(simulationTime)
dt = simulationTime(i) - simulationTime(i-1);
velocityMagnitude = norm(egoCar.Velocity);
distanceTraveled(i) = distanceTraveled(i-1) + velocityMagnitude * dt;
end
disp(distanceTraveled);
rec(end).SimulationTime(1)
The distanceTravelled array will have the distance travelled by the vehicle at each sample time.
Hope this solves your query.

Community Treasure Hunt

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

Start Hunting!

Translated by