-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRRTStar_3D.m
More file actions
112 lines (92 loc) · 3.36 KB
/
Copy pathRRTStar_3D.m
File metadata and controls
112 lines (92 loc) · 3.36 KB
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
clearvars
close all
x_max = 640;
y_max = 480;
z_max = 400;
EPS = 10;
numNodes = 5000;
q_start.coord = [200 150 100];
obs = [220,200,150,200,140,100];
q_start.cost = 0;
q_start.parent = 0;
q_goal.coord = [450 350 260];
q_goal.cost = 0;
nodes(1) = q_start;
figure(1)
axis([0 x_max 0 y_max 0 z_max])
patch(obsgen(obs))
hold on
for i = 1:1:numNodes
q_rand = [rand(1)*x_max rand(1)*y_max rand(1)*z_max];
plot3(q_rand(1), q_rand(2), q_rand(3), 'x', 'Color', [0 0.4470 0.7410])
% Pick the closest node from existing list to branch out from
ndist = zeros(length(nodes),1);
for j = 1:1:length(nodes)
n = nodes(j);
tmp = dist_3d(n.coord, q_rand);
ndist(j) = tmp;
end
[val, idx] = min(ndist);
q_near = nodes(idx);
q_new.coord = steer3d(q_rand, q_near.coord, val, EPS);
if noCollision(q_new.coord, q_near.coord, obs)%collision detection
line([q_near.coord(1), q_new.coord(1)], [q_near.coord(2), q_new.coord(2)], [q_near.coord(3), q_new.coord(3)], 'Color', 'k', 'LineWidth', 2);
drawnow
hold on
q_new.cost = dist_3d(q_new.coord, q_near.coord) + q_near.cost;
% Within a radius of r, find all existing nodes
q_nearest = [];
r = 50;
neighbor_count = 1;
for j = 1:1:length(nodes)
if noCollision(nodes(j).coord, q_new.coord, obs) && (dist_3d(nodes(j).coord, q_new.coord)) <= r
q_nearest(neighbor_count).coord = nodes(j).coord;
q_nearest(neighbor_count).cost = nodes(j).cost;
neighbor_count = neighbor_count+1;
end
end
% Initialize cost to currently known value
q_min = q_near;
C_min = q_new.cost;
% Iterate through all nearest neighbors to find alternate lower
% cost paths
for k = 1:1:length(q_nearest)
if q_nearest(k).cost + dist_3d(q_nearest(k).coord, q_new.coord) < C_min
q_min = q_nearest(k);
C_min = q_nearest(k).cost + dist_3d(q_nearest(k).coord, q_new.coord);
line([q_min.coord(1), q_new.coord(1)], [q_min.coord(2), q_new.coord(2)], [q_min.coord(3), q_new.coord(3)], 'Color', 'g');
hold on
end
end
% Update parent to least cost-from node
for j = 1:1:length(nodes)
if nodes(j).coord == q_min.coord
q_new.parent = j;
end
end
% Append to nodes
nodes = [nodes q_new];
% Break if goal node is already reached
if dist_3d(q_new.coord, q_goal.coord) < EPS
break
end
end
end
D = [];
for j = 1:1:length(nodes)
tmpdist = dist_3d(nodes(j).coord, q_goal.coord);
D = [D tmpdist];
end
% Search backwards from goal to start to find the optimal least cost path
[val, idx] = min(D);
q_final = nodes(idx);
q_goal.parent = idx;
q_end = q_goal;
nodes = [nodes q_goal];
while q_end.parent ~= 0
start = q_end.parent;
line([q_end.coord(1), nodes(start).coord(1)], [q_end.coord(2), nodes(start).coord(2)], [q_end.coord(3), nodes(start).coord(3)], 'Color', 'r', 'LineWidth', 4);
hold on
q_end = nodes(start);
end
save('nodes.mat','nodes','idx');