-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3jsbot_rrt_connect.js
326 lines (265 loc) · 8.47 KB
/
3jsbot_rrt_connect.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//////////////////////////////////////////////////
///// RRT MOTION PLANNER
//////////////////////////////////////////////////
// CS148:
// implement RRT-Connect by Kuffner and LaValle (2000)
// paper link: http://msl.cs.uiuc.edu/~lavalle/papers/KufLav00.pdf
// compute motion plan and output into robot_path array
// elements of robot_path are vertices based on tree structure in tree_init()
// motion planner assumes collision checking by robot_collision_test()
/*
CS148: reference code has functions for:
tree_add_vertex
tree_add_edge
random_config
new_config
nearest_neighbor
rrt_extend
rrt_connect
find_path
path_dfs
*/
function robot_rrt_planner_init() {
robot_path_traverse_idx = 0;
// form configuration from base location and joint angles
q_start_config = [
robot.origin.xyz[0],
robot.origin.xyz[1],
robot.origin.xyz[2],
robot.origin.rpy[0],
robot.origin.rpy[1],
robot.origin.rpy[2]
];
//console.log(q_start_config);
q_names = {}; // store mapping between joint names and q DOFs
for (x in robot.joints) {
q_names[x] = q_start_config.length;
q_start_config = q_start_config.concat(robot.joints[x].angle);
}
//console.log(q_start_config);
// set goal configuration as the zero configuration
var i;
q_goal_config = new Array(q_start_config.length);
for (i=0;i<q_goal_config.length;i++) q_goal_config[i] = 0;
// CS148: add necessary RRT initialization here
// make sure the rrt iterations are not running faster than animation update
cur_time = Date.now();
EPSILON = .5;
if (typeof Ta !== 'undefined'){
for (var i = 0; i <= Ta.newest; i++){
scene.remove(Ta.vertices[i].geom);
}
}
if (typeof Tb !== 'undefined'){
for (var i = 0; i <= Tb.newest; i++){
scene.remove(Tb.vertices[i].geom);
}
}
Ta = tree_init(q_start_config);
Tb = tree_init(q_goal_config);
Ta.vertices[Ta.newest].geom.material.color = {r:0,g:0,b:1};
Tb.vertices[Tb.newest].geom.material.color = {r:0,g:1,b:0};
q_init = q_start_config;
q_goal = q_goal_config;
rrt_iterate = true;
total_vertices = 0;
//console.log("planner initialized");
}
// function draw_highlighted_path(robot_path) {
// for (i=0;i<robot_path.length;i++) {
// robot_path[i].geom.material.color = {r:1,g:0,b:0};
// }
// }
function robot_rrt_planner_iterate() {
if (rrt_iterate && (Date.now()-cur_time > 10)) {
robot_path = robot_rrt_planner_iterate_loop(q_init, q_goal);
if (robot_path.length !== 0) {
rrt_iterate = 0;
cur_time = Date.now();
return "Reached";
}
cur_time = Date.now();
}
// return path not currently found
return "Advanced";
}
function tree_add_vertex (T, q) {
T.newest = T.newest + 1;
T.vertices[T.newest] = {};
T.vertices[T.newest].vertex = q;
T.vertices[T.newest].edges = [];
add_config_origin_indicator_geom(T.vertices[T.newest]);
total_vertices += 1;
}
function tree_add_edge (T, child_index, parent_index) {
T.vertices[child_index].parent = T.vertices[parent_index];
}
function random_config () {
var q_rand = [Math.random()*(robot_boundary[1][0]-(robot_boundary[0][0]))+(robot_boundary[0][0]),
0,
Math.random()*(robot_boundary[1][2]-(robot_boundary[0][2]))+(robot_boundary[0][2]),
0,
Math.random()*(6.28318531),
0];
for (var i = 6; i < q_init.length; i++) {
q_rand.push(0);
}
return q_rand;
}
function nearest_neighbor (q, T) {
var curr_nearest = T.vertices[0].vertex;
var curr_dist = Infinity;
for (var i = 0; i <= T.newest; i++) {
var dist = distance(T.vertices[i].vertex, q);
if (dist < curr_dist){
curr_nearest = T.vertices[i].vertex;
curr_dist = dist;
}
}
return curr_nearest;
}
function nearest_neighbor_index (q, T) {
var curr_nearest = 0;
var curr_dist = Infinity;
for (var i = 0; i <= T.newest; i++) {
var dist = distance(T.vertices[i].vertex, q);
if (dist < curr_dist){
curr_nearest = i;
curr_dist = dist;
}
}
return curr_nearest;
}
function vector_normalize (A) {
var B = Array(A.length);
var total = 0;
for (i = 0; i < A.length; i++) {
total += A[i]*A[i];
}
var length_squared = total;
var length = Math.sqrt(length_squared);
for (i = 0; i < B.length; i++) {
B[i]= A[i]/length;
}
return B;
}
function find_path (Ta, Tb) {
var path = [];
//Ta
var current_node = TaEnd;
current_node.geom.material.color = {r:1,g:0,b:0};
if (current_node !== undefined) {
path.unshift(current_node);
}
while (current_node.parent !== undefined) {
current_node = current_node.parent;
current_node.geom.material.color = {r:1,g:0,b:0};
path.unshift(current_node);
}
//Tb
current_node = TbEnd;
current_node.geom.material.color = {r:1,g:0,b:0};
if (current_node !== undefined) {
path.push(current_node);
}
while (current_node.parent !== undefined) {
current_node = current_node.parent;
current_node.geom.material.color = {r:1,g:0,b:0};
path.push(current_node);
}
return path;
}
function new_config (q, q_near) {
var d1 = [];
for (var i = 0; i < q.length; i++){
d1.push(q[i] - q_near[i]);
}
d1 = vector_normalize(d1);
var q_new = [];
for (var i = 0; i < q.length; i++){
q_new.push(q_near[i] + EPSILON*(d1[i]));
}
return q_new;
}
function distance (A, B){
var total = 0;
for (var i = 0; i < A.length; i++) {
total += Math.pow((A[i] - B[i]), 2);
}
total = Math.sqrt(total);
return total;
}
function rrt_extend (T, q) {
//something in Tb
var neighbor_index = nearest_neighbor_index(q, T);
var q_near = T.vertices[neighbor_index].vertex;
//also in Tb, but if it == q then already in Ta
var q_new = new_config(q, q_near);
TbEnd = Tb.vertices[neighbor_index];
TaEnd = Ta.vertices[Ta.newest];
if (!robot_collision_test(q_new)){
tree_add_vertex(T, q_new);
tree_add_edge(T, T.newest, neighbor_index);
if (distance(q_new, q) < EPSILON) {
//alert("Added to tree and Reached");
return "Reached";
} else {
//alert("Added to tree and Advanced");
return "Advanced";
}
}
return "Trapped";
}
function rrt_connect (T, q) {
var ret = "Advanced";
while (ret === "Advanced") {
ret = rrt_extend(T, q);
}
return ret;
}
function robot_rrt_planner_iterate_loop(q_init, q_goal) {
//alert("Loop");
qrand = random_config();
// console.log("QRand:");
// console.log(qrand);
if (rrt_extend(Ta, qrand) !== "Trapped"){
//alert("Trying to connect!");
if (rrt_connect(Tb, Ta.vertices[Ta.newest].vertex) === "Reached") {
//alert("total_vertices: " + total_vertices + "\nTa verts: " + (Ta.newest+1) + "\nTb Verts: " + (Tb.newest+1));
return find_path(Ta, Tb);
}
//alert("Done Connecting.");
}
temp = Ta;
Ta = Tb;
Tb = temp;
//alert("Swapping.");
return [];
}
function tree_init(q) {
// create tree object
var tree = {};
// initialize with vertex for given configuration
tree.vertices = [];
tree.vertices[0] = {};
tree.vertices[0].vertex = q;
tree.vertices[0].edges = [];
// create rendering geometry for base location of vertex configuration
add_config_origin_indicator_geom(tree.vertices[0]);
// maintain index of newest vertex added to tree
tree.newest = 0;
return tree;
}
function add_config_origin_indicator_geom(vertex) {
// create a threejs rendering geometry for the base location of a configuration
// assumes base origin location for configuration is first 3 elements
// assumes vertex is from tree and includes vertex field with configuration
temp_geom = new THREE.CubeGeometry(0.1,0.1,0.1);
temp_material = new THREE.MeshLambertMaterial( { color: 0xffff00, transparent: true, opacity: 0.7 } );
temp_mesh = new THREE.Mesh(temp_geom, temp_material);
temp_mesh.position.x = vertex.vertex[0];
temp_mesh.position.y = vertex.vertex[1];
temp_mesh.position.z = vertex.vertex[2];
scene.add(temp_mesh);
vertex.geom = temp_mesh;
}