如何在我的障碍物或我的机器人周围添加 safe_zone?

How could I add a safe_zone around my obstacles or around my robot?

我正在Python研究这个A_star算法,当算法避开障碍物时,我需要在障碍物或机器人本身周围创建一个safe_zone,直到当机器人经过障碍物附近时,将没有机会撞到障碍物。那么,我该如何添加这个 safe_zone?有什么帮助吗?

我的代码如下所示:

from __future__ import print_function
import random

grid = [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 1, 0]]


init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x] 
cost = 1


#the cost map which pushes the path closer to the goal
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])):            
        heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])




#the actions we can take
delta = [[-1, 0 ], # go up
         [ 0, -1], # go left
         [ 1, 0 ], # go down
         [ 0, 1 ]] # go right


#function to search the path
def search(grid,init,goal,cost,heuristic):

    closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
    closed[init[0]][init[1]] = 1
    action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid

    x = init[0]
    y = init[1]
    g = 0

    f = g + heuristic[init[0]][init[0]]
    cell = [[f, g, x, y]]

    found = False  # flag that is set when search is complete
    resign = False # flag set if we can't find expand

    while not found and not resign:
        if len(cell) == 0:
            resign = True
            return "FAIL"
        else:
            cell.sort()#to choose the least costliest action so as to move closer to the goal
            cell.reverse()
            next = cell.pop()
            x = next[2]
            y = next[3]
            g = next[1]
            f = next[0]


            if x == goal[0] and y == goal[1]:
                found = True
            else:
                for i in range(len(delta)):#to try out different valid actions
                    x2 = x + delta[i][0]
                    y2 = y + delta[i][1]
                    if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
                        if closed[x2][y2] == 0 and grid[x2][y2] == 0:
                            g2 = g + cost
                            f2 = g2 + heuristic[x2][y2]
                            cell.append([f2, g2, x2, y2])
                            closed[x2][y2] = 1
                            action[x2][y2] = i
    invpath = []
    x = goal[0]
    y = goal[1]
    invpath.append([x, y])#we get the reverse path from here
    while x != init[0] or y != init[1]:
        x2 = x - delta[action[x][y]][0]
        y2 = y - delta[action[x][y]][1]
        x = x2
        y = y2
        invpath.append([x, y])

    path = []
    for i in range(len(invpath)):
        path.append(invpath[len(invpath) - 1 - i])
    print("ACTION MAP")
    for i in range(len(action)):
        print(action[i])

    return path

a = search(grid,init,goal,cost,heuristic)
for i in range(len(a)):
    print(a[i])

实现此目的的典型方法是在进行搜索之前给障碍物充气。

假设您的机器人是一个半径为 25 厘米的圆形。如果机器人中心距离障碍物小于25厘米,机器人的边缘就会撞到障碍物,对吧?因此,你将障碍物放大(充气)25 厘米(意味着距离原始障碍物小于 25 厘米的任何点本身都会成为障碍物),以便你可以规划机器人中心的运动。

如果您想要额外的安全余量,例如 10 厘米(即机器人的边缘距离障碍物超过 10 厘米),您可以将障碍物充气 35 厘米,而不是 25 厘米。

对于非圆形机器人,障碍物应至少充气最长轴的一半,以确保不会与障碍物发生碰撞。例如Robot shape为50x80,障碍物要充气80/2 = 40以保证轨迹安全。

另外,请注意障碍 inflation 方法最适合 circular/square 形状的机器人。对于具有一个较长轴的矩形 Robots/Robots,在机器人周围的网格地图具有狭窄通道的情况下可能会出现问题,即使机器人可以实际通过它,但障碍物 inflation 可以使其通过不可行。

这个障碍 inflation 可以在被视为图像的地图上使用形态学操作以编程方式完成。例如参见 [​​=10=] 中的膨胀。