对单个车辆,输入起点和终点坐标,随后将生成一条路径(这里假设走的就是最简单的直线),我按如下方式写了代码,跑的时候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>
求帮助~