rrt算法

1、算法原理9571041437964e689f9c8da976940ec4.gif

1)将起点q_{ini}加入树,随机扩展一个节点q_{rand},并在已有节点中寻找距离其最近的节点q_{near}

6a4ea3154191482b8eb0473490d10169.png

 2)在q_{near}--->q_{rand}方向上,以采样步长γ扩展,并进行collisionchecking,无碰撞,得到新节点q_{new};有碰撞,重新采样q_{rand}

9051307b89204cbd9ab797ac2fe8f79d.png

f6c6567905dc4547b67f648246d29110.png

21370545c29d40a89466034364e16f1e.png

79ed182629da4f4495391fcc68f53f21.png

4c0114576b8e4b6a9c57f2f524d867ce.png

30a1828ceb9d4708b5b9913cbd3a1ac7.png

3)计算q_{new}q_{goal}之间距离,当小于某一给定值时,终止采样;

4)从q_{goal}回溯,找到起点q_{ini},从而确定从起点到终点的路径。

rrt算法具有概率的完备性,只要采样次数足够多,总可以找到连接起点终点的一条路径;

但在路径搜索过程中,完全没有考虑cost,这会导致找到的路径比较差。

2、matlab代码

clear;
close all;
clc;

%% 载入地图
figure(1);
pic=imread('map.png');
fig=rgb2gray(pic);
imshow(fig);
hold on;
title('RRT* algorithm');
mLength=size(fig,2);
mWidth=size(fig,1);
Point=ginput(2);  %获取起点坐标、终点坐标
startPoint=Point(1,:);
goalPoint=Point(2,:);
disp(['起点坐标为:(',num2str(startPoint),')']);
disp(['终点坐标为:(',num2str(goalPoint),')']);
plot(startPoint(1),startPoint(2),'mo','MarkerSize',8,'MarkerFaceColor','m');
plot(goalPoint(1),goalPoint(2),'rp','MarkerSize',8,'MarkerFaceColor','r');
%% 参数初始化
sampleNums=30000;  %采样次数
stepLength=20;     %采样步长
%将起点加入树
nodes_list(1).position=startPoint;
nodes_list(1).parentind=0;
nodes_list(1).cost=0;
count=0;
for j=1:sampleNums
    count=count+1;
%随机采样,rand-->new
node_rand=[mLength*rand,mWidth*rand];
N=size(nodes_list,2); %获取树中当前节点数
minDis=Inf; %初始化树中节点距离newnode的最小距离
for i=1:N
    d=norm(node_rand-nodes_list(i).position);
    if minDis > d
        minDis=d;
        ind=i;
    end
end
near2rand=(node_rand-nodes_list(ind).position)/norm(node_rand-nodes_list(ind).position)*stepLength;
newnode=nodes_list(ind).position+near2rand;
% collisionchecking
flag=collisionCheck(newnode,nodes_list(ind).position,fig);
if flag
    plot(newnode(1),newnode(2),'ro','MarkerFaceColor','r','MarkerSize',2);
    plot([nodes_list(ind).position(1),newnode(1)],[nodes_list(ind).position(2),newnode(2)],'b-','LineWidth',1);
    nodes_list(N+1).position=newnode;
    nodes_list(N+1).parentind=ind;
    nodes_list(N+1).cost=nodes_list(ind).cost+norm(near2rand);
    if norm(newnode-goalPoint)<=stepLength
        nodes_list(N+2).position=goalPoint;
        nodes_list(N+2).parentind=N+1;
        nodes_list(N+2).cost=nodes_list(N+1).cost+norm(newnode-goalPoint);
        plot([newnode(1),goalPoint(1)],[newnode(2),goalPoint(2)],'b-');
  break;
    end
end
pause(0.0);
end
% 从终点回溯,寻找路径
index=N+2;
while nodes_list(index).parentind~=0
    plot([nodes_list(index).position(1),nodes_list(nodes_list(index).parentind).position(1)], ...
        [nodes_list(index).position(2),nodes_list(nodes_list(index).parentind).position(2)],'r-',...
        'LineWidth',3);
    index=nodes_list(index).parentind;
end
if count==sampleNums
    disp('超出采样次数');
else
    disp(['SUCCESS!采样次数为:',num2str(count)]);
    disp(['cost:',num2str(nodes_list(N+2).cost)]);
end

碰撞检测函数

function flag = collisionCheck(newnode,nearnode,map)
%COLLISIONCHECK 
%   flag==0 有障碍物,拓展失败 flag==1 无障碍物
    near2new=newnode-nearnode;
    flag=1;
    if newnode(1)>=size(map,2)||newnode(1)<=1||newnode(2)>=size(map,1)||newnode(2)<=1
        flag=0;
    end
for i=0:0.1:1
    if flag==0
        break;
    end
    checkingPoint=nearnode+i*near2new;
    if checkingPoint(1)>=size(map,2)||checkingPoint(1)<=1||checkingPoint(2)>=size(map,1)||checkingPoint(2)<=1
        flag=0;
        break;
    end
    if map(ceil(checkingPoint(2)),ceil(checkingPoint(1)))==0||map(ceil(checkingPoint(2)),floor(checkingPoint(1)))==0 ...
            ||map(floor(checkingPoint(2)),ceil(checkingPoint(1)))==0||map(floor(checkingPoint(2)),floor(checkingPoint(1)))==0
        flag=0;
        break;
    end
end

欢迎批评指正!

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值