-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perpendicular_Parking_Simulator_with_Parking_space_detection.m
259 lines (218 loc) · 8.2 KB
/
Perpendicular_Parking_Simulator_with_Parking_space_detection.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
close all
%% Perpendicular 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=[50,100,1200,250]
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*dx_robo;
r_left_y3=2*dy_robo;
r_left_y4=2*dy_robo+1.5*dx_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*dy_robo;
center_line_x4=5*dx_robo+1.5*dy_robo;
center_line_y1=2*dy_robo+1.6*dx_robo;
center_line_y2=2*dy_robo+1.5*dx_robo;
center_line_y3=2*dy_robo+1.6*dx_robo;
center_line_y4=2*dy_robo+1.5*dx_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*dy_robo;
r_right_x2=5*dx_robo+1.5*dy_robo;
r_right_x3=5*dx_robo+1.5*dy_robo+3*dx_robo;
r_right_x4=5*dx_robo+1.5*dy_robo+3*dx_robo;
r_right_y1=2*dy_robo;
r_right_y2=2*dy_robo+1.5*dx_robo;
r_right_y3=2*dy_robo;
r_right_y4=2*dy_robo+1.5*dx_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 rear center
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 - Perpendicular Parking with Parking Spot Detection');
% 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);
%Echoes of Ultrasonic Sensor
intp_line_x = [0 1499 1500 1724 1725 2625];
intp_line_y = [300 300 750 750 300 300];
detect_flag = 0;
parking_spot_detected = 0;
vehicle_speed=166
%Check for Parking Spot
%distance = new2_line_y - y_car;
distance = intp_line_y(3) - intp_line_y(2) - y_car;
if distance > 350 && detect_flag == 0
start_distance = intp_line_x(3);
detect_flag = 1;
parking_spot_detected = 0;
end
if detect_flag == 1
park_distance = intp_line_x(5) - start_distance
if park_distance > 200
parking_spot_detected = 1;
else
parking_spot_detected = 0;
end
if distance < 350
detect_flag = 0;
end
end
if parking_spot_detected == 1
% Move the rectangle along the straight line
for i = 1:113
% Calculate the x-coordinate for each step
%clf(f1);
axis([-2*dx_robo 11*dx_robo -3*dy_robo 6*dy_robo]);
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]);
hold on
% Plot Ultrasonic Sensor Echoes
new2_line_y = interp1(intp_line_x, intp_line_y, x_car);
plot(x_car, new2_line_y, 'r*', 'LineWidth', 5)
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, '.')
pause(sample_time/10);
end
%end
% Move the rectangle along the straight line
%arc1
x=x_car;
y=y_car;
theta=0;
speed=76;
for i = 1:53
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-2*dx_robo 11*dx_robo -2*dy_robo 6*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:45
% Calculate the x-coordinate for each step
i
%clf(f1);
axis([-02*dx_robo 11*dx_robo -02*dy_robo 6*dy_robo]);
del_theta = -speed/wheel_base*-tan(0);
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;
end