#First interfacing program
#TraCI
import os #OS module which provides function for interacting with
OS
import sys #to communicate with other software
import optparse #a python module to write command line tools
import subprocess #allows to communicate b/w pyhton script and cmd.exe
import random
#check for environment variables
'''
if 'sumo-0.27.1' in os.environ:
tools = os.path.join(os.environ['sumo-0.27.1'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variables 'sumo-0.27.1'")
'''
try:
sys.path.append(os.path.join(os.path.dirname(
__file__), '..', '..', '..', '..', "tools")) # tutorial in tests
sys.path.append(os.path.join(os.environ.get("sumo-0.27.1", os.path.join(
os.path.dirname(__file__), "..", "..", "..")), "tools")) #
tutorial in docs
from sumolib import checkBinary
except ImportError:
sys.exit(
"please declare environment variable 'SUMO_HOME' as the root
directory of your sumo installation (it should contain folders 'bin',
'tools' and 'docs')")
PORT = 8813 #port used for communicating with sumo instance
#sumo is started as a subprocess
sumoBinary =
"C:/Users/vr2002/Downloads/sumo-win64-0.27.1/sumo-0.27.1/bin/sumo-gui.exe"
#sumoBinary is a variable which holds the path to sumo-gui, you can any
name for this variable
sumoProcess = subprocess.Popen([sumoBinary, "-c", "att2.sumo.cfg",
"--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)
#For any time step I am saving all the vehicle IDs in a text file and then
verifying if the vehicle v1 is in the text file (i.e; exited system).
#If it's in the system it's stopped irrespective of it's position depending
on state of signal at node B2.
import traci
traci.init(PORT)
step = 0
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
get_id = traci.simulation.getArrivedIDList() #list of vehicles
exited the system in current time step
with open ("vid3.txt", "a") as vehicle_id: #opened a text file
named vid3 located in same folder, using with closes it automatically once
writing work is done
vehicle_id.write (str(get_id) + '\n') #stored all the vehicle
IDs as a string
with open ("vid3.txt", "r") as read_vehicle_id: #opened a text file
named vid3 located in same folder,
veh_id = read_vehicle_id.read() #reading the data
stored in text file
if 'v1' in veh_id: #to verify if
string 'v1' is present in list veh_id
continue
else:
if traci.trafficlights.getPhase("B2") == 0: #signal at B2 in
state GGrr
traci.vehicle.setSpeed("v1", 0) #stops the vehicle
'v1'
else:
traci.vehicle.setSpeed("v1", 20) #sets the speed of
'v1' to 20 units
step = step + 1
traci.close()
Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center
585-754-7133
------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
sumo-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sumo-user