-
Notifications
You must be signed in to change notification settings - Fork 45
/
rrt_star.m
189 lines (166 loc) · 5.19 KB
/
rrt_star.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
function [path, flag, cost, expand] = rrt_star(map, start, goal)
%%
% @file: rrt_star.m
% @breif: RRT-Star motion planning
% @paper: Sampling-based algorithms for optimal motion planning
% @author: Winter
% @update: 2023.2.2
%%
% optimal radius
param.r = 30;
% Maximum expansion distance one step
param.max_dist = 0.5;
% Maximum number of sample points
param.sample_num = 10000;
% heuristic sample
param.goal_sample_rate = 0.05;
% map size
[param.x_range, param.y_range] = size(map);
% resolution
param.resolution = 0.1;
% sample list
sample_list = [start, 0, start];
path = [];
flag = false;
cost = 0;
expand = [];
% main loop
for i=1: param.sample_num
% generate a random node in the map
node_rand = generate_node(goal, param);
% visited
if loc_list(node_rand, sample_list, [1, 2])
continue
end
% generate new node
[node_new, success] = get_nearest(sample_list, node_rand, map, param);
if success
sample_list = [node_new; sample_list];
distance = dist(node_new(1:2), goal');
% goal found
if distance <= param.max_dist && ~is_collision(node_new(1:2), goal, map, param)
goal_ = [goal, node_new(3) + distance, node_new(1:2)];
sample_list = [goal_; sample_list];
flag = true;
cost = goal_(3);
break
end
end
end
if flag
path = extract_path(sample_list, start);
expand = sample_list;
end
end
%%
function index = loc_list(node, list, range)
% @breif: locate the node in given list
num = size(list);
index = 0;
if ~num(1)
return
else
for i=1:num(1)
if isequal(node(range), list(i, range))
index = i;
return;
end
end
end
end
function node = generate_node(goal, param)
%breif: Generate a random node to extend exploring tree.
if rand() > param.goal_sample_rate
x = 0.5 + (param.x_range - 1) * rand();
y = 0.5 + (param.y_range - 1) * rand();
node = [x, y];
return
end
node = goal;
return
end
function [new_node, flag] = get_nearest(node_list, node, map, param)
%@breif: Get the node from `node_list` that is nearest to `node`.
flag = false;
% find nearest neighbor
dist_vector = dist(node_list(:, 1:2), node');
[~, index] = min(dist_vector);
node_near = node_list(index, :);
% regular and generate new node
distance = min(dist(node_near(1:2), node'), param.max_dist);
theta = angle(node_near, node);
new_node = [node_near(1) + distance * cos(theta), ...
node_near(2) + distance * sin(theta), ...
node_near(3) + distance, ...
node_near(1:2)];
% obstacle check
if is_collision(new_node(1:2), node_near(1:2), map, param)
return
end
% rewire optimization
[node_num, ~] = size(node_list);
for i=1:node_num
node_n = node_list(i, :);
% inside the optimization circle
new_dist = dist(node_n(1:2), new_node(1:2)');
if new_dist < param.r
cost = node_n(3) + new_dist;
% update new sample node's cost and parent
if new_node(3) > cost && ~is_collision(new_node(1:2), node_n(1:2), map, param)
new_node(4:5) = node_n(1:2);
new_node(3) = cost;
else
% update nodes' cost inside the radius
cost = new_node(3) + new_dist;
if node_n(3) > cost && ~is_collision(new_node(1:2), node_n(1:2), map, param)
node_list(i, 4:5) = new_node(1:2);
node_list(i, 3) = cost;
end
end
else
continue;
end
end
flag = true;
end
function flag = is_collision(node1, node2, map, param)
%@breif: Judge collision when moving from node1 to node2.
flag = true;
theta = angle(node1, node2);
distance = dist(node1, node2');
% distance longer than the threshold
if (distance > param.max_dist)
return
end
% sample the line between two nodes and check obstacle
n_step = round(distance / param.resolution);
for i=1:n_step
x = node1(1) + i * param.resolution * cos(theta);
y = node1(2) + i * param.resolution * sin(theta);
if map(round(x), round(y)) == 2
return
end
end
flag = false;
end
function path = extract_path(close, start)
% @breif: Extract the path based on the CLOSED set.
path = [];
closeNum = length(close(:, 1));
index = 1;
while 1
path = [path; close(index, 1:2)];
if isequal(close(index, 1:2), start)
break;
end
for i=1:closeNum
if isequal(close(i, 1:2), close(index, 4:5))
index = i;
break;
end
end
end
end
function theta = angle(node1, node2)
theta = atan2(node2(2) - node1(2), node2(1) - node1(1));
end