Open lsm2842035890 opened 3 days ago
在bridge/bridge.py里面 async for message in websocket: 这个里面是从Apollo那边拿的信息
我看了您的代码,大致意思是因为apollo通过websocket传输数据,所以您实现异步函数来实时捕获。我把自己的代码改成异步函数了,但是为什么捕获不到呢
async def testApollo(tag):
env = Env()
SIMULATOR_HOST = env.str("LGSVL__SIMULATOR_HOST", "127.0.0.1")
SIMULATOR_PORT = env.int("LGSVL__SIMULATOR_PORT", 8181)
BRIDGE_HOST = env.str("LGSVL__AUTOPILOT_0_HOST", "127.0.0.1")
BRIDGE_PORT = env.int("LGSVL__AUTOPILOT_0_PORT", 9090)
LGSVL__AUTOPILOT_HD_MAP = env.str("LGSVL__AUTOPILOT_HD_MAP", "san_francisco")
LGSVL__AUTOPILOT_0_VEHICLE_CONFIG = env.str("LGSVL__AUTOPILOT_0_VEHICLE_CONFIG", 'Lincoln2017MKZ_LGSVL')
LGSVL__SIMULATION_DURATION_SECS = 1500
vehicle_conf = env.str("LGSVL__VEHICLE_0", lgsvl.wise.DefaultAssets.ego_lincoln2017mkz_apollo6_modular)
scene_name = env.str("LGSVL__MAP", "12da60a7-2fc9-474d-a62a-5cc08cb97fe8")
sim = lgsvl.Simulator(SIMULATOR_HOST, SIMULATOR_PORT)
if sim.current_scene == scene_name:
sim.reset()
else:
sim.load(scene_name)
sim.set_date_time(datetime(2022, 6, 22, 11, 0, 0, 0), True)
spawns = sim.get_spawn()
state2 = lgsvl.AgentState()
# state2.transform = spawns[0]
state2.transform = sim.map_point_on_lane(lgsvl.Vector(-297.000030517578, 10, 4.99996948242188))
print(state2)
print("Loading vehicle {}...".format(vehicle_conf))
ego = sim.add_agent(vehicle_conf, lgsvl.AgentType.EGO, state2)
print("Connecting to bridge...")
ego.connect_bridge(BRIDGE_HOST, BRIDGE_PORT)
dv = lgsvl.dreamview.Connection(sim, ego, BRIDGE_HOST)
dv.set_hd_map(LGSVL__AUTOPILOT_HD_MAP)
dv.set_vehicle(LGSVL__AUTOPILOT_0_VEHICLE_CONFIG)
default_modules = [
'Localization',
'Transform',
'Routing',
'Prediction',
'Planning',
'Control',
'Recorder',
'Perception'
]
dv.disable_apollo()
time.sleep(5)
dv.setup_apollo(592598.94, 4134701.40, default_modules)
dv.set_destination(592598.94,4134701.40,coord_type=CoordType.Northing)
sim.run(LGSVL__SIMULATION_DURATION_SECS)
async def monitor_ego_and_npcs_data():
uri = "ws://localhost:8888" # 替换为实际的 WebSocket 地址
try:
print("[WebSocket] Attempting to connect...")
async with websockets.connect(uri) as websocket:
init_msg = json.dumps({ 'type': 'HMIAction', 'action': "START_MODULE", 'value': "Planning" })
print(init_msg)
await websocket.send(init_msg)
print("[WebSocket] Connected to Apollo WebSocket!")
while True:
message = await websocket.recv()
print(f"[WebSocket] Received: {message}")
except Exception as e:
print(f"[WebSocket] Error: {e}")
# 主函数
async def main():
simulation_task = asyncio.create_task(
testApollo(1)
)
websocket_task = asyncio.create_task(monitor_ego_and_npcs_data())
print("Waiting for tasks to complete...")
# 等待所有任务完成
await asyncio.gather(simulation_task, websocket_task)
if __name__ == "__main__":
asyncio.run(main())
打印如下: Waiting for tasks to complete... Loading vehicle 2e9095fa-c9b9-4f3f-8d7d-65fa2bb03921... Connecting to bridge...
你需要先确认apollo正常在运行。然后确认他的数据端口是哪个。似乎你的数据端口有误。如果你的simulator+apollo运行正常,你可以查看simulator端的代码,其中包含了如何接收/处理Apollo发送的信息。
上面显示是http://localhost:8888 代码没有执行到try: print("[WebSocket] Attempting to connect...") async with websockets.connect(uri) as websocket:也就是没有进入到这个函数async def monitor_ego_and_npcs_data()
你需要先确认apollo正常在运行。然后确认他的数据端口是哪个。似乎你的数据端口有误。如果你的simulator+apollo运行正常,你可以查看simulator端的代码,其中包含了如何接收/处理Apollo发送的信息。
好的我再去看看~
上面显示是http://localhost:8888 代码没有执行到try: print("[WebSocket] Attempting to connect...") async with websockets.connect(uri) as websocket:也就是没有进入到这个函数async def monitor_ego_and_npcs_data()
如果是这种情况,你可以先别尝试异步运行(似乎异步代码有bug,你的线程monitor_ego_and_npcs_data没进入)。可以先启动apollo+simulator,确保其在运行。再用另一个脚本单独开一个线程,只接受信息,试试能不能收到。
您好,非常感谢您的提示!!
import math
import time
import random
from lgsvl import Transform, Vector
from lgsvl.dreamview import CoordType
from datetime import datetime
from environs import Env
import lgsvl
import asyncio
import websockets
import json
import requests
import threading
def testApollo(tag):
env = Env()
SIMULATOR_HOST = env.str("LGSVL__SIMULATOR_HOST", "[127.0.0.1](http://127.0.0.1/)")
SIMULATOR_PORT = [env.int](http://env.int/)("LGSVL__SIMULATOR_PORT", 8181)
BRIDGE_HOST = env.str("LGSVL__AUTOPILOT_0_HOST", "[127.0.0.1](http://127.0.0.1/)")
BRIDGE_PORT = [env.int](http://env.int/)("LGSVL__AUTOPILOT_0_PORT", 9090)
LGSVL__AUTOPILOT_HD_MAP = env.str("LGSVL__AUTOPILOT_HD_MAP", "san_francisco")
LGSVL__AUTOPILOT_0_VEHICLE_CONFIG = env.str("LGSVL__AUTOPILOT_0_VEHICLE_CONFIG", 'Lincoln2017MKZ_LGSVL')
LGSVL__SIMULATION_DURATION_SECS = 1500
vehicle_conf = env.str("LGSVL__VEHICLE_0", lgsvl.wise.DefaultAssets.ego_lincoln2017mkz_apollo6_modular)
scene_name = env.str("LGSVL__MAP", "12da60a7-2fc9-474d-a62a-5cc08cb97fe8")
sim = lgsvl.Simulator(SIMULATOR_HOST, SIMULATOR_PORT)
if sim.current_scene == scene_name:
sim.reset()
else:
sim.load(scene_name)
sim.set_date_time(datetime(2022, 6, 22, 11, 0, 0, 0), True)
spawns = sim.get_spawn()
state2 = lgsvl.AgentState()
# state2.transform = spawns[0]
state2.transform = sim.map_point_on_lane(lgsvl.Vector(-297.000030517578, 10, 4.99996948242188))
print(state2)
print("Loading vehicle {}...".format(vehicle_conf))
ego = sim.add_agent(vehicle_conf, lgsvl.AgentType.EGO, state2)
print("Connecting to bridge...")
ego.connect_bridge(BRIDGE_HOST, BRIDGE_PORT)
dv = lgsvl.dreamview.Connection(sim, ego, BRIDGE_HOST)
dv.set_hd_map(LGSVL__AUTOPILOT_HD_MAP)
dv.set_vehicle(LGSVL__AUTOPILOT_0_VEHICLE_CONFIG)
default_modules = [
'Localization',
'Transform',
'Routing',
'Prediction',
'Planning',
'Control',
'Recorder',
'Perception'
]
dv.disable_apollo()
time.sleep(5)
dv.setup_apollo(592598.94, 4134701.40, default_modules)
dv.set_destination(592598.94,4134701.40,coord_type=CoordType.Northing)
sim.run(LGSVL__SIMULATION_DURATION_SECS)
async def monitor_ego_and_npcs_data():
uri = "ws://localhost:8888/websocket" # 替换为实际的 WebSocket 地址
# uri = "ws://localhost:8888"
try:
print("[WebSocket] Attempting to connect...")
async with websockets.connect(uri) as websocket:
# init_msg = json.dumps({ 'type': 'HMIStatus' })
# init_msg = json.dumps({ 'type': 'EgoStatusRequest' })
init_msg = json.dumps({ 'type': 'HMIAction' })
websocket.send(init_msg)
print("[WebSocket] Connected to Apollo WebSocket!")
print("sdasad")
while True:
message = await websocket.recv()
try:
# 尝试将接收到的消息解析为 JSON 格式
data = json.loads(message)
if data["type"] == "HMIAction":
print("[WebSocket] Parsed JSON:", json.dumps(data, indent=4)) # 美化打印 JSON
except json.JSONDecodeError:
# 如果无法解析为 JSON 格式,打印原始字节串
print("[WebSocket] Error decoding JSON:", message)
# message = websocket.recv()
# print(message["DATA"])
except Exception as e:
print(f"[WebSocket] Error: {e}")
def start_websocket_thread():
# 使用 asyncio 运行 WebSocket 监听
asyncio.run(monitor_ego_and_npcs_data())
if __name__ == "__main__":
websocket_thread = threading.Thread(target=start_websocket_thread)
websocket_thread.daemon = True # 使线程成为守护线程,主线程退出时也会退出
websocket_thread.start()
# 启动仿真任务
testApollo(1)
# 等待 WebSocket 线程结束(如果需要,也可以设置超时等)
websocket_thread.join()
根据您的提示,我能截获到数据,打印出如下: [WebSocket] Parsed JSON: { "data": { "currentMap": "San Francisco", "currentMode": "Mkz Standard Debug", "currentVehicle": "Lincoln2017MKZ LGSVL", "dockerImage": "apolloauto/apollo:dev-x86_64-18.04-20221124_1708", "maps": [ "Sunnyvale Big Loop", "Sunnyvale With Two Offices", "Shalun", "Borregas Ave", "San Francisco", "Sunnyvale Loop", "Demo", "San Mateo", "Carla Town01" ], "modes": [ "Vehicle Calibration", "Ipc2 Mkz Standard Debug", "Mkz Standard Debug", "Rtk", "Mkz Close Loop", "Ipc1 Mkz Standard Debug", "Ipc2 Mkz Close Loop", "Map Collection", "Mkz Standard Debug Hesai", "Dev Kit Close Loop", "Mkz Standard Debug Smart Recorder", "Camera-Lidar Sensor Calibration", "Ipc1 Mkz Close Loop", "Mkz Lgsvl", "Car Teleop", "Mkz 64beam", "Console Teleop", "Dev Kit Debug", "Lidar-IMU Sensor Calibration", "Navigation" ], "modules": { "AudioDetection": false, "Camera": false, "Canbus": false, "Control": true, "GPS": false, "Guardian": false, "Localization": false, "Microphone": false, "Perception": false, "Planning": true, "Prediction": true, "Radar": false, "Recorder": false, "Routing": true, "Storytelling": false, "Traffic Light": false, "Transform": true, "Velodyne": false }, "monitoredComponents": { "GPS": { "message": "", "status": "FATAL" }, "Lidar 128": { "message": "the message /apollo/sensor/lidar128/compensator/PointCloud2 reseived is empty.", "status": "FATAL" }, "Localization": { "message": "No LocalizationStatus received", "status": "ERROR" }, "Recorder": { "message": "", "status": "FATAL" } }, "otherComponents": {}, "passengerMsg": "", "utmZoneId": 10, "vehicles": [ "Mkz Example", "Lincoln2017MKZ LGSVL" ] }, "type": "HMIStatus" } 我想要得到ego的信息,我猜测是init_msg的请求类型导致它没有返回我想要的,那如果想要获取到ego或者其他npc的属性信息,我应该填的type类型为何值
您好,非常感谢您的提示!!
import math import time import random from lgsvl import Transform, Vector from lgsvl.dreamview import CoordType from datetime import datetime from environs import Env import lgsvl import asyncio import websockets import json import requests import threading def testApollo(tag): env = Env() SIMULATOR_HOST = env.str("LGSVL__SIMULATOR_HOST", "[127.0.0.1](http://127.0.0.1/)") SIMULATOR_PORT = [env.int](http://env.int/)("LGSVL__SIMULATOR_PORT", 8181) BRIDGE_HOST = env.str("LGSVL__AUTOPILOT_0_HOST", "[127.0.0.1](http://127.0.0.1/)") BRIDGE_PORT = [env.int](http://env.int/)("LGSVL__AUTOPILOT_0_PORT", 9090) LGSVL__AUTOPILOT_HD_MAP = env.str("LGSVL__AUTOPILOT_HD_MAP", "san_francisco") LGSVL__AUTOPILOT_0_VEHICLE_CONFIG = env.str("LGSVL__AUTOPILOT_0_VEHICLE_CONFIG", 'Lincoln2017MKZ_LGSVL') LGSVL__SIMULATION_DURATION_SECS = 1500 vehicle_conf = env.str("LGSVL__VEHICLE_0", lgsvl.wise.DefaultAssets.ego_lincoln2017mkz_apollo6_modular) scene_name = env.str("LGSVL__MAP", "12da60a7-2fc9-474d-a62a-5cc08cb97fe8") sim = lgsvl.Simulator(SIMULATOR_HOST, SIMULATOR_PORT) if sim.current_scene == scene_name: sim.reset() else: sim.load(scene_name) sim.set_date_time(datetime(2022, 6, 22, 11, 0, 0, 0), True) spawns = sim.get_spawn() state2 = lgsvl.AgentState() # state2.transform = spawns[0] state2.transform = sim.map_point_on_lane(lgsvl.Vector(-297.000030517578, 10, 4.99996948242188)) print(state2) print("Loading vehicle {}...".format(vehicle_conf)) ego = sim.add_agent(vehicle_conf, lgsvl.AgentType.EGO, state2) print("Connecting to bridge...") ego.connect_bridge(BRIDGE_HOST, BRIDGE_PORT) dv = lgsvl.dreamview.Connection(sim, ego, BRIDGE_HOST) dv.set_hd_map(LGSVL__AUTOPILOT_HD_MAP) dv.set_vehicle(LGSVL__AUTOPILOT_0_VEHICLE_CONFIG) default_modules = [ 'Localization', 'Transform', 'Routing', 'Prediction', 'Planning', 'Control', 'Recorder', 'Perception' ] dv.disable_apollo() time.sleep(5) dv.setup_apollo(592598.94, 4134701.40, default_modules) dv.set_destination(592598.94,4134701.40,coord_type=CoordType.Northing) sim.run(LGSVL__SIMULATION_DURATION_SECS) async def monitor_ego_and_npcs_data(): uri = "ws://localhost:8888/websocket" # 替换为实际的 WebSocket 地址 # uri = "ws://localhost:8888" try: print("[WebSocket] Attempting to connect...") async with websockets.connect(uri) as websocket: # init_msg = json.dumps({ 'type': 'HMIStatus' }) # init_msg = json.dumps({ 'type': 'EgoStatusRequest' }) init_msg = json.dumps({ 'type': 'HMIAction' }) websocket.send(init_msg) print("[WebSocket] Connected to Apollo WebSocket!") print("sdasad") while True: message = await websocket.recv() try: # 尝试将接收到的消息解析为 JSON 格式 data = json.loads(message) if data["type"] == "HMIAction": print("[WebSocket] Parsed JSON:", json.dumps(data, indent=4)) # 美化打印 JSON except json.JSONDecodeError: # 如果无法解析为 JSON 格式,打印原始字节串 print("[WebSocket] Error decoding JSON:", message) # message = websocket.recv() # print(message["DATA"]) except Exception as e: print(f"[WebSocket] Error: {e}") def start_websocket_thread(): # 使用 asyncio 运行 WebSocket 监听 asyncio.run(monitor_ego_and_npcs_data()) if __name__ == "__main__": websocket_thread = threading.Thread(target=start_websocket_thread) websocket_thread.daemon = True # 使线程成为守护线程,主线程退出时也会退出 websocket_thread.start() # 启动仿真任务 testApollo(1) # 等待 WebSocket 线程结束(如果需要,也可以设置超时等) websocket_thread.join()
根据您的提示,我能截获到数据,打印出如下: [WebSocket] Parsed JSON: { "data": { "currentMap": "San Francisco", "currentMode": "Mkz Standard Debug", "currentVehicle": "Lincoln2017MKZ LGSVL", "dockerImage": "apolloauto/apollo:dev-x86_64-18.04-20221124_1708", "maps": [ "Sunnyvale Big Loop", "Sunnyvale With Two Offices", "Shalun", "Borregas Ave", "San Francisco", "Sunnyvale Loop", "Demo", "San Mateo", "Carla Town01" ], "modes": [ "Vehicle Calibration", "Ipc2 Mkz Standard Debug", "Mkz Standard Debug", "Rtk", "Mkz Close Loop", "Ipc1 Mkz Standard Debug", "Ipc2 Mkz Close Loop", "Map Collection", "Mkz Standard Debug Hesai", "Dev Kit Close Loop", "Mkz Standard Debug Smart Recorder", "Camera-Lidar Sensor Calibration", "Ipc1 Mkz Close Loop", "Mkz Lgsvl", "Car Teleop", "Mkz 64beam", "Console Teleop", "Dev Kit Debug", "Lidar-IMU Sensor Calibration", "Navigation" ], "modules": { "AudioDetection": false, "Camera": false, "Canbus": false, "Control": true, "GPS": false, "Guardian": false, "Localization": false, "Microphone": false, "Perception": false, "Planning": true, "Prediction": true, "Radar": false, "Recorder": false, "Routing": true, "Storytelling": false, "Traffic Light": false, "Transform": true, "Velodyne": false }, "monitoredComponents": { "GPS": { "message": "", "status": "FATAL" }, "Lidar 128": { "message": "the message /apollo/sensor/lidar128/compensator/PointCloud2 reseived is empty.", "status": "FATAL" }, "Localization": { "message": "No LocalizationStatus received", "status": "ERROR" }, "Recorder": { "message": "", "status": "FATAL" } }, "otherComponents": {}, "passengerMsg": "", "utmZoneId": 10, "vehicles": [ "Mkz Example", "Lincoln2017MKZ LGSVL" ] }, "type": "HMIStatus" } 我想要得到ego的信息,我猜测是init_msg的请求类型导致它没有返回我想要的,那如果想要获取到ego或者其他npc的属性信息,我应该填的type类型为何值
具体请参考文件 bridge/lgsvl_method.py,里面有详细的实现和通讯协议。简而言之你需要subscribe一些channel,从而持续的收取信息。
在apollo和lgsvl仿真进行的时候怎么得到ego和npc的属性(速度位置等等),具体在哪个python文件里实现~