-
Notifications
You must be signed in to change notification settings - Fork 10
/
RRT.m
98 lines (96 loc) · 3.29 KB
/
RRT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
%***************************************
%Author: Chaoqun Wang
%Date: 2019-10-15
%***************************************
%% ���̳�ʼ��
clear all; close all;
x_I=1; y_I=1; % ���ó�ʼ��
x_G=700; y_G=700; % ����Ŀ���
Thr=50; %����Ŀ�����ֵ
Delta= 30; % ������չ����
%% ������ʼ��
T.v(1).x = x_I; % T������Ҫ��������v�ǽڵ㣬�����Ȱ���ʼ����뵽T������
T.v(1).y = y_I;
T.v(1).xPrev = x_I; % ��ʼ�ڵ�ĸ��ڵ���Ȼ���䱾��
T.v(1).yPrev = y_I;
T.v(1).dist=0; %�Ӹ��ڵ㵽�ýڵ�ľ��룬�����ȡŷ�Ͼ���
T.v(1).indPrev = 0; %
%% ��ʼ������������ҵ����
figure(1);
ImpRgb=imread('newmap.png');
Imp=rgb2gray(ImpRgb);
imshow(Imp)
xL=size(Imp,1);%��ͼx�᳤��
yL=size(Imp,2);%��ͼy�᳤��
hold on
plot(x_I, y_I, 'ro', 'MarkerSize',10, 'MarkerFaceColor','r');
plot(x_G, y_G, 'go', 'MarkerSize',10, 'MarkerFaceColor','g');% ��������Ŀ���
count=1;
goal = [x_G,y_G];
for iter = 1:3000
x_rand=[];
x_rand(1) = xL*rand;
x_rand(2) = yL*rand;
x_near=[];
min_dist = 1000000;
near_iter = 1;
%%=======寻找x_near===========%%
[~,N]=size(T.v);
for j = 1:N
x_near(1) = T.v(j).x;
x_near(2) = T.v(j).y;
dist = norm(x_rand - x_near);
if min_dist > dist
min_dist = dist;
near_iter = j;
end
end
x_near(1) = T.v(near_iter).x;
x_near(2) = T.v(near_iter).y;
%%========获取x_new============%%
x_new=[];
near_to_rand = [x_rand(1)-x_near(1),x_rand(2)-x_near(2)];
normlized = near_to_rand / norm(near_to_rand) * Delta;
x_new = x_near + normlized;
%%=======障碍检测===============%%
if ~collisionChecking(x_near,x_new,Imp)
continue;
end
count=count+1;
%%========将X_NEW增加到树中========%%
T.v(count).x = x_new(1);
T.v(count).y = x_new(2);
T.v(count).xPrev = x_near(1);
T.v(count).yPrev = x_near(2);
T.v(count).dist= norm(x_new - x_near);
T.v(count).indPrev = near_iter;
plot([x_near(1),x_new(1)],[x_near(2),x_new(2)],'-r');
hold on;
%========判断是否找到路径==========%%
if norm(x_new - goal) < Thr
break;
end
pause(0.1);
end
%% ·���Ѿ��ҵ��������ѯ
if iter < 2000
path.pos(1).x = x_G; path.pos(1).y = y_G;
path.pos(2).x = T.v(end).x; path.pos(2).y = T.v(end).y;
pathIndex = T.v(end).indPrev; % �յ����·��
j=0;
while 1
path.pos(j+3).x = T.v(pathIndex).x;
path.pos(j+3).y = T.v(pathIndex).y;
pathIndex = T.v(pathIndex).indPrev;
if pathIndex == 1
break
end
j=j+1;
end % ���յ���ݵ����
path.pos(end+1).x = x_I; path.pos(end).y = y_I; % ������·��
for j = 2:length(path.pos)
plot([path.pos(j).x; path.pos(j-1).x;], [path.pos(j).y; path.pos(j-1).y], 'b', 'Linewidth', 3);
end
else
disp('Error, no path found!');
end