• 毫米波雷达和视觉传感器融合的检测仿真代码


    一、构建驾驶场景

    驾驶场景为两车道的高速公路,长度为500m,共有四辆车。主车正前方和正后方各有一辆行使的车辆,还有一辆从左侧超车的车辆。所有车辆按设定的路径和车速行驶;超车车辆从右侧车道开始,变道至坐车到超车后又返回右车道。

    1. scenario=drivingScenario; %构建驾驶场景
    2. scenario.SampleTime=0.01;
    3. roadCenters=[0 0;50 0;100 0;250 20;500 40];%道路中心
    4. roadWidth=7.2;%道路宽度
    5. road(scenario,roadCenters,roadWidth);%在驾驶场景中添加道路
    6. egoCar=vehicle(scenario,"ClassID",1);%添加主车
    7. path(egoCar,roadCenters(2:end,:)-[0 1.8],25)%主车路径的路径和车速
    8. leadCar=vehicle(scenario,"ClassID",1);%添加前方车辆
    9. path(leadCar,[70 0;roadCenters(3:end,:)]-[0 1.8],25);%前方车辆的路径和车速
    10. passingCar=vehicle(scenario,"ClassID",1);%添加超车车辆
    11. waypoints=[0 -1.8;50 1.8;100 1.8;250 21.8;400 32.2;500 38.2];%设置路线点
    12. path(passingCar,waypoints,35);%超车车辆的路径和车速
    13. chaseCar=vehicle(scenario,"ClassID",1);%添加后方车辆
    14. path(chaseCar,[25 0;roadCenters(2:end,:)]-[0 1.8],25);%后方车辆的路径和车速
    15. plot(scenario)

    所得如下结果

    二、定义毫米波雷达和视觉传感器

    本示例中,主车带有六个毫米波雷达和两个视觉传感器,传感器的检测范围为360°。传感器检测区域有一些重叠和覆盖 。主车前后各装一个远程毫米波雷达传感器和一个视觉传感器;两侧分别装有两个短程毫米波雷达,每个短程毫米波雷达可覆盖90°检测范围,其中一个毫米波雷达覆盖车辆中部到后部区域,另一个毫米波雷达覆盖车辆中部到前部区域。

    1. sensors=cell(8,1);
    2. %设置位于汽车前保险杠中央的前向远程毫米波雷达
    3. sensors{1}=radarDetectionGenerator("SensorIndex",1,"Height",0.2,"MaxRange",174,...
    4. "SensorLocation",[egoCar.Wheelbase+ egoCar.FrontOverhang,0],"FieldOfView",[20,5]);
    5. %设置位于汽车后保险杠中央的前向远程毫米波雷达
    6. sensors{2}=radarDetectionGenerator("SensorIndex",2,"Height",0.2,"Yaw",180,...
    7. "SensorLocation",[-egoCar.RearOverhang,0],"MaxRange",174,"FieldOfView",[20,5]);
    8. %设置车辆左后轮罩处的左后短程毫米波雷达
    9. sensors{3}=radarDetectionGenerator("SensorIndex",3,"Height",0.2,"Yaw",120,...
    10. "SensorLocation",[0,egoCar.Width/2],"MaxRange",30,"ReferenceRange",50,...
    11. "FieldOfView",[90,5],"AzimuthResolution",10,"RangeResolution",1.25);
    12. %设置车辆右后轮罩处的右后短程毫米波雷达
    13. sensors{4}=radarDetectionGenerator("SensorIndex",4,"Height",0.2,"Yaw",-120,...
    14. "SensorLocation",[0,-egoCar.Width/2],"MaxRange",30,"ReferenceRange",50,...
    15. "FieldOfView",[90,5],"AzimuthResolution",10,"RangeResolution",1.25);
    16. %设置车辆左前轮罩处的左前短程毫米波雷达
    17. sensors{5}=radarDetectionGenerator("SensorIndex",5,"Height",0.2,"Yaw",60,...
    18. "SensorLocation",[egoCar.Wheelbase,egoCar.Width/2],"MaxRange",30,...
    19. "ReferenceRange",50,"FieldOfView",[90,5],"AzimuthResolution",10,...
    20. "RangeResolution",1.25);
    21. %设置车辆右前轮罩处的右前短程毫米波雷达
    22. sensors{6}=radarDetectionGenerator("SensorIndex",6,"Height",0.2,"Yaw",-60,...
    23. "SensorLocation",[egoCar.Wheelbase,-egoCar.Width/2],"MaxRange",30,...
    24. "ReferenceRange",50,"FieldOfView",[90,5],"AzimuthResolution",10,...
    25. "RangeResolution",1.25);
    26. %设置前挡风玻璃上的前视觉传感器
    27. sensors{7} = visionDetectionGenerator('SensorIndex', 7, 'FalsePositivesPerImage', 0.1, ...
    28. 'SensorLocation', [0.75*egoCar.Wheelbase 0], 'Height', 1.1);
    29. %设置后挡风玻璃上的后视觉传感器
    30. sensors{8} = visionDetectionGenerator('SensorIndex', 8, 'FalsePositivesPerImage', 0.1, ...
    31. 'SensorLocation', [0.2*egoCar.Wheelbase 0], 'Height', 1.1, 'Yaw', 180);

     三、创建跟踪器

    1. tracker = multiObjectTracker('FilterInitializationFcn', @initSimDemoFilter, ...
    2. 'AssignmentThreshold', 30, 'ConfirmationThreshold', [4 5]);%多目标跟踪器
    3. positionSelector = [1 0 0 0; 0 0 1 0]; % 传感器位置
    4. velocitySelector = [0 1 0 0; 0 0 0 1]; % 传感器速度
    5. BEP=createDemoDisplay(egoCar,sensors);%创建显示并返回鸟瞰图的句柄
    6. imshow(BEP)

    四、辅助函数

    initSimDemoFilter
    该函数初始化一个基于单个检测目标的固定速度的卡尔曼滤波器。

    1. function filter = initSimDemoFilter(detection)
    2. % Use a 2-D constant velocity model to initialize a trackingKF filter.
    3. % The state vector is [x;vx;y;vy]
    4. % The detection measurement vector is [x;y;vx;vy]
    5. % As a result, the measurement model is H = [1 0 0 0; 0 0 1 0; 0 1 0 0; 0 0 0 1]
    6. H = [1 0 0 0; 0 0 1 0; 0 1 0 0; 0 0 0 1];
    7. filter = trackingKF('MotionModel', '2D Constant Velocity', ...
    8. 'State', H' * detection.Measurement, ...
    9. 'MeasurementModel', H, ...
    10. 'StateCovariance', H' * detection.MeasurementNoise * H, ...
    11. 'MeasurementNoise', detection.MeasurementNoise);
    12. end

    createDemoDisplay
    该函数生成一个带有三个面板的显示界面:

    1、左上方显示:跟随自车的俯视图;

    2、左下方显示:跟踪自车的摄像头图像;

    3、右半部分:鸟瞰图显示;

    1. function BEP = createDemoDisplay(egoCar, sensors)
    2. % Make a figure
    3. hFigure = figure('Position', [0, 0, 1200, 640], 'Name', 'Sensor Fusion with Synthetic Data Example');
    4. movegui(hFigure, [0 -1]); % Moves the figure to the left and a little down from the top
    5. % Add a car plot that follows the ego vehicle from behind
    6. hCarViewPanel = uipanel(hFigure, 'Position', [0 0 0.5 0.5], 'Title', 'Chase Camera View');
    7. hCarPlot = axes(hCarViewPanel);
    8. chasePlot(egoCar, 'Parent', hCarPlot);
    9. % Add a car plot that follows the ego vehicle from a top view
    10. hTopViewPanel = uipanel(hFigure, 'Position', [0 0.5 0.5 0.5], 'Title', 'Top View');
    11. hCarPlot = axes(hTopViewPanel);
    12. chasePlot(egoCar, 'Parent', hCarPlot, 'ViewHeight', 130, 'ViewLocation', [0 0], 'ViewPitch', 90);
    13. % Add a panel for a bird's-eye plot
    14. hBEVPanel = uipanel(hFigure, 'Position', [0.5 0 0.5 1], 'Title', 'Bird''s-Eye Plot');
    15. % Create bird's-eye plot for the ego vehicle and sensor coverage
    16. hBEVPlot = axes(hBEVPanel);
    17. frontBackLim = 60;
    18. BEP = birdsEyePlot('Parent', hBEVPlot, 'Xlimits', [-frontBackLim frontBackLim], 'Ylimits', [-35 35]);
    19. % Plot the coverage areas for radars
    20. for i = 1:6
    21. cap = coverageAreaPlotter(BEP,'FaceColor','red','EdgeColor','red');
    22. if isa(sensors{i},'drivingRadarDataGenerator')
    23. plotCoverageArea(cap, sensors{i}.MountingLocation(1:2),...
    24. sensors{i}.RangeLimits(2), sensors{i}.MountingAngles(1), sensors{i}.FieldOfView(1));
    25. else
    26. plotCoverageArea(cap, sensors{i}.SensorLocation,...
    27. sensors{i}.MaxRange, sensors{i}.Yaw, sensors{i}.FieldOfView(1));
    28. end
    29. end
    30. % Plot the coverage areas for vision sensors
    31. for i = 7:8
    32. cap = coverageAreaPlotter(BEP,'FaceColor','blue','EdgeColor','blue');
    33. if isa(sensors{i},'drivingRadarDataGenerator')
    34. plotCoverageArea(cap, sensors{i}.MountingLocation(1:2),...
    35. sensors{i}.RangeLimits(2), sensors{i}.MountingAngles(1), 45);
    36. else
    37. plotCoverageArea(cap, sensors{i}.SensorLocation,...
    38. sensors{i}.MaxRange, sensors{i}.Yaw, 45);
    39. end
    40. end
    41. % Create a vision detection plotter put it in a struct for future use
    42. detectionPlotter(BEP, 'DisplayName','vision', 'MarkerEdgeColor','blue', 'Marker','^');
    43. % Combine all radar detections into one entry and store it for later update
    44. detectionPlotter(BEP, 'DisplayName','radar', 'MarkerEdgeColor','red');
    45. % Add road borders to plot
    46. laneMarkingPlotter(BEP, 'DisplayName','lane markings');
    47. % Add the tracks to the bird's-eye plot. Show last 10 track updates.
    48. trackPlotter(BEP, 'DisplayName','track', 'HistoryDepth',10);
    49. axis(BEP.Parent, 'equal');
    50. xlim(BEP.Parent, [-frontBackLim frontBackLim]);
    51. ylim(BEP.Parent, [-40 40]);
    52. % Add an outline plotter for ground truth
    53. outlinePlotter(BEP, 'Tag', 'Ground truth');
    54. end

    updateBEP
    更新鸟瞰图,包括道路边界,雷达检测点和路径。

    1. function updateBEP(BEP, egoCar, detections, confirmedTracks, psel, vsel)
    2. % Update road boundaries and their display
    3. [lmv, lmf] = laneMarkingVertices(egoCar);
    4. plotLaneMarking(findPlotter(BEP,'DisplayName','lane markings'),lmv,lmf);
    5. % update ground truth data
    6. [position, yaw, length, width, originOffset, color] = targetOutlines(egoCar);
    7. plotOutline(findPlotter(BEP,'Tag','Ground truth'), position, yaw, length, width, 'OriginOffset', originOffset, 'Color', color);
    8. % update barrier data
    9. [bPosition,bYaw,bLength,bWidth,bOriginOffset,bColor,numBarrierSegments] = targetOutlines(egoCar, 'Barriers');
    10. plotBarrierOutline(findPlotter(BEP,'Tag','Ground truth'),numBarrierSegments,bPosition,bYaw,bLength,bWidth,...
    11. 'OriginOffset',bOriginOffset,'Color',bColor);
    12. % Prepare and update detections display
    13. N = numel(detections);
    14. detPos = zeros(N,2);
    15. isRadar = true(N,1);
    16. for i = 1:N
    17. detPos(i,:) = detections{i}.Measurement(1:2)';
    18. if detections{i}.SensorIndex > 6 % Vision detections
    19. isRadar(i) = false;
    20. end
    21. end
    22. plotDetection(findPlotter(BEP,'DisplayName','vision'), detPos(~isRadar,:));
    23. plotDetection(findPlotter(BEP,'DisplayName','radar'), detPos(isRadar,:));
    24. % Remove all object tracks that are unidentified by the vision detection
    25. % generators before updating the tracks display. These have the ObjectClassID
    26. % parameter value as 0 and include objects such as barriers.
    27. isNotBarrier = arrayfun(@(t)t.ObjectClassID,confirmedTracks)>0;
    28. confirmedTracks = confirmedTracks(isNotBarrier);
    29. % Prepare and update tracks display
    30. trackIDs = {confirmedTracks.TrackID};
    31. labels = cellfun(@num2str, trackIDs, 'UniformOutput', false);
    32. [tracksPos, tracksCov] = getTrackPositions(confirmedTracks, psel);
    33. tracksVel = getTrackVelocities(confirmedTracks, vsel);
    34. plotTrack(findPlotter(BEP,'DisplayName','track'), tracksPos, tracksVel, tracksCov, labels);
    35. end

    五、驾驶场景仿真

    以下循环移动车辆,调用传感器仿真,进行跟踪。
    注意场景生成和传感器仿真可以不同步,明确不同时间步分布用于场景仿真和传感器仿真可以实现场景仿真和传感器仿真解耦。这对于构建高精度执行器运动模型估计车辆运动,独立于传感器测量速度有好处。
    另一个例子是传感器有不同的更新频率,假设某一传感器更新频率为20ms,而另一传感器更新频率50ms,你可以规定场景的更新频率为10ms,则传感器可以在适当的时间更新数据。
    本示例中,场景生成频率为0.01s,传感器检测频率为0.1s,传感器返回一个isValidTime的标志位,指示如果传感器生成检测则该标志位为true,这个标志位可以用于跟踪器的调用(有检测数据时)。
    另外需要说明,传感器可以模拟目标物的多个检测点,尤其是当目标物距离传感器非常近的时候。由于跟踪器multiObjectTracker只能跟踪1个目标物的1个检测点,因此跟踪器处理前需要对雷达反射点进行聚类。这将通过设置TargetReportFormat为默认的Clustered detections模式实现。传感器模型可以输出原始的检测数据,或使用内部跟踪器的跟踪数据。

    1. toSnap = true;
    2. while advance(scenario) && ishghandle(BEP.Parent)
    3. % 获取场景时间
    4. time = scenario.SimulationTime;
    5. % 获取另一辆车在主车坐标系中的位置
    6. ta = targetPoses(egoCar);
    7. % 传感器仿真
    8. detectionClusters = {};
    9. isValidTime = false(1,8);
    10. for i = 1:8
    11. [sensorDets,numValidDets,isValidTime(i)] = sensors{i}(ta, time);
    12. if numValidDets
    13. for j = 1:numValidDets
    14. % Vision detections do not report SNR. The tracker requires
    15. % that they have the same object attributes as the radar
    16. % detections. This adds the SNR object attribute to vision
    17. % detections and sets it to a NaN.
    18. if ~isfield(sensorDets{j}.ObjectAttributes{1}, 'SNR')
    19. sensorDets{j}.ObjectAttributes{1}.SNR = NaN;
    20. end
    21. % Remove the Z-component of measured position and velocity
    22. % from the Measurement and MeasurementNoise fields
    23. sensorDets{j}.Measurement = sensorDets{j}.Measurement([1 2 4 5]);
    24. sensorDets{j}.MeasurementNoise = sensorDets{j}.MeasurementNoise([1 2 4 5],[1 2 4 5]);
    25. end
    26. detectionClusters = [detectionClusters; sensorDets]; %#ok
    27. end
    28. end
    29. % Update the tracker if there are new detections
    30. if any(isValidTime)
    31. if isa(sensors{1},'drivingRadarDataGenerator')
    32. vehicleLength = sensors{1}.Profiles.Length;
    33. else
    34. vehicleLength = sensors{1}.ActorProfiles.Length;
    35. end
    36. confirmedTracks = updateTracks(tracker, detectionClusters, time);
    37. % Update bird's-eye plot
    38. updateBEP(BEP, egoCar, detectionClusters, confirmedTracks, positionSelector, velocitySelector);
    39. end
    40. % Snap a figure for the document when the car passes the ego vehicle
    41. if ta(1).Position(1) > 0 && toSnap
    42. toSnap = false;
    43. snapnow
    44. end
    45. end

    点击Run,驾驶场景开始运动。得到输出结果

  • 相关阅读:
    工信部等四部门印发重要标准化指南,引领人工智能产业高质量发展
    java毕业设计花苑物业综合服务平台mybatis+源码+调试部署+系统+数据库+lw
    【办公类-04-04】华为助手导出照片视频分类(根据图片、视频的文件名日期导入“年-月-日”文件夹中,并转移到“年-月”文件中整理、转移到“年”文件夹中整理)
    手写实现call() apply() bind()函数,附有详细注释,包含this指向、arguments讲解
    Go语言用Resty库编写的音频爬虫代码
    常用的Lambda操作
    争论不休的一个话题:金额到底是用Long还是BigDecimal?
    Redis实现分布式锁
    An工具介绍之骨骼工具
    QtCreator按顺序编译多个子项目
  • 原文地址:https://blog.csdn.net/m0_64346597/article/details/126189705