-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirection.py
68 lines (56 loc) · 2.25 KB
/
Direction.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
import traci
from Credits import DistanceFromJunction
# list for direction of neighbouring cars
direction = []
# main function for direction
def direction_check(neighbours):
for neighbour in range(0, len(neighbours)):
vehicles = traci.vehicle.getIDList()
if neighbours[neighbour] in vehicles:
# checks direction for each vehicle
route_direction(neighbours[neighbour])
# checks if clash after adding to direction list
return check_clash(neighbours)
def route_direction(neighbour):
# returns route ID which is direction
direc = traci.vehicle.getRouteID(neighbour)
# adds to direction list if not in list
if direc not in direction:
direction.append(direc)
# checks if there is a clash in direction
def check_clash(neighbours):
# east and south clash so will set stop
if ('E' in direction) and ('S' in direction):
set_stop(neighbours)
return True
# east and north clash so will set stop
if ('E' in direction) and ('N' in direction):
set_stop(neighbours)
return True
# west and south clash so sets stop
if ('W' in direction) and ('S' in direction):
set_stop(neighbours)
return True
# west and north clash so sets stop
if ('W' in direction) and ('N' in direction):
set_stop(neighbours)
return True
return False
# function to set the stop
def set_stop(neighbours):
for i in range(0, len(neighbours)):
vehicles = traci.vehicle.getIDList()
if neighbours[i] in vehicles:
# returns current edge that car is on
current_edge = traci.vehicle.getRoadID(neighbours[i])
# adds _0 to obtain lane of car
full_current_edge = current_edge + "_0"
# gets distance of lane that car is on
distance = traci.lane.getLength(full_current_edge)
# only sets stop if car has not reached junction, else no need for stop
if DistanceFromJunction.passed_junction(neighbours[i]):
try:
traci.vehicle.setStop(neighbours[i], traci.vehicle.getRoadID(neighbours[i]), distance, 0,
100, 0)
except traci.exceptions.TraCIException:
pass