diff --git a/simulation/addcarstoplocation.py b/simulation/addcarstoplocation.py new file mode 100644 index 0000000000000000000000000000000000000000..54b3e6569fdd7b47e16f9ef2caaeb818da26c9aa --- /dev/null +++ b/simulation/addcarstoplocation.py @@ -0,0 +1,104 @@ +import os, sys +import traci +import traci.constants as tc +import optparse +import random +import sumolib +import traci # noqa +import csv +import PySimpleGUI as sg +import Queue + +# Very basic form. Return values as a list + + +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'") + +from sumolib import checkBinary # noqa + + +def get_options(): + optParser = optparse.OptionParser() + optParser.add_option("--nogui", action="store_true", + default=False, help="run the commandline version of sumo") + options, args = optParser.parse_args() + return options + + +def run(): + step = 0 + while step < 3600: + traci.simulationStep() +# this is the main entry point of this script 这是这个脚本的主要入口 +if __name__ == "__main__": + options = get_options() + # this script has been called from the command line. It will start sumo as a + # server, then connect and run 这个脚本已经从命令行调用。它将启动 sumo 作为服务器,然后连接和运行 + if options.nogui: + sumoBinary = checkBinary('sumo') + else: + sumoBinary = checkBinary('sumo-gui') + # this is the normal way of using traci. sumo is started as a 这是使用 traci 的正常方式。 + # subprocess and then the python script connects and runs 相扑是从一个子进程开始的,然后 python 脚本连接并运行 + traci.start([sumoBinary, "-c", "exa_traci.sumocfg", "--tripinfo-output", "tripinfo.xml"]) + + for step in range(0, 3600): + # while traci.simulation.getMinExpectedNumber() > 0 : + traci.simulationStep() + # if traci.simulation.getTime() == 20: + # 弹窗添加车辆 + layout = [ + [sg.Text('Please enter Vehicle stoping location')], + [sg.Text('Name', size=(10, 1)), sg.InputText('')], + [sg.Text('startedge', size=(10, 1)), sg.InputText(''), sg.Text('endedge', size=(10, 1)), sg.InputText('')], + [sg.Text('x', size=(10, 1)), sg.InputText(''), sg.Text('y', size=(10, 1)), sg.InputText('')], + [sg.Text('waiting time', size=(10, 1)), sg.InputText('')], + [sg.Button('ok'), sg.Button('cancel')] + ] + # Create the Window=================================2 + window = sg.Window('window titel', layout=layout) + # Event Loop to process "events" and get the "values" of the inputs + while True: + event, values = window.read() # ====================3 + # if user closes window or clicks cancel + if event in (None, 'cancel'): + break; + # print('add:', values[0], values[1], values[2]) + print('add:', values[0], values[1], values[2],values[3], values[4],values[5]) + Queue.vehcleData_queue.put(values[0]) + id = Queue.vehcleData_queue.get() + print("id",id) + Queue.vehcleData_queue.put(values[1]) + o = Queue.vehcleData_queue.get() + print("startedge",o) + Queue.vehcleData_queue.put(values[2]) + d =Queue.vehcleData_queue.get() + print("endedge",d) + Queue.vehcleData_queue.put(values[3]) + xxxx = float(Queue.vehcleData_queue.get()) + print("x",xxxx) + Queue.vehcleData_queue.put(values[4]) + yyyy = float(Queue.vehcleData_queue.get()) + print("y",yyyy) + Queue.vehcleData_queue.put(values[5]) + waitingtime = Queue.vehcleData_queue.get() + print("waiting time", waitingtime) + + # traci.simulation.convertRoad(x=xxxx, y=yyyy,isGeo=True) # 根据坐标获取车辆停车点 + edgeID, lanePosition, laneIndex =traci.simulation.convertRoad(x=xxxx, y=yyyy) + # edgeID, lanePosition, laneIndex = traci.simulation.convertRoad(-369, 14) # 根据坐标获取车辆停车点 + print(edgeID, lanePosition, laneIndex) + + traci.route.add(routeID="newroad",edges=[o,d]) + + traci.vehicle.add(vehID=id, routeID="newroad",typeID="reroutingType") + + traci.vehicle.setStop(vehID=id, edgeID=edgeID, pos=lanePosition, duration=waitingtime, flags=0, startPos=5) + + run() + traci.close() + sys.stdout.flush() \ No newline at end of file