-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraci_tryout.py
66 lines (53 loc) · 2.46 KB
/
traci_tryout.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
import traci
import sumolib
import sys
nets_file =
try:
# Define paths to SUMO binary, network file, and route file
sumo_binary = "sumo-gui" # Use "sumo" for non-GUI mode
# nets_file = "path/net.xml" # Replace with actual path to your net.xml
# routes_file = "path/to/rou.xml" # Replace with actual path to your rou.xml
# Command to start SUMO with TraCI connection
sumo_cmd = [sumo_binary, "-n", nets_file, "-r", routes_file, "--start"]
# Attempt to start SUMO with the specified command
try:
traci.start(sumo_cmd, label="sim1")
except traci.exceptions.TraCIException as e:
print(f"Failed to start TraCI: {e}")
sys.exit(1)
print("TraCI connected successfully.")
# Main simulation loop with safe TraCI operations
for step in range(1000):
try:
traci.simulationStep() # Advance simulation by one step
# Check vehicle count on a specific edge
edge_id = "t_e" # Replace with actual edge ID
try:
vehicle_count = traci.edge.getLastStepVehicleNumber(edge_id)
print(f"Step {step}, Vehicle count on {edge_id}: {vehicle_count}")
except traci.exceptions.TraCIException as e:
print(f"Error retrieving vehicle count for edge {edge_id}: {e}")
# Get traffic light phase and queue length
tls_id = "t" # Replace with actual traffic light ID
try:
phase = traci.trafficlight.getPhase(tls_id)
print(f"Traffic light {tls_id} phase: {phase}")
except traci.exceptions.TraCIException as e:
print(f"Error retrieving traffic light phase for {tls_id}: {e}")
# Pause to inspect values for each step
input("Press Enter to continue to the next step...")
except KeyboardInterrupt:
print("\nSimulation interrupted by user.")
break # Safely exit the loop if user interrupts
except (FileNotFoundError, traci.exceptions.TraCIException) as e:
print(f"Error in setup or simulation: {e}")
except Exception as e:
print(f"Unexpected error occurred: {e}")
finally:
# Ensure that TraCI closes safely even if an error occurs
try:
if traci.isLoaded():
traci.close()
print("TraCI connection closed safely.")
except traci.exceptions.TraCIException as e:
print(f"Error while closing TraCI: {e}")