|
| 1 | +""" |
| 2 | +
|
| 3 | +2D grid map sample |
| 4 | +
|
| 5 | +author: Atsushi Sakai (@Atsushi_twi) |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import math |
| 10 | +import numpy as np |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from scipy.stats import norm |
| 13 | + |
| 14 | +AREA_WIDTH = 10.0 |
| 15 | + |
| 16 | +STD = 10.0 # standard diviation |
| 17 | + |
| 18 | + |
| 19 | +def generate_gaussian_grid_map(ox, oy, xyreso): |
| 20 | + |
| 21 | + minx, miny, maxx, maxy, xw, yw = calc_grid_map_config(ox, oy, xyreso) |
| 22 | + |
| 23 | + # calc each potential |
| 24 | + pmap = [[0.0 for i in range(yw)] for i in range(xw)] |
| 25 | + |
| 26 | + for ix in range(xw): |
| 27 | + for iy in range(yw): |
| 28 | + |
| 29 | + x = ix * xyreso + minx |
| 30 | + y = iy * xyreso + miny |
| 31 | + |
| 32 | + # Search minimum distance |
| 33 | + mindis = float("inf") |
| 34 | + for (iox, ioy) in zip(ox, oy): |
| 35 | + d = math.sqrt((iox - x)**2 + (ioy - y)**2) |
| 36 | + if mindis >= d: |
| 37 | + mindis = d |
| 38 | + |
| 39 | + pdf = (1.0 - norm.cdf(mindis, 0.0, STD)) |
| 40 | + pmap[ix][iy] = pdf |
| 41 | + |
| 42 | + draw_heatmap(pmap, minx, maxx, miny, maxy, xyreso) |
| 43 | + plt.plot(ox, oy, "xr") |
| 44 | + plt.plot(0.0, 0.0, "ob") |
| 45 | + |
| 46 | + |
| 47 | +def calc_grid_map_config(ox, oy, xyreso): |
| 48 | + minx = round(min(ox) - AREA_WIDTH / 2.0) |
| 49 | + miny = round(min(oy) - AREA_WIDTH / 2.0) |
| 50 | + maxx = round(max(ox) + AREA_WIDTH / 2.0) |
| 51 | + maxy = round(max(oy) + AREA_WIDTH / 2.0) |
| 52 | + xw = int(round((maxx - minx) / xyreso)) |
| 53 | + yw = int(round((maxy - miny) / xyreso)) |
| 54 | + |
| 55 | + return minx, miny, maxx, maxy, xw, yw |
| 56 | + |
| 57 | + |
| 58 | +class precastDB: |
| 59 | + |
| 60 | + def __init__(self): |
| 61 | + self.px = 0.0 |
| 62 | + self.py = 0.0 |
| 63 | + self.d = 0.0 |
| 64 | + self.angle = 0.0 |
| 65 | + self.ix = 0 |
| 66 | + self.iy = 0 |
| 67 | + |
| 68 | + def __str__(self): |
| 69 | + return str(self.px) + "," + str(self.py) + "," + str(self.d) + "," + str(self.angle) |
| 70 | + |
| 71 | + |
| 72 | +def precasting(minx, miny, xw, yw, xyreso, yawreso): |
| 73 | + |
| 74 | + precast = [[] for i in range(round((math.pi * 2.0) / yawreso) + 1)] |
| 75 | + |
| 76 | + for ix in range(xw): |
| 77 | + for iy in range(yw): |
| 78 | + px = ix * xyreso + minx |
| 79 | + py = iy * xyreso + miny |
| 80 | + |
| 81 | + d = math.sqrt(px**2 + py**2) |
| 82 | + angle = math.atan2(py, px) |
| 83 | + if angle < 0.0: |
| 84 | + angle += math.pi * 2.0 |
| 85 | + |
| 86 | + angleid = math.floor(angle / yawreso) |
| 87 | + |
| 88 | + pc = precastDB() |
| 89 | + |
| 90 | + pc.px = px |
| 91 | + pc.py = py |
| 92 | + pc.d = d |
| 93 | + pc.ix = ix |
| 94 | + pc.iy = iy |
| 95 | + pc.angle = angle |
| 96 | + |
| 97 | + precast[angleid].append(pc) |
| 98 | + |
| 99 | + return precast |
| 100 | + |
| 101 | + |
| 102 | +def generate_ray_casting_grid_map(ox, oy, xyreso): |
| 103 | + |
| 104 | + minx, miny, maxx, maxy, xw, yw = calc_grid_map_config(ox, oy, xyreso) |
| 105 | + |
| 106 | + pmap = [[0.0 for i in range(yw)] for i in range(xw)] |
| 107 | + |
| 108 | + yawreso = math.radians(10.0) |
| 109 | + |
| 110 | + precast = precasting(minx, miny, xw, yw, xyreso, yawreso) |
| 111 | + |
| 112 | + for (x, y) in zip(ox, oy): |
| 113 | + |
| 114 | + d = math.sqrt(x**2 + y**2) |
| 115 | + angle = math.atan2(y, x) |
| 116 | + if angle < 0.0: |
| 117 | + angle += math.pi * 2.0 |
| 118 | + |
| 119 | + angleid = math.floor(angle / yawreso) |
| 120 | + |
| 121 | + gridlist = precast[angleid] |
| 122 | + |
| 123 | + ix = int(round((x - minx) / xyreso)) |
| 124 | + iy = int(round((y - miny) / xyreso)) |
| 125 | + |
| 126 | + for grid in gridlist: |
| 127 | + if grid.d > (d): |
| 128 | + pmap[grid.ix][grid.iy] = 0.5 |
| 129 | + |
| 130 | + pmap[ix][iy] = 1.0 |
| 131 | + |
| 132 | + draw_heatmap(pmap, minx, maxx, miny, maxy, xyreso) |
| 133 | + plt.plot(ox, oy, "xr") |
| 134 | + plt.plot(0.0, 0.0, "ob") |
| 135 | + |
| 136 | + |
| 137 | +def draw_heatmap(data, minx, maxx, miny, maxy, xyreso): |
| 138 | + x, y = np.mgrid[slice(minx - xyreso / 2.0, maxx + xyreso / 2.0, xyreso), |
| 139 | + slice(miny - xyreso / 2.0, maxy + xyreso / 2.0, xyreso)] |
| 140 | + plt.pcolor(x, y, data, vmax=1.0, cmap=plt.cm.Blues) |
| 141 | + plt.axis("equal") |
| 142 | + |
| 143 | + |
| 144 | +def main(): |
| 145 | + print(__file__ + " start!!") |
| 146 | + |
| 147 | + xyreso = 0.5 |
| 148 | + |
| 149 | + for i in range(5): |
| 150 | + ox = (np.random.rand(4) - 0.5) * 10.0 |
| 151 | + oy = (np.random.rand(4) - 0.5) * 10.0 |
| 152 | + plt.cla() |
| 153 | + generate_gaussian_grid_map(ox, oy, xyreso) |
| 154 | + plt.pause(1.0) |
| 155 | + |
| 156 | + plt.cla() |
| 157 | + generate_ray_casting_grid_map(ox, oy, xyreso) |
| 158 | + plt.pause(1.0) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + main() |
0 commit comments