-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.py
90 lines (68 loc) · 2.98 KB
/
intersection.py
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
from lane import *
class Intersection:
def __init__(self, intersection_id, redlight_duration, greenlight_duration, left_duration,distance_to_intersection, distance_to_next_intersection, final_intersection_id):
self.intersection_id = intersection_id
self.lanes = {
'N': Lane(self, 'N'),
'S': Lane(self, 'S'),
'E': Lane(self, 'E'),
'W': Lane(self, 'W'),
'Nleft': Lane(self, 'Nleft'),
'Sleft': Lane(self, 'Sleft'),
'Eleft': Lane(self, 'Eleft'),
'Wleft': Lane(self, 'Wleft')
}
self.lights = {'N': 'green', 'S': 'green', 'E': 'red', 'W': 'red','Nleft': 'red', 'Sleft': 'red', 'Eleft': 'red', 'Wleft': 'red'}
# self.lights = {'N': 'red', 'S': ['red', 'red'], 'E': ['red', 'red'], 'W': ['red', 'red']}
self.redlight_duration = redlight_duration # in seconds
self.greenlight_duration = greenlight_duration # in seconds
self.left_duration = left_duration
self.distance_to_intersection = distance_to_intersection # in meters
self.distance_to_next_intersection = distance_to_next_intersection # in meters
# self.distance_to_exit = 100 # in meters
self.next_intersection_id = intersection_id + 1
if self.next_intersection_id > final_intersection_id:
self.next_intersection_id = None
# Use four servers to represent whether a certain direction of the intersection is occupied.
# False means that direction is not occupied by vehicle.
self.NorthBound = False
self.SouthBound = False
self.EastBound = False
self.WestBound = False
def change_lights(self, direction):
if self.lights[direction] == 'red':
self.lights[direction] = 'green'
else:
self.lights[direction] = 'red'
def get_intersection_id(self):
return self.intersection_id
def get_light_state(self, direction):
return self.lights[direction]
def get_redlight_duration(self):
return self.redlight_duration
def get_greenlight_duration(self):
return self.greenlight_duration
def get_left_duration(self):
return self.left_duration
def get_distance_to_intersection(self):
return self.distance_to_intersection
def get_distance_to_next_intersection(self):
return self.distance_to_next_intersection
def get_next_intersection_id(self):
return self.next_intersection_id
def get_northbound_state(self):
return self.NorthBound
def get_southbound_state(self):
return self.SouthBound
def get_eastbound_state(self):
return self.EastBound
def get_westbound_state(self):
return self.WestBound
def occupy_northbound(self):
self.NorthBound = True
def occupy_southbound(self):
self.SouthBound = True
def occupy_eastbound(self):
self.EastBound = True
def occupy_westbound(self):
self.WestBound = True