My scenario:
I have set up a working “net” with edges, junctions and lanes. Imported from Open Streetmap and it all works wel. I made routes and trips with 0d2trips and duarouter.
Now I want to interact with the “vehicles”. Say I have located a beginning (edge or junction) and an end (edge or junction) in a “net”… and when a “vehicle” passes a junction or edge I want a signa land obtain the “vehicle” information. Most likely its ID, because then I can obtain position, speed, etc..
How can I do this? I have tried something in TRACI in Python…. but I cannot “obtain” the “vehicle ID” and the trigger/event (for me) is uncertain if I did this right?
I “add” the codesnippet and not working code.
[code]
import os, sys
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
sumoBinary = "D:/eclipse/Sumo/bin/sumo-gui"
sumoCmd = [sumoBinary, "-c", "D:/eclipse/Sumo/autobahn - kopie.sumocfg"]
import traci
import traci.constants as tc
traci.start(sumoCmd)
junctionID = "6499463362"
traci.junction.subscribeContext(junctionID, tc.CMD_GET_VEHICLE_VARIABLE, 25, [tc.VAR_SPEED, tc.VAR_WAITING_TIME])
traci.edge.subscribeContext("6861416#3", tc.CMD_GET_LANE_VARIABLE,50)
step = 0
j_info = {}
while step < 1000:
traci.simulationStep()
#print("Vehicle:", tc.CMD_GET_VEHICLE_VARIABLE)
pos_x, pos_y = traci.junction.getPosition(junctionID)
lon, lat = traci.simulation.convertGeo(pos_x, pos_y)
print("Lat:"+str(lat));
print("Lon:"+str(lon));
print("Time:", traci.simulation.getTime())
print("Vehicles:", traci.simulation.getLoadedNumber())
data2 = tc.CMD_GET_VEHICLE_VARIABLE
vehicles=traci.vehicle.getIDList()
i=0
for i in range(0,len(vehicles)):
print(len(vehicles))
print(vehicles[i])
edge = traci.edge.getSubscriptionResults("6861416#3")
print("EdgeID:", edge)
#if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
# traci.trafficlight.setRedYellowGreenState("0", "GrGr")
#"{'283': {64: 27.72624641781673, 122: 0.0}}")
step += 1
#for det in traci.lane.getIDList():
# print(" last=", traci.lane.getLastStepVehicleIDs(det))
junction_info = traci.junction.getSubscriptionResults(junctionID)
print("Data:", str(junction_info ))
if junction_info:
j_info = junction_info
print("Data:", traci.vehicle.getAngle(j_info))
if str(type(junction_info )) == "<type 'dict'>":
for car in junction_info .items():
print("YES")
print("Car:",car)
#if str(type(data)) == "<type 'dict'>":
# for car in data:
#agent = agents[car]
#agent.collectSumoData(car, data[car])
traci.close()
[\code]
Thanks Ben