I have a route in SUMO which has a car and a traffic light, now I want to get the distance from the car to the traffic light and I wrote this code with the help of Traci4matlab library in Level-2 MATLAB S -Function block, now I run it , it gives a graph that doesn't look like reality. Please help me to get a command to detect the distance from the car to the traffic light.
I have a route in SUMO that has a car and a traffic light, now I want to get the distance from the car to the traffic light, and I wrote this code in Level-2 MATLAB S-Faunction, now when I run it, it gives a graph that is not similar to reality
function mdlInitializeSizes(block)
% Specify the number of input and output ports
block.NumInputPorts = 1;
block.NumOutputPorts = 2; % Additional output port for vehicle position
% Specify the size and type of the input and output ports
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DataTypeID = 0; % double data type
block.InputPort(1).Dimensions = []; % Allow any dimensions
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).DataTypeID = 0; % double data type
block.OutputPort(1).Dimensions = []; % Allow any dimensions
block.OutputPort(1).SamplingMode = 'Sample'; % Set the frame status to known
block.OutputPort(2).Complexity = 'Real'; % New output port for vehicle position
block.OutputPort(2).DataTypeID = 0; % double data type
block.OutputPort(2).Dimensions = []; % Allow any dimensions
block.OutputPort(2).SamplingMode = 'Sample'; % Set the frame status to known
% Set the sample time of the block (-1 means inherited)
block.SampleTimes = [-1 0];
% Register the mdlInitializeConditions, mdlUpdate, and mdlTerminate methods
block.RegBlockMethod('InitializeConditions', @mdlInitializeConditions);
block.RegBlockMethod('Update', @mdlUpdate);
block.RegBlockMethod('Terminate', @mdlTerminate);
block.RegBlockMethod('SetInputPortSamplingMode', @mdlSetInputPortSamplingMode); % Add this line
end
function mdlSetInputPortSamplingMode(block, portIdx, samplingMode)
% Set the sampling mode of the first input port
if portIdx == 1
block.InputPort(portIdx).SamplingMode = samplingMode;
end
end
function mdlInitializeConditions(~)
% Initialize any necessary variables or states
import traci.constants;
% Start the SUMO simulation
traci.start('sumo-gui -c ./config_file.sumocfg --start');
end
function mdlUpdate(block)
% Get the input value (desired speed)
if isnumeric(block.InputPort(1).Data)
desiredspeed = double(block.InputPort(1).Data);
else
error('Input data is not a numeric value');
end
% Import Traci4Matlab constants
import traci.constants;
% Advance the simulation step
traci.simulationStep();
% Get the vehicle ID list
vehicleID = traci.vehicle.getIDList();
% Set the desired speed for the first vehicle
traci.vehicle.setSpeed(char(vehicleID), desiredspeed);
% Get the current speed of the first vehicle
currentSpeed = traci.vehicle.getSpeed(char(vehicleID));
% Get the position of the first vehicle
position = traci.vehicle.getPosition(char(vehicleID));
% Get the IDs of all traffic lights
trafficLights = traci.trafficlights.getIDList();
% Initialize variables for tracking the closest traffic light
closestLightID = '';
closestDistance = Inf;
% Iterate over all traffic lights to find the closest one
for i = 1:length(trafficLights)
% Get the IDs of all lanes associated with the current traffic light
lanes = traci.trafficlights.getControlledLanes(char(trafficLights(i)));
% Iterate over all lanes to find the first lane's position
for j = 1:length(lanes)
% Get the position of the lane area corners
laneAreaCorners = traci.lane.getShape(char(lanes(j)));
% Convert lane area corners to numeric array
laneAreaCorners = cell2mat(laneAreaCorners);
% Calculate the average position of the lane
lanePosition = mean(laneAreaCorners);
% Calculate the distance between the vehicle and the current lane
distance = norm(position - lanePosition);
% Update the closest traffic light information if necessary
if distance < closestDistance
closestDistance = distance;
closestLightID = char(trafficLights(i));
end
% Break the loop after finding the position of the first lane
%break
end
end
% Set the output values
block.OutputPort(1).Data = double(currentSpeed(1)); % Assign the first element of the current Speed vector
block.OutputPort(2).Data = double(closestDistance);
end
function mdlTerminate(~)
% Clean up any resources, if necessary
% Close the SUMO simulation
traci.close();
end