-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation.py
More file actions
372 lines (312 loc) · 13.2 KB
/
Copy pathsimulation.py
File metadata and controls
372 lines (312 loc) · 13.2 KB
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
import random
import time
import threading
import pygame
import sys
# =========================
# 2x2 INTERSECTION GRID + CAMERA
# =========================
TILE_W, TILE_H = 1400, 800
GRID_COLS, GRID_ROWS = 2, 2
NO_OF_INTERSECTIONS = GRID_COLS * GRID_ROWS # 4
# intersection_id layout:
# 0 1
# 2 3
OFFSETS = {
0: (0, 0),
1: (TILE_W, 0),
2: (0, TILE_H),
3: (TILE_W, TILE_H),
}
# Default values of signal timers
defaultGreen = {0: 10, 1: 10, 2: 10, 3: 10}
defaultRed = 150
defaultYellow = 5
noOfSignals = 4
speeds = {'car': 2.25, 'bus': 1.8, 'truck': 1.8, 'bike': 2.5}
vehicleTypes = {0: 'car', 1: 'bus', 2: 'truck', 3: 'bike'}
directionNumbers = {0: 'right', 1: 'down', 2: 'left', 3: 'up'}
# Coordinates of vehicles' start (BASE for one intersection tile)
BASE_x = {'right': [0, 0, 0], 'down': [755, 727, 697], 'left': [1400, 1400, 1400], 'up': [602, 627, 657]}
BASE_y = {'right': [348, 370, 398], 'down': [0, 0, 0], 'left': [498, 466, 436], 'up': [800, 800, 800]}
# Coordinates of signal image and timer (BASE for one intersection tile)
BASE_signalCoods = [(530, 230), (810, 230), (810, 570), (530, 570)]
BASE_signalTimerCoods = [(530, 210), (810, 210), (810, 550), (530, 550)]
# Coordinates of stop lines (BASE for one intersection tile)
BASE_stopLines = {'right': 590, 'down': 330, 'left': 800, 'up': 535}
BASE_defaultStop = {'right': 580, 'down': 320, 'left': 810, 'up': 545}
# Gap between vehicles
stoppingGap = 15
movingGap = 15
pygame.init()
simulation = pygame.sprite.Group()
# =========================
# PER-INTERSECTION STATE
# =========================
signals = [[] for _ in range(NO_OF_INTERSECTIONS)]
currentGreen = [0] * NO_OF_INTERSECTIONS
nextGreen = [(cg + 1) % noOfSignals for cg in currentGreen]
currentYellow = [0] * NO_OF_INTERSECTIONS
# per-intersection x/y spawn stacks and vehicles buckets
x = []
y = []
vehicles = []
for iid in range(NO_OF_INTERSECTIONS):
ox, oy = OFFSETS[iid]
x.append({
'right': [v + ox for v in BASE_x['right']],
'down': [v + ox for v in BASE_x['down']],
'left': [v + ox for v in BASE_x['left']],
'up': [v + ox for v in BASE_x['up']],
})
y.append({
'right': [v + oy for v in BASE_y['right']],
'down': [v + oy for v in BASE_y['down']],
'left': [v + oy for v in BASE_y['left']],
'up': [v + oy for v in BASE_y['up']],
})
vehicles.append({
'right': {0: [], 1: [], 2: [], 'crossed': 0},
'down': {0: [], 1: [], 2: [], 'crossed': 0},
'left': {0: [], 1: [], 2: [], 'crossed': 0},
'up': {0: [], 1: [], 2: [], 'crossed': 0},
})
def stopLine(iid, direction):
ox, oy = OFFSETS[iid]
if direction in ('right', 'left'):
return BASE_stopLines[direction] + ox
return BASE_stopLines[direction] + oy
def defaultStop(iid, direction):
ox, oy = OFFSETS[iid]
if direction in ('right', 'left'):
return BASE_defaultStop[direction] + ox
return BASE_defaultStop[direction] + oy
class TrafficSignal:
def __init__(self, red, yellow, green):
self.red = red
self.yellow = yellow
self.green = green
self.signalText = ""
class Vehicle(pygame.sprite.Sprite):
def __init__(self, iid, lane, vehicleClass, direction_number, direction):
pygame.sprite.Sprite.__init__(self)
self.iid = iid
self.lane = lane
self.vehicleClass = vehicleClass
self.speed = speeds[vehicleClass]
self.direction_number = direction_number
self.direction = direction
self.x = x[iid][direction][lane]
self.y = y[iid][direction][lane]
self.crossed = 0
vehicles[iid][direction][lane].append(self)
self.index = len(vehicles[iid][direction][lane]) - 1
path = "images/" + direction + "/" + vehicleClass + ".png"
self.image = pygame.image.load(path)
if len(vehicles[iid][direction][lane]) > 1 and vehicles[iid][direction][lane][self.index-1].crossed == 0:
prev = vehicles[iid][direction][lane][self.index-1]
if direction == 'right':
self.stop = prev.stop - prev.image.get_rect().width - stoppingGap
elif direction == 'left':
self.stop = prev.stop + prev.image.get_rect().width + stoppingGap
elif direction == 'down':
self.stop = prev.stop - prev.image.get_rect().height - stoppingGap
elif direction == 'up':
self.stop = prev.stop + prev.image.get_rect().height + stoppingGap
else:
self.stop = defaultStop(iid, direction)
# shift spawn stacks
if direction == 'right':
temp = self.image.get_rect().width + stoppingGap
x[iid][direction][lane] -= temp
elif direction == 'left':
temp = self.image.get_rect().width + stoppingGap
x[iid][direction][lane] += temp
elif direction == 'down':
temp = self.image.get_rect().height + stoppingGap
y[iid][direction][lane] -= temp
elif direction == 'up':
temp = self.image.get_rect().height + stoppingGap
y[iid][direction][lane] += temp
simulation.add(self)
def move(self):
iid = self.iid
d = self.direction
w = self.image.get_rect().width
h = self.image.get_rect().height
if d == 'right':
if self.crossed == 0 and self.x + w > stopLine(iid, d):
self.crossed = 1
if ((self.x + w <= self.stop or self.crossed == 1 or (currentGreen[iid] == 0 and currentYellow[iid] == 0)) and
(self.index == 0 or self.x + w < (vehicles[iid][d][self.lane][self.index-1].x - movingGap))):
self.x += self.speed
elif d == 'down':
if self.crossed == 0 and self.y + h > stopLine(iid, d):
self.crossed = 1
if ((self.y + h <= self.stop or self.crossed == 1 or (currentGreen[iid] == 1 and currentYellow[iid] == 0)) and
(self.index == 0 or self.y + h < (vehicles[iid][d][self.lane][self.index-1].y - movingGap))):
self.y += self.speed
elif d == 'left':
if self.crossed == 0 and self.x < stopLine(iid, d):
self.crossed = 1
if ((self.x >= self.stop or self.crossed == 1 or (currentGreen[iid] == 2 and currentYellow[iid] == 0)) and
(self.index == 0 or self.x > (vehicles[iid][d][self.lane][self.index-1].x +
vehicles[iid][d][self.lane][self.index-1].image.get_rect().width + movingGap))):
self.x -= self.speed
elif d == 'up':
if self.crossed == 0 and self.y < stopLine(iid, d):
self.crossed = 1
if ((self.y >= self.stop or self.crossed == 1 or (currentGreen[iid] == 3 and currentYellow[iid] == 0)) and
(self.index == 0 or self.y > (vehicles[iid][d][self.lane][self.index-1].y +
vehicles[iid][d][self.lane][self.index-1].image.get_rect().height + movingGap))):
self.y -= self.speed
# =========================
# SIGNAL CONTROL PER INTERSECTION
# =========================
def initialize(iid):
ts1 = TrafficSignal(0, defaultYellow, defaultGreen[0])
signals[iid].append(ts1)
ts2 = TrafficSignal(ts1.red + ts1.yellow + ts1.green, defaultYellow, defaultGreen[1])
signals[iid].append(ts2)
ts3 = TrafficSignal(defaultRed, defaultYellow, defaultGreen[2])
signals[iid].append(ts3)
ts4 = TrafficSignal(defaultRed, defaultYellow, defaultGreen[3])
signals[iid].append(ts4)
repeat(iid)
def repeat(iid):
while signals[iid][currentGreen[iid]].green > 0:
updateValues(iid)
time.sleep(1)
currentYellow[iid] = 1
# reset stop positions for vehicles on current green direction
for lane in range(0, 3):
for v in vehicles[iid][directionNumbers[currentGreen[iid]]][lane]:
v.stop = defaultStop(iid, directionNumbers[currentGreen[iid]])
while signals[iid][currentGreen[iid]].yellow > 0:
updateValues(iid)
time.sleep(1)
currentYellow[iid] = 0
# reset times
signals[iid][currentGreen[iid]].green = defaultGreen[currentGreen[iid]]
signals[iid][currentGreen[iid]].yellow = defaultYellow
signals[iid][currentGreen[iid]].red = defaultRed
currentGreen[iid] = nextGreen[iid]
nextGreen[iid] = (currentGreen[iid] + 1) % noOfSignals
signals[iid][nextGreen[iid]].red = signals[iid][currentGreen[iid]].yellow + signals[iid][currentGreen[iid]].green
repeat(iid)
def updateValues(iid):
for i in range(0, noOfSignals):
if i == currentGreen[iid]:
if currentYellow[iid] == 0:
signals[iid][i].green -= 1
else:
signals[iid][i].yellow -= 1
else:
signals[iid][i].red -= 1
# =========================
# VEHICLE GENERATION
# =========================
def generateVehicles():
while True:
iid = random.randint(0, NO_OF_INTERSECTIONS - 1)
vehicle_type = random.randint(0, 3)
lane_number = random.randint(1, 2)
temp = random.randint(0, 99)
dist = [25, 50, 75, 100]
if temp < dist[0]:
direction_number = 0
elif temp < dist[1]:
direction_number = 1
elif temp < dist[2]:
direction_number = 2
else:
direction_number = 3
Vehicle(iid, lane_number, vehicleTypes[vehicle_type], direction_number, directionNumbers[direction_number])
time.sleep(1)
class Main:
# start signal controllers (one per intersection)
for iid in range(NO_OF_INTERSECTIONS):
t = threading.Thread(name=f"init_{iid}", target=initialize, args=(iid,), daemon=True)
t.start()
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
# WINDOW size (keep as one tile) - camera scroll shows the rest
screenWidth = TILE_W
screenHeight = TILE_H
screenSize = (screenWidth, screenHeight)
background = pygame.image.load('images/intersection.png')
screen = pygame.display.set_mode(screenSize)
pygame.display.set_caption("SIMULATION - 4 Intersections (Camera View)")
# Loading signal images and font
redSignal = pygame.image.load('images/signals/red.png')
yellowSignal = pygame.image.load('images/signals/yellow.png')
greenSignal = pygame.image.load('images/signals/green.png')
font = pygame.font.Font(None, 30)
# =========================
# WORLD SURFACE + CAMERA
# =========================
WORLD_W = TILE_W * GRID_COLS
WORLD_H = TILE_H * GRID_ROWS
world = pygame.Surface((WORLD_W, WORLD_H))
camera_x = 0
camera_y = 0
CAMERA_SPEED = 25
# Vehicle generation thread
thread2 = threading.Thread(name="generateVehicles", target=generateVehicles, args=(), daemon=True)
thread2.start()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# =========================
# DRAW EVERYTHING TO WORLD
# =========================
world.fill((0, 0, 0))
# draw all intersection backgrounds
for iid in range(NO_OF_INTERSECTIONS):
ox, oy = OFFSETS[iid]
world.blit(background, (ox, oy))
# draw signals + timers
for iid in range(NO_OF_INTERSECTIONS):
ox, oy = OFFSETS[iid]
for i in range(0, noOfSignals):
signalPos = (BASE_signalCoods[i][0] + ox, BASE_signalCoods[i][1] + oy)
timerPos = (BASE_signalTimerCoods[i][0] + ox, BASE_signalTimerCoods[i][1] + oy)
if i == currentGreen[iid]:
if currentYellow[iid] == 1:
signals[iid][i].signalText = signals[iid][i].yellow
world.blit(yellowSignal, signalPos)
else:
signals[iid][i].signalText = signals[iid][i].green
world.blit(greenSignal, signalPos)
else:
if signals[iid][i].red <= 10:
signals[iid][i].signalText = signals[iid][i].red
else:
signals[iid][i].signalText = "---"
world.blit(redSignal, signalPos)
txt = font.render(str(signals[iid][i].signalText), True, white, black)
world.blit(txt, timerPos)
# draw vehicles
for vehicle in simulation:
world.blit(vehicle.image, [vehicle.x, vehicle.y])
vehicle.move()
# =========================
# CAMERA CONTROLS (ARROWS or WASD)
# =========================
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
camera_x = max(0, camera_x - CAMERA_SPEED)
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
camera_x = min(WORLD_W - screenWidth, camera_x + CAMERA_SPEED)
if keys[pygame.K_UP] or keys[pygame.K_w]:
camera_y = max(0, camera_y - CAMERA_SPEED)
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
camera_y = min(WORLD_H - screenHeight, camera_y + CAMERA_SPEED)
# =========================
# SHOW CAMERA VIEW
# =========================
screen.blit(world, (-camera_x, -camera_y))
pygame.display.update()
Main()