-
Notifications
You must be signed in to change notification settings - Fork 10
/
informed_RRTstar.m
191 lines (187 loc) · 6.51 KB
/
informed_RRTstar.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
%% informed RRT*
%***************************************
%Author: Chaoqun Wang
%Date: 2019-10-15
%***************************************
%% ���̳�ʼ��
clear all; close all;
x_I=1; y_I=1; % ���ó�ʼ��
x_G=350; y_G=350; % ����Ŀ���
Thr=50; %����Ŀ�����ֵ
Delta= 60; % ������չ����
%% ������ʼ��
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];
start_goal_dist = 1000000;
path.pos(1).x = 350;
path.pos(1).y = 350;
for iter = 1:1000
x_rand=[];
%%=========采样想x_rand========%%
if start_goal_dist < 1000000
while 1
x_rand(1) = xL*rand;
x_rand(2) = yL*rand;
if new_node(x_rand(1),x_rand(2),start_goal_dist)
break;
end
end
else
x_rand(1) = xL*rand;
x_rand(2) = yL*rand;
end
%%=======寻找x_near===========%%
x_near=[];
min_dist = 1000000;
near_iter = 1;
near_iter_tmp = 1;
[~,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
%%======= nearC && chooseParent =========%%
nearptr = [];
nearcount = 0;
neardist = norm(x_new - x_near) + T.v(near_iter_tmp).dist;
for j = 1:N
if j == near_iter_tmp
continue;
end
x_neartmp(1) = T.v(j).x;
x_neartmp(2) = T.v(j).y;
dist = norm(x_new - x_neartmp) + T.v(j).dist;
norm_dist = norm(x_new - x_neartmp);
if norm_dist < 120
%nearC
if collisionChecking(x_neartmp,x_new,Imp)
nearcount = nearcount + 1;
nearptr(nearcount,1) = j;
if neardist > dist
neardist = dist;
near_iter = j;
end
end
end
end
x_near(1) = T.v(near_iter).x;
x_near(2) = T.v(near_iter).y;
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(near_iter).dist;
T.v(count).indPrev = near_iter;
%%======== rewirte =========%%
[M,~] = size(nearptr);
for k = 1:M
x_1(1) = T.v(nearptr(k,1)).x;
x_1(2) = T.v(nearptr(k,1)).y;
x1_prev(1) = T.v(nearptr(k,1)).xPrev;
x1_prev(2) = T.v(nearptr(k,1)).yPrev;
if T.v(nearptr(k,1)).dist > (T.v(count).dist + norm(x_1-x_new))
T.v(nearptr(k,1)).dist = T.v(count).dist + norm(x_1-x_new);
T.v(nearptr(k,1)).xPrev = x_new(1);
T.v(nearptr(k,1)).yPrev = x_new(2);
T.v(nearptr(k,1)).indPrev = count;
plot([x_1(1),x1_prev(1)],[x_1(2),x1_prev(2)],'-w');
hold on;
plot([x_1(1),x_new(1)],[x_1(2),x_new(2)],'-g');
hold on;
end
end
plot([x_near(1),x_new(1)],[x_near(2),x_new(2)],'-r');
hold on;
plot(x_new(1),x_new(2),'*r');
hold on;
if norm(x_new - goal) < Thr
if (T.v(count).dist + norm(x_new - goal)) < start_goal_dist
start_goal_dist = (T.v(count).dist + norm(x_new - goal));
if length(path.pos) > 2
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], 'w', 'Linewidth', 3);
end
end
path.pos = [];
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
end
continue;
end
pause(0.01);
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