|
| 1 | +""" |
| 2 | +
|
| 3 | +Visibility Graph Planner |
| 4 | +
|
| 5 | +author: Atsushi Sakai (@Atsushi_twi) |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | +show_animation = True |
| 12 | + |
| 13 | +class VisibilityGraphPlanner: |
| 14 | + |
| 15 | + def __init__(self, robot_radius): |
| 16 | + self.robot_radius = robot_radius |
| 17 | + |
| 18 | + def planning(self, start_x, start_y, goal_x, goal_y, obstacles): |
| 19 | + nodes = self.exstract_graph_node(start_x, start_y, goal_x, goal_y, |
| 20 | + obstacles) |
| 21 | + |
| 22 | + graph = self.generate_graph(nodes, obstacles) |
| 23 | + |
| 24 | + rx, ry = dijkstra_search(graph) |
| 25 | + |
| 26 | + return rx, ry |
| 27 | + |
| 28 | + def exstract_graph_node(self, start_x, start_y, goal_x, goal_x, obstacles): |
| 29 | + nodes = [] |
| 30 | + |
| 31 | + return nodes |
| 32 | + |
| 33 | + def generate_graph(self, nodes, obstacles): |
| 34 | + |
| 35 | + graph = [] |
| 36 | + |
| 37 | + return graph |
| 38 | + |
| 39 | + |
| 40 | +class ObstaclePolygon: |
| 41 | + |
| 42 | + def __init__(self, x_list, y_list): |
| 43 | + self.x_list = x_list |
| 44 | + self.y_list = y_list |
| 45 | + |
| 46 | + self.close_polygon() |
| 47 | + |
| 48 | + def close_polygon(self): |
| 49 | + is_x_same = self.x_list[0] == self.x_list[-1] |
| 50 | + is_y_same = self.y_list[0] == self.y_list[-1] |
| 51 | + if is_x_same and is_y_same: |
| 52 | + return # no need to close |
| 53 | + |
| 54 | + self.x_list.append(self.x_list[0]) |
| 55 | + self.y_list.append(self.y_list[0]) |
| 56 | + |
| 57 | + def plot(self): |
| 58 | + plt.plot(self.x_list, self.y_list, "-b") |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + print(__file__ + " start!!") |
| 63 | + |
| 64 | + # start and goal position |
| 65 | + sx, sy = 10.0, 10.0 # [m] |
| 66 | + gx, gy = 50.0, 50.0 # [m] |
| 67 | + robot_radius = 5.0 # [m] |
| 68 | + |
| 69 | + obstacles = [ObstaclePolygon( |
| 70 | + [20.0, 30.0, 15.0], |
| 71 | + [20.0, 20.0, 30.0], |
| 72 | + ), ObstaclePolygon( |
| 73 | + [30.0, 45.0, 50.0, 40.0], |
| 74 | + [50.0, 40.0, 20.0, 40.0], |
| 75 | + )] |
| 76 | + |
| 77 | + rx, ry = VisibilityGraphPlanner(robot_radius).planning(sx, sy, gx, gy, |
| 78 | + obstacles) |
| 79 | + assert rx, 'Cannot found path' |
| 80 | + if show_animation: # pragma: no cover |
| 81 | + plt.plot(sx, sy, "or") |
| 82 | + plt.plot(gx, gy, "ob") |
| 83 | + [ob.plot() for ob in obstacles] |
| 84 | + plt.plot(rx, ry, "-r") |
| 85 | + plt.axis("equal") |
| 86 | + plt.show() |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments