RRT是一种多维空间中有效率的规划方法。它以一个初始点作为根节点,通过随机采样增加叶子节点的方式,生成一个随机扩展树,当随机树中的叶子节点包含了目标点或进入了目标区域,便可以在随机树中找到一条由从初始点到目标点的路径。RRT方法是概率完备且不最优的。
function BuildRRT(qinit, K, Δq)T.init(qinit)for k = 1 to Kqrand = Sample() -- chooses a random configurationqnearest = Nearest(T, qrand) -- selects the node in the RRT tree that is closest to qrandif Distance(qnearest, qgoal) < Threshold thenreturn trueqnew = Extend(qnearest, qrand, Δq) -- moving from qnearest an incremental distance in the direction of qrandif qnew ≠ NULL thenT.AddNode(qnew)return falsefunction Sample() -- Alternatively,one could replace Sample with SampleFree(by using a collision detection algorithm to reject samples in C_obstaclep = Random(0, 1.0)if 0 < p < Prob thenreturn qgoalelseif Prob < p < 1.0 thenreturn RandomNode()
RRT 伪代码
初始化时随机树T只包含一个节点:根节点qinit。首先Sample函数从状态空间中随机选择一个采样点qrand(4行);然后Nearest函数从随机树中选择一个距离qrand最近的节点qnearest(5行);最后Extend函数通过从qnearest向qrand扩展一段距离,得到一个新的节点qnew(8行)。如果qnew与障碍物发生碰撞,则Extend函数返回空,放弃这次生长,否则将qnew加入到随机树中。重复上述步骤直到qnearest和目标点qgaol距离小于一个阈值,则代表随机树到达了目标点,算法返回成功(6~7行)。为了使算法可控,可以设定运行时间上限或搜索次数上限(3行)。如果在限制次数内无法到达目标点,则算法返回失败。
为了加快随机树到达目标点的速度,简单的改进方法是:在随机树每次的生长过程中,根据随机概率来决定qrand是目标点还是随机点。在Sample函数中设定参数Prob,每次得到一个0到1.0的随机值p,当0<p<Prob的时候,随机树朝目标点生长行;当Prob<p<1.0时,随机树朝一个随机方向生长。
上述算法的效果是随机采样点会“拉着”树向外生长,这样能更快、更有效地探索空间(The effect is that the nearly uniformly distributed samples “pull” thetree toward them, causing the tree to rapidly exploreC-Space)。随机探索也讲究策略,如果我们从树中随机取一个点,然后向着随机的方向生长,那么结果是什么样的呢?见下图(Left:A tree generated by applying a uniformly-distributed randommotion from a randomly chosen tree node does not explore very far. Right:A treegenerated by the RRT algorithm using samples drawn randomly from a uniform distribution. Both trees have 2000 nodes)。可以看到,同样是随机树,但是这棵树并没很好地探索空间。
下面是具体的代码
%% RRT parametersmap=im2bw(imread('map.bmp')); % input map read from a bmp file. for new maps write the file name heresource=[490 490]; % source position in Y, X formatgoal=[10 900]; % goal position in Y, X formatstepsize = 20; % size of each step of the RRTthreshold = 20; % nodes closer than this threshold are taken as almost the samemaxFailedAttempts = 10000;display = true; % display of RRTif ~feasiblePoint(source,map), error('source lies on an obstacle or outside map'); endif ~feasiblePoint(goal,map), error('goal lies on an obstacle or outside map'); endif display,imshow(map);rectangle('position',[1 1 size(map)-1],'edgecolor','k'); endtic; % tic-toc: Functions for Elapsed TimeRRTree = double([source -1]); % RRT rooted at the source, representation node and parent indexfailedAttempts = 0;counter = 0;pathFound = false;while failedAttempts <= maxFailedAttempts % loop to grow RRTs%% chooses a random configurationif rand < 0.5sample = rand(1,2) .* size(map); % random sampleelsesample = goal; % sample taken as goal to bias tree generation to goalend%% selects the node in the RRT tree that is closest to qrand[A, I] = min( distanceCost(RRTree(:,1:2),sample) ,[],1); % find the minimum value of each columnclosestNode = RRTree(I(1),1:2);%% moving from qnearest an incremental distance in the direction of qrandtheta = atan2(sample(1)-closestNode(1),sample(2)-closestNode(2)); % direction to extend sample to produce new nodenewPoint = double(int32(closestNode(1:2) + stepsize * [sin(theta) cos(theta)]));if ~checkPath(closestNode(1:2), newPoint, map) % if extension of closest node in tree to the new point is feasiblefailedAttempts = failedAttempts + 1;continue;endif distanceCost(newPoint,goal) < threshold, pathFound = true; break; end % goal reached[A, I2] = min( distanceCost(RRTree(:,1:2),newPoint) ,[],1); % check if new node is not already pre-existing in the treeif distanceCost(newPoint,RRTree(I2(1),1:2)) < threshold, failedAttempts = failedAttempts + 1; continue; end RRTree = [RRTree; newPoint I(1)]; % add nodefailedAttempts = 0;if display, line([closestNode(2);newPoint(2)],[closestNode(1);newPoint(1)]);counter = counter + 1; M(counter) = getframe; end % Capture movie frame end% getframe returns a movie frame, which is a structure having two fieldsif display && pathFound, line([closestNode(2);goal(2)],[closestNode(1);goal(1)]); counter = counter+1;M(counter) = getframe; endif display, disp('click/press any key'); waitforbuttonpress; endif ~pathFound, error('no path found. maximum attempts reached'); end%% retrieve path from parent informationpath = [goal];prev = I(1);while prev > 0path = [RRTree(prev,1:2); path];prev = RRTree(prev,3);endpathLength = 0;for i=1:length(path)-1, pathLength = pathLength + distanceCost(path(i,1:2),path(i+1,1:2)); end % calculate path lengthfprintf('processing time=%d \nPath Length=%d \n\n', toc, pathLength); imshow(map);rectangle('position',[1 1 size(map)-1],'edgecolor','r');line(path(:,2),path(:,1));
rrt_main.m
%% distanceCost.mfunction h=distanceCost(a,b)h = sqrt(sum((a-b).^2, 2));
distanceCost.m
%% checkPath.m function feasible=checkPath(n,newPos,map)feasible=true;dir=atan2(newPos(1)-n(1),newPos(2)-n(2));for r=0:0.5:sqrt(sum((n-newPos).^2))posCheck=n+r.*[sin(dir) cos(dir)];if ~(feasiblePoint(ceil(posCheck),map) && feasiblePoint(floor(posCheck),map) && ... feasiblePoint([ceil(posCheck(1)) floor(posCheck(2))],map) && feasiblePoint([floor(posCheck(1)) ceil(posCheck(2))],map))feasible=false;break;endif ~feasiblePoint(newPos,map), feasible=false; endend
checkPath.m
%% feasiblePoint.mfunction feasible=feasiblePoint(point,map)feasible=true;% check if collission-free spot and inside mapsif ~(point(1)>=1 && point(1)<=size(map,1) && point(2)>=1 && point(2)<=size(map,2) && map(point(1),point(2))==1)feasible=false;end
feasiblePoint.m
结果如图