智能驾驶——prm算法

204 阅读2分钟

前言

本文已参与「新人创作礼」活动,一起开启掘金创作之路。这是老早之前看智能驾驶相关的内容时学习的算法,除了PRM算法,还有A*算法都是智能驾驶常用的寻路算法。 路径规划作为机器人完成各种任务的基础,一直是研究的热点。研究人员提出了许多规划方法:如人工势场法、单元分解法、随机路标图(PRM)法、快速搜索树(RRT)法等。传统的人工势场、单元分解法需要对空间中的障碍物进行精确建模,当环境中的障碍物较为复杂时,将导致规划算法计算量较大。基于随机采样技术的PRM法可以有效解决高维空间和复杂约束中的路径规划问题。

  PRM是一种基于图搜索的方法,它将连续空间转换成离散空间,再利用A*等搜索算法在路线图上寻找路径,以提高搜索效率。这种方法能用相对少的随机采样点来找到一个解,对多数问题而言,相对少的样本足以覆盖大部分可行的空间,并且找到路径的概率为1(随着采样数增加,P(找到一条路径)指数的趋向于1)。显然,当采样点太少,或者分布不合理时,PRM算法是不完备的,但是随着采用点的增加,也可以达到完备。所以PRM是概率完备且不最优的。

matlab代码

function simpleTest()
    disp('Program started');
    vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
    vrep.simxFinish(-1);      % just in case, close all opened connections
    clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);

if (clientID>-1)
    disp('Connected to remote API server');
        
	[returnCode,sensorHandle] = vrep.simxGetObjectHandle(clientID,'Vision_sensor',vrep.simx_opmode_blocking);
	[returnCode,objectHandle] = vrep.simxGetObjectHandle(clientID,'start',vrep.simx_opmode_blocking);
	[returnCode,goalHandle] =   vrep.simxGetObjectHandle(clientID,'goal',vrep.simx_opmode_blocking);

	% Retrieves the image of a vision sensor as an image array(each image pixel is a byte (greyscale image))
	[returnCode,resolution,image] = vrep.simxGetVisionSensorImage2(clientID, sensorHandle, 1, vrep.simx_opmode_blocking);
	
	% Creates a BinaryOccupancyGrid object with resolution specified in cells per meter.
	width = 5; 	height = 5; 	  % Map width / height, specified as a double in meters.
	grid = robotics.BinaryOccupancyGrid(image, resolution(1) / width);
	grid.GridLocationInWorld = [-width/2, -height/2]; % world coordinates of the bottom-left corner of the grid
	
	% Inflate the Map Based on Robot Dimension
	inflate(grid, 0.1);
	
	% Create a roadmap with 200 nodes and calculate the path
	prm = robotics.PRM(grid, 200);
	prm.ConnectionDistance = 1;

	[returnCode,startLocation] = vrep.simxGetObjectPosition(clientID,objectHandle,-1,vrep.simx_opmode_blocking); 
	[returnCode,endLocation] =   vrep.simxGetObjectPosition(clientID,goalHandle,  -1,vrep.simx_opmode_blocking);

	path = findpath(prm, double(startLocation(1:2)), double(endLocation(1:2)));
	show(prm)
	
	% Simply jump through the path points, no interpolation here:
	for i=1 : size(path,1)
	  pos = [path(i,:), 0.05];
	  vrep.simxSetObjectPosition(clientID, objectHandle, -1, pos, vrep.simx_opmode_blocking);
	  pause(0.5);
	end


    % Now close the connection to V-REP:    
    vrep.simxFinish(clientID);
else
    disp('Failed connecting to remote API server');
end
vrep.delete(); % call the destructor!

disp('Program ended');

end

结语

具体大家可以参考