问题
I am new in SUMO. I have a .net, a .rou (containing 300 vehicles with vehicle depart, id, route edges attributes), a .trip and a .sumoconfig file representing a traffic scenario. I want to create these 300 vehicles as python Vehicle object generating from a Vehicle class containing other functions to communicate with each other. How can they connect dynamically to sumo and get linked to those 300 cars in the scenario? I can write a server that listens for these objects and accepts connection but what is the way of forwarding or linking them to those sumo scenario vehicles? Any hint or reference or link to code will be highly appreciated.
回答1:
The example which is closest to what you want to achieve is probably the CityMobil tutorial, see http://sumo.dlr.de/wiki/Tutorials/CityMobil but it boils down to something like that:
import traci
import traci.constants as tc
traci.start(["sumo", "my.sumocfg"])
traci.simulation.subscribe()
while True:
moveNodes = {}
traci.simulationStep()
# update the position of all running vehicles
for veh, subs in traci.vehicle.getAllSubscriptionResults().items():
moveNodes[veh] = (subs[tc.VAR_ROAD_ID], subs[tc.VAR_LANEPOSITION])
# add new departed vehicles
for v in traci.simulation.getSubscriptionResults()[tc.VAR_DEPARTED_VEHICLES_IDS]:
traci.vehicle.subscribe(v)
subs = traci.vehicle.getSubscriptionResults(v)
moveNodes[v] = (subs[tc.VAR_ROAD_ID], subs[tc.VAR_LANEPOSITION])
This gives you a map storing up to date positions for all vehicles. Please note that the map is rebuilt every step from scratch and thus you do not need to care about leaving vehicles. If your vehicle objects persist for longer you will need to delete them as soon as there are no more subscription results for them.
来源:https://stackoverflow.com/questions/54014806/multiple-python-client-objects-connecting-to-one-sumo-simulation