-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
188 lines (145 loc) · 4.78 KB
/
main.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
% PEDRO ESCOBOZA
function main()
clf;
clear;
clc;
% Information display properties ------------------------------------------
SHOW_BANNERS = true;
PRINT_TO_CONSOLE = false;
USE_KM = false;
DRAW_CENTER_OF_ROTATION = false;
DRAW_STEERING_DIRECTION = true;
DRAW_ACCELERATION_DIRECTION = true;
DRAW_VELOCITY_DIRECTION = true;
DRAW_AABB = false;
% Simulation properties ---------------------------------------------------
dt = double(0.1);
NUM_TICKS = 10000;
SECONDS_TO_START = 2;
% Plot properties ---------------------------------------------------------
pbaspect([1,1,1]);
spacer = 1.5;
hold on;
axis equal;
% Road properties ---------------------------------------------------------
SIZE_MULTIPLIER = 100;
TRACK_WIDTH = 12;
equation = [];
xo = 3;
xf = 28;
BORDER_LINES_DRAWING_DELTA_STEP = 0.01;
path = Path(xo,xf,BORDER_LINES_DRAWING_DELTA_STEP,equation,TRACK_WIDTH,SIZE_MULTIPLIER);
% Stands' properties ------------------------------------------------------
% (Reference position for offset is 20.5 m from road CENTER line)
CUSTOM__Y_OFFSET = false;
Y_OFFSET = -5;
if ~CUSTOM__Y_OFFSET
Y_OFFSET = (20.5 + TRACK_WIDTH/TRACK_WIDTH/2) / SIZE_MULTIPLIER; % 20 + 5% meters safe distance
else
Y_OFFSET = Y_OFFSET/SIZE_MULTIPLIER;
end
st_a_x = [22.160389436299898,22.9603894363001,22.9603894363001,22.160389436299898];
st_a_y = [30.13829615269992,30.13829615269992,30.238296152700443,30.238296152699306] + Y_OFFSET;
st_b_x = [7.721288267899989,8.521288267900013,8.521288267900013,7.721288267899989];
st_b_y = [18.294650411299987,18.294650411299703,18.394650411299942,18.394650411299942] - Y_OFFSET;
stands = [Box(st_a_x,st_a_y,SIZE_MULTIPLIER,'k'),Box(st_b_x,st_b_y,SIZE_MULTIPLIER,'k')];
for i = 1:numel(stands)
AABB{i} = stands(i).m_poly.Vertices;
end
% Camera properties -------------------------------------------------------
CAM_AXIS_SIZE = 100;
CAMERA_FOLLOW_FIRST = true;
cameraStaticAxis = [path.m_beginPos(1)-50,path.m_endPos(1)+50,1550,3300];
camera = Camera(path.m_beginPos,CAM_AXIS_SIZE);
% Car properties ----------------------------------------------------------
NUM_CARS = 5;
AXLE_DISTANCE = 5;
ALL_SAME_MASS = false;
MASS = 700; % (kg)
arr = ones(1,NUM_CARS);
pos_x = (5+path.m_beginPos(1)).*arr;
pos_y = path.m_endPos(2).*arr;
for i = 2:NUM_CARS
pos_x(i) = pos_x(i-1) - spacer;
pos_y(i) = pos_y(i-1) + spacer;
end
ai = ["crasher","cautious","cautious","turtle","speer"];
ang = 290.*arr; %(deg)
mass = [660,700,660,665,1200]; %(kg)
if ALL_SAME_MASS
mass = MASS.*arr;
end
cof = 0.35.*arr; %(coefficient of friction)
frontalArea = 2.2.*arr; %(m^2)
axle = AXLE_DISTANCE.*arr; %(m)
speedLimit = 0; %(m/s)
color = ['r','g','b','y','k'];
% Car initialization
for i = 1:NUM_CARS
cars(i) = Car(true,CarAi(path,ai(i),Y_OFFSET*SIZE_MULTIPLIER),[pos_x(i),pos_y(i)],ang(i),mass(i),cof(i),frontalArea(i),axle(i),speedLimit,color(i),DRAW_AABB);
end
% Banner properties -------------------------------------------------------
BANNER_DISP = [15,15];
if ~CAMERA_FOLLOW_FIRST
BANNER_DISP = 10.*BANNER_DISP;
end
BANNER_SPACER = BANNER_DISP./NUM_CARS;
if SHOW_BANNERS
for i = 1:NUM_CARS
banners(i) = Banner(cars(i).m_p,"P:(%.1f,\t%.1f) m | V: %.2f m/s;",BANNER_DISP);
end
end
% Draw the road
drawPath(path);
% Draw the cars in place
for n =1: NUM_CARS
drawCar(cars(n),DRAW_CENTER_OF_ROTATION,DRAW_STEERING_DIRECTION,DRAW_VELOCITY_DIRECTION, DRAW_VELOCITY_DIRECTION);
end
% Draw the stands
drawBoxes(stands);
axis equal;
pause(SECONDS_TO_START);
for i = 1:NUM_TICKS
clf;
axis equal;
hold on;
drawPath(path);
% Draw the stands
drawBoxes(stands);
for n = 1:NUM_CARS
% Update each car instance
updateCar(cars(n),dt,stands);
% Change car inputs
drive(cars(n).m_ai,cars(n));
end
% Graphics separated from calculations to manage plot properties
% independently
for n = 1:NUM_CARS
% Display the cars' graphics
drawCar(cars(n),DRAW_CENTER_OF_ROTATION,DRAW_STEERING_DIRECTION,DRAW_VELOCITY_DIRECTION, DRAW_VELOCITY_DIRECTION);
end
% Update the camera position to follow the rightmost car
if CAMERA_FOLLOW_FIRST
updateCamera(camera,cars)
else
axis(cameraStaticAxis);
end
% Update the banners
if SHOW_BANNERS
for i = 1:NUM_CARS
disp = BANNER_DISP + (i*BANNER_SPACER).*[1,1];
%if ~mod(i,2)
% disp = -disp;
%end
updateBanner(banners(i),cars(i).m_p,disp,[cars(i).m_p,vectorMagnitude(cars(i).m_v)]);
end
end
drawnow;
% Print car information to console
if PRINT_TO_CONSOLE
printCars(cars,i*dt,useKm);
end
end
fprintf("\n===== End of simulation =====\n\n Number of ticks: %d\n",NUM_TICKS);
end
% PEDRO ESCOBOZA