|
| 1 | +""" |
| 2 | +
|
| 3 | +Probablistic Road Map (PRM) Planner |
| 4 | +
|
| 5 | +author: Atsushi Sakai (@Atsushi_twi) |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import random |
| 10 | +import math |
| 11 | +import numpy as np |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +from matplotrecorder import matplotrecorder |
| 14 | +from pyfastnns import pyfastnns |
| 15 | +matplotrecorder.donothing = True |
| 16 | + |
| 17 | +# parameter |
| 18 | +N_SAMPLE = 500 |
| 19 | +N_KNN = 10 |
| 20 | +MAX_EDGE_LEN = 30.0 # [m] Maximum edge length |
| 21 | + |
| 22 | + |
| 23 | +class Node: |
| 24 | + |
| 25 | + def __init__(self, x, y, cost, pind): |
| 26 | + self.x = x |
| 27 | + self.y = y |
| 28 | + self.cost = cost |
| 29 | + self.pind = pind |
| 30 | + |
| 31 | + def __str__(self): |
| 32 | + return str(self.x) + "," + str(self.y) + "," + str(self.cost) + "," + str(self.pind) |
| 33 | + |
| 34 | + |
| 35 | +def PRM_planning(sx, sy, gx, gy, ox, oy, rr): |
| 36 | + |
| 37 | + sample_x, sample_y = sample_points(sx, sy, gx, gy, rr, ox, oy) |
| 38 | + plt.plot(sample_x, sample_y, ".r") |
| 39 | + |
| 40 | + road_map = generate_roadmap(sample_x, sample_y, rr) |
| 41 | + |
| 42 | + rx, ry = dijkstra_planning( |
| 43 | + sx, sy, gx, gy, ox, oy, rr, road_map, sample_x, sample_y) |
| 44 | + |
| 45 | + return rx, ry |
| 46 | + |
| 47 | + |
| 48 | +def generate_roadmap(sample_x, sample_y, rr): |
| 49 | + |
| 50 | + road_map = [] |
| 51 | + nsample = len(sample_x) |
| 52 | + skdtree = pyfastnns.NNS(np.vstack((sample_x, sample_y)).T) |
| 53 | + |
| 54 | + for (i, ix, iy) in zip(range(nsample), sample_x, sample_y): |
| 55 | + |
| 56 | + index = skdtree.search( |
| 57 | + np.matrix([ix, iy]).T, k=nsample) |
| 58 | + edge_id = [] |
| 59 | + |
| 60 | + for ii in range(1, len(index[0][0][0])): |
| 61 | + # nx = sample_x[index[i]] |
| 62 | + # ny = sample_y[index[i]] |
| 63 | + |
| 64 | + # if !is_collision(ix, iy, nx, ny, rr, okdtree) |
| 65 | + edge_id.append(index[0][0][0][ii]) |
| 66 | + if len(edge_id) >= N_KNN: |
| 67 | + break |
| 68 | + |
| 69 | + road_map.append(edge_id) |
| 70 | + |
| 71 | + # plot_road_map(road_map, sample_x, sample_y) |
| 72 | + |
| 73 | + return road_map |
| 74 | + |
| 75 | + |
| 76 | +def dijkstra_planning(sx, sy, gx, gy, ox, oy, rr, road_map, sample_x, sample_y): |
| 77 | + """ |
| 78 | + gx: goal x position [m] |
| 79 | + gx: goal x position [m] |
| 80 | + ox: x position list of Obstacles [m] |
| 81 | + oy: y position list of Obstacles [m] |
| 82 | + reso: grid resolution [m] |
| 83 | + rr: robot radius[m] |
| 84 | + """ |
| 85 | + |
| 86 | + nstart = Node(sx, sy, 0.0, -1) |
| 87 | + ngoal = Node(gx, gy, 0.0, -1) |
| 88 | + |
| 89 | + openset, closedset = dict(), dict() |
| 90 | + openset[len(road_map) - 2] = nstart |
| 91 | + |
| 92 | + while True: |
| 93 | + if len(openset) == 0: |
| 94 | + print("Cannot find path") |
| 95 | + break |
| 96 | + |
| 97 | + print(len(openset), len(closedset)) |
| 98 | + |
| 99 | + c_id = min(openset, key=lambda o: openset[o].cost) |
| 100 | + current = openset[c_id] |
| 101 | + print("current", current, c_id) |
| 102 | + # input() |
| 103 | + |
| 104 | + # show graph |
| 105 | + plt.plot(current.x, current.y, "xc") |
| 106 | + if len(closedset.keys()) % 10 == 0: |
| 107 | + plt.pause(0.001) |
| 108 | + matplotrecorder.save_frame() |
| 109 | + |
| 110 | + if c_id == (len(road_map) - 1): |
| 111 | + print("Find goal") |
| 112 | + ngoal.pind = current.pind |
| 113 | + ngoal.cost = current.cost |
| 114 | + break |
| 115 | + |
| 116 | + # Remove the item from the open set |
| 117 | + del openset[c_id] |
| 118 | + # Add it to the closed set |
| 119 | + closedset[c_id] = current |
| 120 | + |
| 121 | + # expand search grid based on motion model |
| 122 | + for i in range(len(road_map[c_id])): |
| 123 | + n_id = road_map[c_id][i] |
| 124 | + print(i, n_id) |
| 125 | + dx = sample_x[n_id] - current.x |
| 126 | + dy = sample_y[n_id] - current.y |
| 127 | + d = math.sqrt(dx**2 + dy**2) |
| 128 | + node = Node(sample_x[n_id], sample_y[n_id], |
| 129 | + current.cost + d, c_id) |
| 130 | + |
| 131 | + # if not verify_node(node, obmap, minx, miny, maxx, maxy): |
| 132 | + # continue |
| 133 | + |
| 134 | + if n_id in closedset: |
| 135 | + continue |
| 136 | + # Otherwise if it is already in the open set |
| 137 | + if n_id in openset: |
| 138 | + if openset[n_id].cost > node.cost: |
| 139 | + openset[n_id].cost = node.cost |
| 140 | + openset[n_id].pind = c_id |
| 141 | + else: |
| 142 | + openset[n_id] = node |
| 143 | + |
| 144 | + # generate final course |
| 145 | + rx, ry = [ngoal.x], [ngoal.y] |
| 146 | + pind = ngoal.pind |
| 147 | + while pind != -1: |
| 148 | + n = closedset[pind] |
| 149 | + rx.append(n.x) |
| 150 | + ry.append(n.y) |
| 151 | + pind = n.pind |
| 152 | + |
| 153 | + return rx, ry |
| 154 | + |
| 155 | + |
| 156 | +def plot_road_map(road_map, sample_x, sample_y): |
| 157 | + |
| 158 | + for i in range(len(road_map)): |
| 159 | + for ii in range(len(road_map[i])): |
| 160 | + ind = road_map[i][ii] |
| 161 | + |
| 162 | + plt.plot([sample_x[i], sample_x[ind]], |
| 163 | + [sample_y[i], sample_y[ind]], "-k") |
| 164 | + |
| 165 | + |
| 166 | +def sample_points(sx, sy, gx, gy, rr, ox, oy): |
| 167 | + maxx = max(ox) |
| 168 | + maxy = max(oy) |
| 169 | + minx = min(ox) |
| 170 | + miny = min(oy) |
| 171 | + |
| 172 | + sample_x, sample_y = [], [] |
| 173 | + |
| 174 | + nns = pyfastnns.NNS(np.vstack((ox, oy)).T) |
| 175 | + |
| 176 | + while len(sample_x) <= N_SAMPLE: |
| 177 | + tx = (random.random() - minx) * (maxx - minx) |
| 178 | + ty = (random.random() - miny) * (maxy - miny) |
| 179 | + |
| 180 | + index, dist = nns.search(np.matrix([tx, ty]).T) |
| 181 | + |
| 182 | + if dist[0] >= rr: |
| 183 | + sample_x.append(tx) |
| 184 | + sample_y.append(ty) |
| 185 | + |
| 186 | + sample_x.append(sx) |
| 187 | + sample_y.append(sy) |
| 188 | + sample_x.append(gx) |
| 189 | + sample_y.append(gy) |
| 190 | + |
| 191 | + return sample_x, sample_y |
| 192 | + |
| 193 | + |
| 194 | +def main(): |
| 195 | + print(__file__ + " start!!") |
| 196 | + |
| 197 | + # start and goal position |
| 198 | + sx = 10.0 # [m] |
| 199 | + sy = 10.0 # [m] |
| 200 | + gx = 50.0 # [m] |
| 201 | + gy = 50.0 # [m] |
| 202 | + robot_size = 5.0 # [m] |
| 203 | + |
| 204 | + ox = [] |
| 205 | + oy = [] |
| 206 | + |
| 207 | + for i in range(60): |
| 208 | + ox.append(i) |
| 209 | + oy.append(0.0) |
| 210 | + for i in range(60): |
| 211 | + ox.append(60.0) |
| 212 | + oy.append(i) |
| 213 | + for i in range(61): |
| 214 | + ox.append(i) |
| 215 | + oy.append(60.0) |
| 216 | + for i in range(61): |
| 217 | + ox.append(0.0) |
| 218 | + oy.append(i) |
| 219 | + for i in range(40): |
| 220 | + ox.append(20.0) |
| 221 | + oy.append(i) |
| 222 | + for i in range(40): |
| 223 | + ox.append(40.0) |
| 224 | + oy.append(60.0 - i) |
| 225 | + |
| 226 | + plt.plot(ox, oy, ".k") |
| 227 | + plt.plot(sx, sy, "xr") |
| 228 | + plt.plot(gx, gy, "xb") |
| 229 | + plt.grid(True) |
| 230 | + plt.axis("equal") |
| 231 | + |
| 232 | + rx, ry = PRM_planning(sx, sy, gx, gy, ox, oy, robot_size) |
| 233 | + |
| 234 | + plt.plot(rx, ry, "-r") |
| 235 | + |
| 236 | + for i in range(20): |
| 237 | + matplotrecorder.save_frame() |
| 238 | + plt.show() |
| 239 | + |
| 240 | + matplotrecorder.save_movie("animation.gif", 0.1) |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == '__main__': |
| 244 | + main() |
0 commit comments