%% Generate Radar Detections of Occluded Targets
% Model the effects of occlusion when generating radar detections from a radarDetectionGenerator System object™.
% Create two cars. Position the first car 40 meters away from the sensor. Position the second car 10 meters directly behind the first car.

car1 = struct('ActorID',1,'Position',[40 0 0]);
car2 = struct('ActorID',2,'Position',[50 0 0]);

%
% Create a radar detection generator System object, radarSensor, with default values. Use the System object to generate detections.

radarSensor = radarDetectionGenerator;
simTime = 0; % start of simulation
[dets,numValidDets] = radarSensor([car1 car2],simTime);

%
% Display the coverage area of the radar detection generator on a bird's-eye plot.

bep = birdsEyePlot('XLim',[0 60],'YLim',[-15 15]);
caPlotter = coverageAreaPlotter(bep,'DisplayName', ...
    'Radar coverage area');
plotCoverageArea(caPlotter,radarSensor.SensorLocation, ...
    radarSensor.MaxRange,radarSensor.Yaw, ...
    radarSensor.FieldOfView(1));

%
% Extract the (X,Y) positions of the targets by converting the (X,Y) values of the Measurement field of the cell array into a MATLAB array. Then, display the detections.

if numValidDets > 0
    detPlotter = detectionPlotter(bep,'DisplayName','Radar detections');
    detPos = cellfun(@(d)d.Measurement(1:2),dets,'UniformOutput',false);
    detPos = cell2mat(detPos')';
    plotDetection(detPlotter,detPos)
end

%
% By default, the radar detection generator excludes targets that are occluded by other objects. Therefore, the radar detects the nearest target but not the target directly behind it. To include the occluded target in the detections, release the radar detection generator, disable line-of-sight occlusion, and generate detections again. Display the detections.

release(radarSensor)
radarSensor.HasOcclusion = false;
[detsNoOcclusion,numValidDets] = radarSensor([car1 car2],simTime);
if numValidDets > 0
    detPos = cellfun(@(d)d.Measurement(1:2),detsNoOcclusion,'UniformOutput',false);
    detPos = cell2mat(detPos')';
    plotDetection(detPlotter, detPos)
end

% Release the radar detection generator.

release(radarSensor)

% Copyright 2012 The MathWorks, Inc.