-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parallel_Parking_Simulator.m
385 lines (343 loc) · 13.7 KB
/
Parallel_Parking_Simulator.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
close all
%% Parallel Parking Simulation of Robot
sample_time=0.1
dx_robo=300
dy_robo=150
speed=166
wheel_base=155
turn_radius=260
steer_angle=(atan(wheel_base/turn_radius))
rad2deg(steer_angle)
% Create a figure and plot the initial rectangle
f1=figure;
f1.Position=[100,600,1200,250]
%r_left = rectangle('Position',[0 (2.5*dy_robo) (5*dx_robo) (1.5*dy_robo)], 'FaceColor', 'r','EdgeColor','b','LineWidth',3);
%line_centre = line([(5*dx_robo) (5*dx_robo+1.5*dx_robo)],[(2.5*dy_robo+1.5*dy_robo) (2.5*dy_robo+1.5*dy_robo)],'color','b','LineWidth',3);
%r_right = rectangle('Position',[(5*dx_robo+1.5*dx_robo) (2.5*dy_robo) (3*dx_robo) (1.5*dy_robo)], 'FaceColor', 'r','EdgeColor','b','LineWidth',3);
%axis([-1*dx_robo 11*dx_robo -1*dy_robo 5*dy_robo]);
hold on;
r_left_x1=0;
r_left_x2=0;
r_left_x3=5*dx_robo;
r_left_x4=5*dx_robo;
r_left_y1=2*dy_robo;
r_left_y2=2*dy_robo+1.5*dy_robo;
r_left_y3=2*dy_robo;
r_left_y4=2*dy_robo+1.5*dy_robo;
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
plot(pgon_r_left);
center_line_x1=5*dx_robo;
center_line_x2=5*dx_robo;
center_line_x3=5*dx_robo+1.5*dx_robo;
center_line_x4=5*dx_robo+1.5*dx_robo;
center_line_y1=2*dy_robo+1.5*dy_robo;
center_line_y2=2*dy_robo+1.6*dy_robo;
center_line_y3=2*dy_robo+1.5*dy_robo;
center_line_y4=2*dy_robo+1.6*dy_robo;
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
plot(pgon_c_line);
r_right_x1=5*dx_robo+1.5*dx_robo;
r_right_x2=5*dx_robo+1.5*dx_robo;
r_right_x3=5*dx_robo+1.5*dx_robo+3*dx_robo;
r_right_x4=5*dx_robo+1.5*dx_robo+3*dx_robo;
r_right_y1=2*dy_robo;
r_right_y2=2*dy_robo+1.5*dy_robo;
r_right_y3=2*dy_robo;
r_right_y4=2*dy_robo+1.5*dy_robo;
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
plot(pgon_r_right);
%robo_coordinates of left bottom corner
x_car=0;
y_car=dy_robo/2;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
plot(pgon_car);
title('EV3 Robot Simulator - Parallel Parking');
% Draw straight line
x_start = 0;
y_start = 0;
x_end = 6.55*dx_robo;
y_end = dy_robo/2;
num_steps = 100;
straight_line = line([x_start x_end], [y_start+dy_robo/2 y_start+dy_robo/2], 'color', 'g', 'LineWidth', 1.5);
% center_offsetx=7.5;
% center_offsety=5;
% radius = 220;
% center = [1950-center_offsetx, y_car+radius-center_offsety];
% %theta = linspace(deg2rad(270), deg2rad(180), num_steps);
% % Define the angle range for the arc (in radians)
% %theta = linspace(0, pi/2, 100); % 0 to 90 degrees
%
% % Calculate the coordinates of the points on the arc
% theta=0
% x = center(1) + radius * cos(theta);
% y = center(2) + radius * sin(theta);
%
% % Plot the arc
% plot(center(1),center(2),'.')
% hold on
% plot(x, y, '.', 'LineWidth', 1);
%
% center2 = [1650-center_offsetx, 465-center_offsety];
% radius2 = radius;
% theta2 = linspace(deg2rad(0), deg2rad(90), num_steps);
% % Define the angle range for the arc (in radians)
% %theta = linspace(0, pi/2, 100); % 0 to 90 degrees
%
% % Calculate the coordinates of the points on the arc
% xarc2 = center2(1) + radius2 * cos(theta2);
% yarc2 = center2(2) + radius2 * sin(theta2);
%
% % Plot the arc
% plot(center2(1),center2(2),'.')
% hold on
% plot(xarc2, yarc2, '.', 'LineWidth', 1);
vehicle_speed=166
% Move the rectangle along the straight line
for i = 1:118
% Calculate the x-coordinate for each step
%clf(f1);
axis([-2*dx_robo 11*dx_robo -2*dy_robo 4*dy_robo]);
x_step = x_end * (i / num_steps);
x_car=x_car+(vehicle_speed*cos(0)*sample_time);
y_car=y_car+(vehicle_speed*sin(0)*sample_time);
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
plot(pgon_car);
hold on;
%poly1 = rotate(pgon_car,45*i/num_steps,[x_car y_car]);
%poly1.Vertices
%plot(poly1);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.')
hold on;
plot(x_car, y_car, '.')
% Update rectangle position
%set(rect, 'Position', [x1_line, y1_line, x2_line, y2_line]);
%poly1 = rotate(rect,45);
%drawnow;
%pause(sample_time/1000);
end
% Move the rectangle along the straight line
%arc1
x=x_car;
y=y_car;
theta=0;
speed=76;
for i = 1:54
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-2*dx_robo 11*dx_robo -2*dy_robo 4*dy_robo]);
del_theta = -speed/wheel_base*tan(steer_angle);
theta=theta+(del_theta*0.1);
rad2deg(theta)
x_car=x_car+(-speed)*cos(theta)*0.1;
y_car=y_car+(-speed)*sin(theta)*0.1;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
%plot(pgon_car);
%hold on;
actual_car = rotate(pgon_car,rad2deg(theta),[x_car y_car]);
%actual_car.Vertices
plot(actual_car);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.', 'LineWidth', 1);
pause(sample_time);
end
for i = 1:18
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-02*dx_robo 11*dx_robo -02*dy_robo 4*dy_robo]);
del_theta = -speed/wheel_base*-tan(steer_angle);
theta=theta+(del_theta*0.1);
rad2deg(theta)
x_car=x_car+(-speed)*cos(theta)*0.1;
y_car=y_car+(-speed)*sin(theta)*0.1;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
%plot(pgon_car);
%hold on;
actual_car = rotate(pgon_car,rad2deg(theta),[x_car y_car]);
%actual_car.Vertices
plot(actual_car);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.', 'LineWidth', 1);
pause(sample_time);
end
speed=166;
for i = 1:5
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-02*dx_robo 11*dx_robo -02*dy_robo 4*dy_robo]);
del_theta = speed/wheel_base*tan(steer_angle);
theta=theta+(del_theta*0.1);
rad2deg(theta)
x_car=x_car+(speed)*cos(theta)*0.1;
y_car=y_car+(speed)*sin(theta)*0.1;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
%plot(pgon_car);
%hold on;
actual_car = rotate(pgon_car,rad2deg(theta),[x_car y_car]);
%actual_car.Vertices
plot(actual_car);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.', 'LineWidth', 1);
pause(sample_time*2);
end
for i = 1:10
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-02*dx_robo 11*dx_robo -02*dy_robo 4*dy_robo]);
del_theta = -speed/wheel_base*-tan(steer_angle);
theta=theta+(del_theta*0.1);
rad2deg(theta)
x_car=x_car+(-speed)*cos(theta)*0.1;
y_car=y_car+(-speed)*sin(theta)*0.1;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
%plot(pgon_car);
%hold on;
actual_car = rotate(pgon_car,rad2deg(theta),[x_car y_car]);
%actual_car.Vertices
plot(actual_car);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.', 'LineWidth', 1);
pause(sample_time);
end
speed=166;
for i = 1:2
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-02*dx_robo 11*dx_robo -02*dy_robo 4*dy_robo]);
del_theta = speed/wheel_base*tan(steer_angle);
theta=theta+(del_theta*0.1);
rad2deg(theta)
x_car=x_car+(speed)*cos(theta)*0.1;
y_car=y_car+(speed)*sin(theta)*0.1;
x1 = x_car; % x-coordinate of top-left corner
y1 = y_car + dy_robo/2; % y-coordinate of top-left corner
x2 = x_car; % x-coordinate of bottom-right corner
y2 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
x3 = x_car+dx_robo; % x-coordinate of bottom-right corner
y3 = y_car + dy_robo/2; % y-coordinate of bottom-right corner
x4 = x_car+dx_robo; % x-coordinate of bottom-right corner
y4 = y_car - dy_robo/2; % y-coordinate of bottom-right corner
% Initialize rectangle plot
pgon_car = polyshape([x1 x2 x4 x3],[y1 y2 y4 y3]);
pgon_r_left = polyshape([r_left_x1 r_left_x2 r_left_x4 r_left_x3],[r_left_y1 r_left_y2 r_left_y4 r_left_y3]);
pgon_c_line = polyshape([center_line_x1 center_line_x2 center_line_x4 center_line_x3],[center_line_y1 center_line_y2 center_line_y4 center_line_y3]);
pgon_r_right = polyshape([r_right_x1 r_right_x2 r_right_x4 r_right_x3],[r_right_y1 r_right_y2 r_right_y4 r_right_y3]);
%plot(pgon_car);
%hold on;
actual_car = rotate(pgon_car,rad2deg(theta),[x_car y_car]);
%actual_car.Vertices
plot(actual_car);
hold on;
plot(pgon_r_left);
hold on;
plot(pgon_c_line);
hold on;
plot(pgon_r_right);
hold on;
plot(x_car, y_car, '.', 'LineWidth', 1);
pause(sample_time*2);
end