-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_with_head_on_collision.m
369 lines (280 loc) · 11.9 KB
/
main_with_head_on_collision.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
%Define Boundary for the global plan
Xminglobal=0;
Xmaxglobal=200;
Yminglobal=0;
Ymaxglobal=200;
statobs1=[20 30 10 20];
rectangle('Position',statobs1,'FaceColor',[0 .5 .5]);
hold on
axis([0 200 0 200])
statobs2=[50 10 10 20];
rectangle('Position',statobs2,'FaceColor',[0 .5 .5]);
hold on
axis([0 200 0 200])
START_X=40;
START_Y=40;
GOAL_X=150;
GOAL_Y=150;
grid_len=10;
MAP_IDX=1;
for i=0:grid_len:Xmaxglobal
for j=0:grid_len:Ymaxglobal
%{
if i==0
i=1
end
if j==0
j=1
end
%}
MAP(MAP_IDX, 1)=i;
MAP(MAP_IDX, 2)=j;
MAP(MAP_IDX, 3)=2;
MAP_IDX=MAP_IDX+1;
end
%disp('yo in here');
end
MAP_IDX=MAP_IDX-1;
OPEN=[];
CLOSED=[];
CLOSED_IDX=1;
OPEN_IDX=1;
OBS_COORD_LIST=[];
OBS_IDX=1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2);% 20,30
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1)+statobs1(3);OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2)+statobs1(4);% 30,50
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1)+10;OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2);% 30,30
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1)+10;OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2)+10;% 30,40
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2)+10;% 20,40
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs1(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs1(2)+20;% 20,40
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2);% 20,30
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1)+statobs1(3);OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2)+statobs2(4);% 30,50
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1)+10;OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2);% 30,30
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1)+10;OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2)+10;% 30,40
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2)+10;% 20,40
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=statobs2(1);OBS_COORD_LIST(OBS_IDX, 2)=statobs2(2)+20;% 20,40
Optimal_path=a_star(OPEN, CLOSED, OPEN_IDX, CLOSED_IDX, OBS_COORD_LIST, OBS_IDX, MAP, MAP_IDX, START_X, START_Y, GOAL_X, GOAL_Y, Xminglobal, Xmaxglobal, Yminglobal, Ymaxglobal);
Optimal_path=flipud(Optimal_path);
path=[];
%state lattice primitive decision
curr_heading=0;
TurnRadius = 2;
PathStep = -1;
t1=0;
t2=5;
v1=4;
v2=4;
Optimal_path(1,3)=0;
Optimal_path(1,4)=0;
manu='x';
%dynamic obs path
obsx0=58;
obsy0=90;
obsvx0= 0;
obsvy0=-50/30;
dt=0.1;
DYN_IDX=1;
%{
for tobs=0:dt:60
obsx0=obsx0 + obsvx0*dt;
obsy0=obsy0 + obsvy0*dt;
%disp(obsy0);
dyn_obs_path(DYN_IDX,1)= obsx0;
dyn_obs_path(DYN_IDX,2)= obsy0;
dyn_obs_path(DYN_IDX,3)= tobs;
DYN_IDX=DYN_IDX+1;
end
%}
for OPT_IDX=1:size(Optimal_path,1)-1
Optimal_path(OPT_IDX,4)=t1;
disp(OPT_IDX);
disp('!!!!!!!!!!!!!!!');
if OPT_IDX<size(Optimal_path,1)-1
[manu, next_manu]=find_direction(Optimal_path(OPT_IDX,1),Optimal_path(OPT_IDX,2),Optimal_path(OPT_IDX+1,1),Optimal_path(OPT_IDX+1,2),Optimal_path(OPT_IDX+2,1),Optimal_path(OPT_IDX+2,2));
disp(manu);
disp(next_manu);
disp('------------------');
next_heading=find_heading(manu,next_manu);
Optimal_path(OPT_IDX+1,3)=next_heading;
%path=dubins_curve([Optimal_path(OPT_IDX,1) Optimal_path(OPT_IDX,2) Optimal_path(OPT_IDX,3)] ,[Optimal_path(OPT_IDX+1,1) Optimal_path(OPT_IDX+1,2) Optimal_path(OPT_IDX+1,3)], TurnRadius, PathStep);
temp_path=non_holo_path(Optimal_path(OPT_IDX,1), Optimal_path(OPT_IDX,2), Optimal_path(OPT_IDX,3), Optimal_path(OPT_IDX+1,1), Optimal_path(OPT_IDX+1,2), Optimal_path(OPT_IDX+1,3),v1,v2,t1,t2);
else
Optimal_path(OPT_IDX+1,3)=curr_heading;
%path=dubins_curve([Optimal_path(OPT_IDX,1) Optimal_path(OPT_IDX,2) Optimal_path(OPT_IDX,3)] ,[Optimal_path(OPT_IDX+1,1) Optimal_path(OPT_IDX+1,2) Optimal_path(OPT_IDX+1,3)], TurnRadius, PathStep);
temp_path=non_holo_path(Optimal_path(OPT_IDX,1), Optimal_path(OPT_IDX,2), Optimal_path(OPT_IDX,3), Optimal_path(OPT_IDX+1,1), Optimal_path(OPT_IDX+1,2), Optimal_path(OPT_IDX+1,3),v1,v2,t1,t2);
end
path=[path; temp_path];
curr_heading=next_heading;
t1=t2;
t2=t2+5;
end
OPT_IDX=OPT_IDX+1;
Optimal_path(OPT_IDX,4)=t1+5;
%Trajectory of obstacle for head on collision
dyn_obs_path=flipud(path);
t=0;
for dyn_idx=1:1:size(dyn_obs_path)
dyn_obs_path(dyn_idx,3)= dyn_obs_path(dyn_idx,3)+pi;
dyn_obs_path(dyn_idx,4)= t;
t=t+0.1;
end
collision_avoided=false;
%for OPT_IDX=1:size(Optimal_path,1)-1
j=size(path,1);
k=size(dyn_obs_path,1);
%Plot the Optimal Path!
p=plot(path(1,1),path(1,2),'bo');
dy=plot(dyn_obs_path(1,1),dyn_obs_path(1,2),'bo');
%j=j+1;
g=max(j,k);
for i=1:1:g
%pause(.00001);
j=size(path,1);
g=max(j,k);
if i<=j
set(p,'XData',path(i,1),'YData',path(i,2));
plot(path(:,1),path(:,2));
%disp(round(path(i,1)));
%disp(round(path(i,2)));
end
if(i<=k)
set(dy,'XData',dyn_obs_path(i,1),'YData',dyn_obs_path(i,2));
%plot(dyn_obs_path(:,1),dyn_obs_path(:,2));
end
local_rect=[path(i,1)-30 path(i,1)-30 path(i,1)+30 path(i,1)+30 path(i,1)-30; path(i,2)-30 path(i,2)+30 path(i,2)+30 path(i,2)-30 path(i,2)-30];
a=line(local_rect(1,:),local_rect(2,:));
drawnow;
is_present_dyn_obs= check_for_dyn_obs(path(i,1)-30,path(i,1)+30,path(i,2)-30,path(i,2)+30,dyn_obs_path(i,1),dyn_obs_path(i,2));
if (is_present_dyn_obs==true && collision_avoided==false)
idx=i;
[dyn_coll_x1,dyn_coll_x2,dyn_coll_y1,dyn_coll_y2,coll_time] = dyn_coll_detection(path, dyn_obs_path, idx);
dyn_obs = [dyn_coll_x1,dyn_coll_x2,dyn_coll_y1,dyn_coll_y2, coll_time];
disp('Initial Dynamic collision detection done');
dyn_obs_x=[dyn_coll_x1 dyn_coll_x2 dyn_coll_x2 dyn_coll_x1 dyn_coll_x1];
dyn_obs_y=[dyn_coll_y1 dyn_coll_y1 dyn_coll_y2 dyn_coll_y2 dyn_coll_y1];
pseudo_obs= line(dyn_obs_x,dyn_obs_y,'Color','r');
% 1st strategy to avoid collision- check if stopping and proceeding avoids the collision
for OPT_IDX=1:1:size(Optimal_path)-1
if ((Optimal_path(OPT_IDX,1)==dyn_obs(1) && Optimal_path(OPT_IDX,2)==dyn_obs(3))|| (Optimal_path(OPT_IDX,1)==dyn_obs(2) && Optimal_path(OPT_IDX,2)==dyn_obs(3)) || (Optimal_path(OPT_IDX,1)==dyn_obs(2) && Optimal_path(OPT_IDX,2)==dyn_obs(4)) || (Optimal_path(OPT_IDX,1)==dyn_obs(1) && Optimal_path(OPT_IDX,2)==dyn_obs(4)))
stop_point_x=Optimal_path(OPT_IDX,1);
stop_point_y=Optimal_path(OPT_IDX,2);
stop_point_IDX= OPT_IDX;
stop_time=Optimal_path(OPT_IDX,4);
time_to_stop=coll_time-stop_time;
break
end
end
disp('Stop point calculated');
new_path= check_stop_avoidance(path, stop_time, time_to_stop,idx);
disp('New path after stopping is found');
[new_coll_x1,new_coll_x2,new_coll_y1,new_coll_y2,coll_time] = dyn_coll_detection(new_path, dyn_obs_path, idx);
disp('Final Dynamic collision detection done');
disp('coll_time is : ');
disp(coll_time);
if coll_time==inf
collision_avoided=true;
path=new_path;
end
% start code to replan
if collision_avoided==false
%adding obstacle coordinates to closed list fr A*
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=dyn_obs(1);OBS_COORD_LIST(OBS_IDX, 2)=dyn_obs(3);
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=dyn_obs(2);OBS_COORD_LIST(OBS_IDX, 2)=dyn_obs(3);
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=dyn_obs(2);OBS_COORD_LIST(OBS_IDX, 2)=dyn_obs(4);
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=dyn_obs(1);OBS_COORD_LIST(OBS_IDX, 2)=dyn_obs(4);
for OPT_IDX=1:1:size(Optimal_path)-1
if(path(i,4)>=Optimal_path(OPT_IDX,4) && path(i,4)<=Optimal_path(OPT_IDX+1,4))
replanning_start_x=Optimal_path(OPT_IDX+1,1);
replanning_start_y=Optimal_path(OPT_IDX+1,2);
replanning_start_time=Optimal_path(OPT_IDX+1,4);
replanning_start_IDX=OPT_IDX+1;
break
end
end
OBS_IDX=OBS_IDX+1;
OBS_COORD_LIST(OBS_IDX, 1)=Optimal_path(replanning_start_IDX-1,1);OBS_COORD_LIST(OBS_IDX, 2)=Optimal_path(replanning_start_IDX-1,2);
new_path_1=[];
new_path_2=[];
new_path_3=[];
new_path=[];
replanning_stop_x=Optimal_path(stop_point_IDX+3, 1);
replanning_stop_y=Optimal_path(stop_point_IDX+3, 2);
replanning_stop_time=Optimal_path(stop_point_IDX+3, 4);
for path_IDX=1:1:size(path)-1
if path(path_IDX,4)<replanning_start_time
new_path_1=[new_path_1;path(path_IDX,:)];
else if path(path_IDX,4)>replanning_stop_time
new_path_3=[new_path_3;path(path_IDX,:)];
end
end
end
%OBS_IDX=OBS_IDX+1;
replanned_path=a_star(OPEN, CLOSED, OPEN_IDX, CLOSED_IDX, OBS_COORD_LIST, OBS_IDX, MAP, MAP_IDX, replanning_start_x, replanning_start_y, replanning_stop_x, replanning_stop_y, Xminglobal, Xmaxglobal, Yminglobal, Ymaxglobal);
replanned_path=flipud(replanned_path);
curr_heading=Optimal_path(replanning_start_IDX,3);
deltaT=(Optimal_path(stop_point_IDX+3,4)-Optimal_path(replanning_start_IDX,4))/(size(replanned_path,1)-1);
t1=Optimal_path(replanning_start_IDX,4);
t2=t1+deltaT;
v1=4;
v2=4;
replanned_path(1,3)=curr_heading;
replanned_path(1,4)=t1;
manu='x';
for replanning_IDX=1:size(replanned_path,1)-1
replanned_path(OPT_IDX,4)=t1;
disp(replanning_IDX);
disp('@@@@@@@@@@@@@@@@@@@@@@@@');
if replanning_IDX<size(replanned_path,1)-1
[manu, next_manu]=find_direction(replanned_path(replanning_IDX,1),replanned_path(replanning_IDX,2),replanned_path(replanning_IDX+1,1),replanned_path(replanning_IDX+1,2),replanned_path(replanning_IDX+2,1),replanned_path(replanning_IDX+2,2));
disp(manu);
disp(next_manu);
disp('------------------');
next_heading=find_heading(manu,next_manu);
replanned_path(replanning_IDX+1,3)=next_heading;
%path=dubins_curve([Optimal_path(OPT_IDX,1) Optimal_path(OPT_IDX,2) Optimal_path(OPT_IDX,3)] ,[Optimal_path(OPT_IDX+1,1) Optimal_path(OPT_IDX+1,2) Optimal_path(OPT_IDX+1,3)], TurnRadius, PathStep);
temp_path=non_holo_path(replanned_path(replanning_IDX,1), replanned_path(replanning_IDX,2), replanned_path(replanning_IDX,3), replanned_path(replanning_IDX+1,1), replanned_path(replanning_IDX+1,2), replanned_path(replanning_IDX+1,3),v1,v2,t1,t2);
else
replanned_path(replanning_IDX+1,3)=Optimal_path(stop_point_IDX+3, 3);
%path=dubins_curve([Optimal_path(OPT_IDX,1) Optimal_path(OPT_IDX,2) Optimal_path(OPT_IDX,3)] ,[Optimal_path(OPT_IDX+1,1) Optimal_path(OPT_IDX+1,2) Optimal_path(OPT_IDX+1,3)], TurnRadius, PathStep);
temp_path=non_holo_path(replanned_path(replanning_IDX,1), replanned_path(replanning_IDX,2), replanned_path(replanning_IDX,3), replanned_path(replanning_IDX+1,1), replanned_path(replanning_IDX+1,2), replanned_path(replanning_IDX+1,3),v1,v2,t1,t2);
end
new_path_2=[new_path_2; temp_path];
curr_heading=next_heading;
t1=t2;
t2=t2+deltaT;
end
replanning_IDX=replanning_IDX+1;
replanned_path(replanning_IDX,4)=t1+deltaT;
new_path= [new_path_1;new_path_2;new_path_3];
[new_coll_x1,new_coll_x2,new_coll_y1,new_coll_y2,coll_time] = dyn_coll_detection(new_path, dyn_obs_path, idx);
disp('Dynamic collision detection after replanning done');
disp('coll_time is : ');
disp(coll_time);
if coll_time==inf
collision_avoided=true;
path=new_path;
end
end
if path(i,4)>stop_time+time_to_stop
delete (pseudo_obs);
end
end
axis([0 200 0 200])
delete(a);
end
%end