大唐盛世·横扫欧亚·君临天下 2023-08-29 07:56 采纳率: 50%
浏览 176

SUMO仿真如何进行路径规划并循迹行驶

对单个车辆,输入起点和终点坐标,随后将生成一条路径(这里假设走的就是最简单的直线),我按如下方式写了代码,跑的时候sumo甚至没换道?我的需求是,小车能按照绝对坐标在gui上寻迹行驶

这是我的.py代码

def calculate_next_position(current_position, end_position, step_size):
    x, y = current_position
    x_end, y_end = end_position
    dx = (x_end - x) * step_size
    dy = (y_end - y) * step_size
    next_position = (x + dx, y + dy)
    return next_position
# 获取起点和终点坐标
start_position = (0,0)
end_position = (100, -10)
step_size = 0.1  # 步长
# 计算起点和终点之间的路径
path = [start_position]
current_position = start_position
while abs(current_position[0] - end_position[0])>0.1:
    next_position = calculate_next_position(current_position, end_position, step_size)
    path.append(next_position)
    current_position = next_position
# 初始化SUMO并开始仿真
sumo_binary = "sumo-gui"  # SUMO可执行文件路径
sumo_cmd = [sumo_binary, "-c", "C:\\sumo-1.18.0\\File\\Bezier\\network.sumo.cfg"]
os.system(" ".join(sumo_cmd))  # 启动SUMO GUI
traci.start(["sumo-gui"])  # 启动traci连接
# 在SUMO中设置车辆并按照计算得到的路径进行移动
vehicle_id = "vehicle_1"
traci.vehicle.add(vehicle_id, "route_1", departLane="random")
print("Vehicle added:", vehicle_id)
for position in path:
    traci.simulationStep()
    traci.vehicle.moveToXY(vehicle_id, "E0", 0, x=position[0], y=position[1])
# 停止仿真并关闭连接
traci.close()

这是我的rou文件

<routes>
    <vType id="car" accel="2.6" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="13.89"/>
    <route id="route0" edges="E0"/>
    <vehicle id="vehicle_1" type="car" route="route0" depart="0" departLane="random"/>
</routes>

这是我的net文件(就是两个端点一条直线的三车道直道)

<net version="1.16" junctionCornerDetail="5" limitTurnSpeed="5.50" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/net_file.xsd">
    <location netOffset="0.00,0.00" convBoundary="0.00,0.00,100.00,0.00" origBoundary="10000000000.00,10000000000.00,-10000000000.00,-10000000000.00" projParameter="!"/>
    <edge id="E0" from="J0" to="J1" priority="-1">
        <lane id="E0_0" index="0" speed="13.89" length="100.00" shape="0.00,-8.00 100.00,-8.00"/>
        <lane id="E0_1" index="1" speed="13.89" length="100.00" shape="0.00,-4.80 100.00,-4.80"/>
        <lane id="E0_2" index="2" speed="13.89" length="100.00" shape="0.00,-1.60 100.00,-1.60"/>
    </edge>
    <junction id="J0" type="dead_end" x="0.00" y="0.00" incLanes="" intLanes="" shape="0.00,0.00 0.00,-9.60"/>
    <junction id="J1" type="dead_end" x="100.00" y="0.00" incLanes="E0_0 E0_1 E0_2" intLanes="" shape="100.00,-9.60 100.00,0.00"/>
</net>

求帮助~

展开全部

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-08-29 09:30
    关注

    引用 皆我百晓生 小程序回复内容作答:
    你的代码中路径规划的部分看起来是正确的,但是要实现小车按照绝对坐标在GUI上行驶的话,需要使用SUMO提供的函数来控制车辆运行。

    在你的代码中,你使用了traci.simulationStep()来进行仿真步骤的前进,但是缺少了控制车辆运行的部分。你需要使用traci.vehicle.moveToXY()函数来控制车辆移动到指定的坐标。

    以下是修改后的代码示例:

    import os
    import traci
    
    def calculate_next_position(current_position, end_position, step_size):
        x, y = current_position
        x_end, y_end = end_position
        dx = (x_end - x) * step_size
        dy = (y_end - y) * step_size
        next_position = (x + dx, y + dy)
        return next_position
    
    # 获取起点和终点坐标
    start_position = (0, 0)
    end_position = (100, -10)
    step_size = 0.1  # 步长
    
    # 计算起点和终点之间的路径
    path = [start_position]
    current_position = start_position
    while abs(current_position[0] - end_position[0]) > 0.1:
        next_position = calculate_next_position(current_position, end_position, step_size)
        path.append(next_position)
        current_position = next_position
    
    # 初始化SUMO并开始仿真
    sumo_binary = "sumo-gui"  # SUMO可执行文件路径
    sumo_cmd = [sumo_binary, "-c", "C:\\sumo-1.18.0\\File\\Bezier\\network.sumo.cfg"]
    os.system(" ".join(sumo_cmd))  # 启动SUMO GUI
    
    # 连接到SUMO
    traci.start(["sumo-gui"])  # 启动traci连接
    
    # 在SUMO中设置车辆并按照计算得到的路径进行移动
    vehicle_id = "vehicle_1"
    traci.vehicle.add(vehicle_id, "route0", departLane="random")
    print("Vehicle added:", vehicle_id)
    
    for position in path:
        traci.simulationStep()
        traci.vehicle.moveToXY(vehicle_id, "E0", 0, x=position[0], y=position[1])
    
    # 停止仿真并关闭连接
    traci.close()
    

    这段代码通过使用traci.vehicle.moveToXY()函数来控制车辆按照绝对坐标在GUI上行驶。在每次仿真步骤中,将调用traci.simulationStep()以便小车能够在GUI上正确行驶。

    请确保你已经正确安装了SUMO,并修改了相应的路径。如果问题仍然存在,请提供更多的信息,以便我们更好地帮助你解决问题。

    展开全部

    评论

    兄弟,你这个代码和我那个一模一样啊

    回复
  • CSDN-Ada助手 CSDN-AI 官方账号 2023-08-29 13:11
    关注

    【以下回答由 GPT 生成】

    我很高兴能够帮助您解决IT方面的问题,但是您没有提供具体的问题。如果您能够告诉我您遇到的问题,我将尽力为您提供解决方案。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部