#!/usr/bin/python# -*- coding: UTF-8 -*- import matplotlib.pyplot as plt
import math
show_animation =TrueclassDijkstra:def__init__(self, ox, oy, resolution, robot_radius):"""
Initialize map for planning
ox: x position list of Obstacles [m]
oy: y position list of Obstacles [m]
resolution: grid resolution [m]
rr: robot radius[m]
"""
self.min_x =None
self.min_y =None
self.max_x =None
self.max_y =None
self.x_width =None
self.y_width =None
self.obstacle_map =None
self.resolution = resolution
self.robot_radius = robot_radius
#构建栅格地图
self.calc_obstacle_map(ox, oy)
self.motion = self.get_motion_model()classNode:def__init__(self, x, y, cost, parent_index):
self.x = x # index of grid
self.y = y # index of grid
self.cost = cost # g(n)
self.parent_index = parent_index
# index of previous Nodedef__str__(self):returnstr(self.x)+","+str(self.y)+","+str(
self.cost)+","+str(self.parent_index)defplanning(self, sx, sy, gx, gy):"""
dijkstra path search
input:
s_x: start x position [m]
s_y: start y position [m]
gx: goal x position [m]
gx: goal x position [m]
output:
rx: x position list of the final path
ry: y position list of the final path
"""#将起点和终点转换为节点形式,即包含下标,代价值和父节点信息
start_node = self.Node(self.calc_xy_index(sx, self.min_x),
self.calc_xy_index(sy, self.min_y),0.0,-1)# round((position - minp) / self.resolution)
goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
self.calc_xy_index(gy, self.min_y),0.0,-1)
open_set, closed_set =dict(),dict()# key - value: hash表#key是表示索引,value 是节点信息
open_set[self.calc_index(start_node)]= start_node
while1:
c_id =