-
Notifications
You must be signed in to change notification settings - Fork 6
/
Intersection.m
186 lines (171 loc) · 6.48 KB
/
Intersection.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
% Author : Mohammad Khayatian
% Arizona State University
clc;
clear all;
close all;
rng(4444);
% for iter = 1:6
flows = 0.001:0.001:0.02;
for op = 1:length(flows)
% v = VideoWriter('mohi.avi'); % for video capturing
% open(v); % for video capturing
rng(441); % load random number generator with seed = 4444;
% Parameters
SimulationTime = 360*1; % Seconds
timer = 60; % traffic light timer period
YL=4; % yellow time
StepTime = 0.1; % Seconds
flow = flows(op); % Car/Second/Road
SpawnThreshold = 1/flow; % Seconds
NumberOfRoads = 4; % DON'T CHANGE
NumberOfLanesPerRoad = 3; % DON'T CHANGE
IMWidth = 60;
IntersectionBounds = struct;
IntersectionBounds.xb1 = -200;
IntersectionBounds.xb2 = 0;
IntersectionBounds.xb3 = IMWidth;
IntersectionBounds.xb4 = IMWidth -IntersectionBounds. xb1;
IntersectionBounds.yb1 = IntersectionBounds.xb1;
IntersectionBounds.yb2 = IntersectionBounds.xb2;
IntersectionBounds.yb3 = IntersectionBounds.xb3;
IntersectionBounds.yb4 = IntersectionBounds.xb4;
laneWidth = (IntersectionBounds.xb3-IntersectionBounds.xb2)/(NumberOfLanesPerRoad*2);
TransmitLine = 100*1;
TurnSpace = 2;
averageDelay = 0;
%% Print parameters
printStep = 200000;
printLabel = 2; %% no print = 0, print ID = 1, print Speed = 2;
print3D = 0; %% 3D = 1, 2D = 0;
%% Car Parameters for generation
CarLength = 6*(4.0);
CarWidth = 2;
L = 5;
amax = 5;
amin = -6;
minSpeed = 11.2;
maxSpeed = 22.3;
CarGenerationDuration = SimulationTime - 50;
%% Network
Log = 0; % 1 for logging all packets
%% Intersection Manager for assignemt
method = 'Crossroads';
% method = 'RIM';
% method = 'TrafficLight';
ComputationSpeedFactor = 10;
RequestedVehiclesList = [];
Vmax = maxSpeed;
Vmin = 2;
%% Simulation
count = 1;
WCRTD = 1.1*1; %WORST CASE ROUND-TRIP DELAY (Seconds)
WCND = 0.1; %WORST-CASE NETWORK DELAY (Seconds)
time = 0;
ID = 0;
%% flow rate measurement
incoming = 0; % # of incoming vehicles
outgoing = 0; % # of outgoing vehicles
t2Window = 100;
t1Widnow = 200;
%%
Distancethreshold = sqrt((CarLength/2)^2 + (CarWidth/2)^2) + 0.1;
failureCheck = 1;
VehicleList = [];
Network = [];
failures = [];
c1 = 1;
GeneratedCarTimeStamp = SpawnThreshold * rand(1,NumberOfRoads * NumberOfLanesPerRoad);
while (time < SimulationTime)
tic
time = count * StepTime;
%% Vehicle Generation
for Lane = 1 : NumberOfRoads * NumberOfLanesPerRoad
if (time >= GeneratedCarTimeStamp(Lane) && time < CarGenerationDuration)
ID = ID + 1;
if time < t1Widnow && time > t2Window
incoming = incoming + 1;
end
VehicleList = [VehicleList; generateCar(Lane,ID,IntersectionBounds,laneWidth,minSpeed,maxSpeed,time,method)];
GeneratedCarTimeStamp(Lane) = time + SpawnThreshold + rand;
end
end
%% Vehicles
if ~isempty(VehicleList)
[Network, VehicleList] = SendToNetwork(VehicleList, Network, IntersectionBounds, TransmitLine,time,WCND,Log);
[Network, VehicleList] = ReceiveFromNetwork(VehicleList, Network,time);
VehicleList = PathPlanning(VehicleList,laneWidth,IntersectionBounds,TurnSpace,Vmax,TransmitLine,StepTime);
VehicleList = ACC(VehicleList,CarLength,amin); % Adaptive Cruise Control
VehicleList = vehicleDynamics(VehicleList,L,StepTime,amax,amin,time,method,TransmitLine,Vmax,Vmin,IntersectionBounds,CarLength);
end
%% Log the speed
% if ~isempty(VehicleList)
% for idd = 1:ID
% for car = 1:length(VehicleList)
% if VehicleList(car).ID==idd
% AllCarsVelocity(count,idd)=VehicleList(car).speed;
% AllCarsAcc(count,idd)=VehicleList(car).acceleration;
% end
% end
% end
% end
%% Remove out of bound vehicles
car = 1;
while car < length(VehicleList) + 1
if (VehicleList(car).position.x > IntersectionBounds.xb4) || ...
(VehicleList(car).position.x < IntersectionBounds.xb1) || ...
(VehicleList(car).position.y < IntersectionBounds.yb1) || ...
(VehicleList(car).position.y > IntersectionBounds.yb4)
averageDelay(c1) = time - VehicleList(car).spawnTime;
c1 = c1 + 1;
if time < t1Widnow && time > t2Window
outgoing = outgoing + 1;
end
VehicleList(car)=[];
car = car - 1;
end
car=car+1;
end
%% Intersection Manager (IM)
for iteration = 1 : ComputationSpeedFactor
[Network, RequestedVehiclesListNew] = IntersectionManagement(Network, RequestedVehiclesList,...
Vmax,Vmin,laneWidth,TransmitLine,time,CarLength,WCRTD,WCND,Log,method,IMWidth);
RequestedVehiclesList = RequestedVehiclesListNew;
end
%% drawing
if rem(count,printStep)==0
% text(80,100,'time');text(100,100,num2str(time));
% text(80,120,'Network');text(120,120,num2str(length(Network)));
ax = gcf;
text(-100,-100,num2str(time))
if print3D == 1
drawVehicle3D(VehicleList,CarLength,CarWidth,printLabel)
drawIM3D(IntersectionBounds,TransmitLine,laneWidth)
zlim([0 40]);
view(30,85)
ax.Position = [438 300 1245 604];
else
drawVehicle(VehicleList,CarLength,CarWidth,printLabel)
drawIM(IntersectionBounds,TransmitLine,laneWidth,method,time,timer,YL)
ax.Position = [123 18 1281 963];
end
grid on
axis ([IntersectionBounds.xb1 IntersectionBounds.xb4 IntersectionBounds.yb1 IntersectionBounds.yb4])
pause(0.00001)
% frame = getframe(ax); % for video capturing
% writeVideo(v,frame); % for video capturing
cla
end
count = count + 1;
elpsed1(count+1)=toc;
end
drawIM(IntersectionBounds,TransmitLine,laneWidth,method,time,timer,YL)
axis ([IntersectionBounds.xb1-10 IntersectionBounds.xb4+10 IntersectionBounds.yb1-10 IntersectionBounds.yb4+10])
% disp(failures)
AVG(op) = sum(averageDelay)/ID;
% [ID sum(averageDelay)]
% AVG(iter,op) = sum(averageDelay)/ID;
end
% end
% end
AVG =AVG';
% close(v); % for video capturing