如何使用 viens 在 OMNEST 上运行 SUMO python traci 脚本

发布于 2025-01-14 12:43:28 字数 3811 浏览 5 评论 0原文

我正在开发《SUMO VEINS》和《OMNEST》。 要在 OMNEST 上运行相扑文件,相扑文件 (.xml) 的输入将输入到 Veines_launchd 中,后者会找到未使用的端口,启动相扑并桥接相扑和 OMNEST 之间的连接。

我想实时(在模拟期间)控制车辆的行为(速度变化)。 为此,我用 python 语言编写了一个 Traci 脚本,它调用 sumo 配置文件并实时控制车辆速度

我的问题是, 我不知道如何使这个 Traci 脚本(python)通过静脉在 OMNEST 上运行。 我应该在哪里给出这个 python 文件作为输入,以便我可以在 OMNEST 中可视化输出。 我的工作环境是Linux

基于一些研究,我想出了两种方法。

1. TraCIScenarioManager模块

2. Veins_Python

方法1: 据我了解,通过使用TraCIScenarioManager模块,OMNEST可以直接连接到正在运行的相扑。 但我不知道我应该在静脉模块内哪里进行必要的更改以使用TraCIScenarioManager而不是TraCIScenarioManagerLaunchd

方法2:关于veins_python,我从github下载了源文件并执行了上述配置步骤。 我用的是windows10和 版本: Veins5.0、OMNeT++ 5.5.1 和 Python3.6 但是我在配置 Veins_Python 时遇到以下错误。

在此处输入图像描述

我还在 Windows 10 上尝试了最新版本的软件 版本: Veins5.2、OMNEST-5.6.2 和 Python3.10 我仍然遇到同样的错误。

我的 Sumo Traci 脚本是

import traci
import time
import traci.constants as tc
import pytz
import datetime
from random import randrange
import pandas as pd


def getdatetime():
        utc_now = pytz.utc.localize(datetime.datetime.utcnow())
        currentDT = utc_now.astimezone(pytz.timezone("Asia/Tokyo"))
        DATIME = currentDT.strftime("%Y-%m-%d %H:%M:%S")
        return DATIME

def flatten_list(_2d_list):
    flat_list = []
    for element in _2d_list:
        if type(element) is list:
            for item in element:
                flat_list.append(item)
        else:
            flat_list.append(element)
    return flat_list


sumoCmd = ["sumo-gui", "-c", "osm.sumocfg"]
traci.start(sumoCmd)

packVehicleData = []
packTLSData = []
packBigData = []

while traci.simulation.getMinExpectedNumber() > 0:
       
        traci.simulationStep();
        timestep = traci.simulation.getTime()

        vehicles=traci.vehicle.getIDList();
        trafficlights=traci.trafficlight.getIDList();

        for i in range(0,len(vehicles)):

                vehid = vehicles[i]
                x, y = traci.vehicle.getPosition(vehicles[i])
                coord = [x, y]
                lon, lat = traci.simulation.convertGeo(x, y)
                gpscoord = [lon, lat]
                spd = round(traci.vehicle.getSpeed(vehicles[i])*3.6,2)


                #Packing of all the data for export to CSV/XLSX
                vehList = [getdatetime(), vehid, coord, gpscoord, spd]
                
                
                print("Vehicle: ", vehicles[i], " at datetime: ", getdatetime())
                print(vehicles[i], " >>> Position: ", coord, " | GPS Position: ", gpscoord, " |", \
                                       " Speed: ", round(traci.vehicle.getSpeed(vehicles[i])*3.6,2), "km/h |", \

                       )


                #Pack Simulated Data
                packBigDataLine = flatten_list([vehList, tlsList])
                packBigData.append(packBigDataLine)


                ##----- CONTROL Vehicles ----##

                #***SET FUNCTION FOR VEHICLES***
                #REF: https://sumo.dlr.de/docs/TraCI/Change_Vehicle_State.html
                NEWSPEED = 15 # value in m/s (15 m/s = 54 km/hr)
                if vehicles[i]=='veh2':

                        traci.vehicle.setSpeedMode('veh2',0)
                        traci.vehicle.setSpeed('veh2',NEWSPEED)
                                                                    

traci.close()

#Generate Excel file
columnnames = ['dateandtime', 'vehid', 'coord', 'gpscoord', 'spd'] 
dataset = pd.DataFrame(packBigData, index=None, columns=columnnames)
dataset.to_excel("output.xlsx", index=False)
time.sleep(5)

如果您能建议我使用静脉在 OMNEST 上执行我的 Sumo traci 脚本的程序或教程,那将会非常有帮助。

I am working on SUMO VEINS and OMNEST.
To run sumo files on OMNEST, the input of sumo files (.xml) are inputted in veins_launchd, which in turn finds an unused port, starts sumo and bridges the connection between sumo and OMNEST.

I want to control the vehicle's behavior (Speed change) on real time (during the simulation).
For this purpose, I have written a Traci script in python language, which calls sumo config file and controls vehicle speed on real time

My issue is,
I do not know how to make this Traci script (python) to run on OMNEST via veins.
Where should I give this python file as input so that I can visualize the output in OMNEST.
My working environment is Linux

Based on some research, I figured out 2 methods.

1. TraCIScenarioManager module

2. Veins_Python

Method1: I understood by using TraCIScenarioManager module, OMNEST can directly connect to the running sumo.
But I don't know where should I make the necessary changes inside veins module to use TraCIScenarioManager instead TraCIScenarioManagerLaunchd

Method2: Regarding veins_python, I downloaded the source file from github and did the configuration steps as mentioned.
I used windows10 and
Versions: Veins5.0, OMNeT++ 5.5.1 and Python3.6
But I got the below error while configuring Veins_Python.

enter image description here

I also tried with the recent versions of software on windows 10
Versions: Veins5.2, OMNEST-5.6.2 and Python3.10
Still I get the same error.

My Sumo Traci script is

import traci
import time
import traci.constants as tc
import pytz
import datetime
from random import randrange
import pandas as pd


def getdatetime():
        utc_now = pytz.utc.localize(datetime.datetime.utcnow())
        currentDT = utc_now.astimezone(pytz.timezone("Asia/Tokyo"))
        DATIME = currentDT.strftime("%Y-%m-%d %H:%M:%S")
        return DATIME

def flatten_list(_2d_list):
    flat_list = []
    for element in _2d_list:
        if type(element) is list:
            for item in element:
                flat_list.append(item)
        else:
            flat_list.append(element)
    return flat_list


sumoCmd = ["sumo-gui", "-c", "osm.sumocfg"]
traci.start(sumoCmd)

packVehicleData = []
packTLSData = []
packBigData = []

while traci.simulation.getMinExpectedNumber() > 0:
       
        traci.simulationStep();
        timestep = traci.simulation.getTime()

        vehicles=traci.vehicle.getIDList();
        trafficlights=traci.trafficlight.getIDList();

        for i in range(0,len(vehicles)):

                vehid = vehicles[i]
                x, y = traci.vehicle.getPosition(vehicles[i])
                coord = [x, y]
                lon, lat = traci.simulation.convertGeo(x, y)
                gpscoord = [lon, lat]
                spd = round(traci.vehicle.getSpeed(vehicles[i])*3.6,2)


                #Packing of all the data for export to CSV/XLSX
                vehList = [getdatetime(), vehid, coord, gpscoord, spd]
                
                
                print("Vehicle: ", vehicles[i], " at datetime: ", getdatetime())
                print(vehicles[i], " >>> Position: ", coord, " | GPS Position: ", gpscoord, " |", \
                                       " Speed: ", round(traci.vehicle.getSpeed(vehicles[i])*3.6,2), "km/h |", \

                       )


                #Pack Simulated Data
                packBigDataLine = flatten_list([vehList, tlsList])
                packBigData.append(packBigDataLine)


                ##----- CONTROL Vehicles ----##

                #***SET FUNCTION FOR VEHICLES***
                #REF: https://sumo.dlr.de/docs/TraCI/Change_Vehicle_State.html
                NEWSPEED = 15 # value in m/s (15 m/s = 54 km/hr)
                if vehicles[i]=='veh2':

                        traci.vehicle.setSpeedMode('veh2',0)
                        traci.vehicle.setSpeed('veh2',NEWSPEED)
                                                                    

traci.close()

#Generate Excel file
columnnames = ['dateandtime', 'vehid', 'coord', 'gpscoord', 'spd'] 
dataset = pd.DataFrame(packBigData, index=None, columns=columnnames)
dataset.to_excel("output.xlsx", index=False)
time.sleep(5)

It would be really helpful if you could suggest me the procedure or tutorial for executing my Sumo's traci script on OMNEST using veins.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2025-01-21 12:43:28

我认为你想要的是不可能的,因为 traci 仅支持单个客户端(即你设置中的静脉),或者如果你想要多个客户端,则需要更改静脉。您可以尝试在静脉内发送消息,请参阅如何从 Veins Car2X 模拟器中的 TraCIDemoRSU11p 访问 TraCI 命令界面?

I think what you want is not possible since traci supports only a single client (which is veins in your setup) or if you want multiple clients veins needs to be changed. You might try to send the messages inside veins though, see How to access TraCI command interface from TraCIDemoRSU11p in Veins Car2X simulator?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文