Skip to content

Commit e8ffc01

Browse files
authored
Merge pull request AtsushiSakai#295 from AtsushiSakai/issue_293
fix AtsushiSakai#293
2 parents 06438d4 + 7c8a940 commit e8ffc01

File tree

2 files changed

+81
-56
lines changed

2 files changed

+81
-56
lines changed

Mapping/grid_map_lib/grid_map_lib.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ def __init__(self, width, height, resolution,
3232
self.center_x = center_x
3333
self.center_y = center_y
3434

35-
self.left_lower_x = self.center_x - \
36-
(self.width / 2.0) * self.resolution
37-
self.left_lower_y = self.center_y - \
38-
(self.height / 2.0) * self.resolution
35+
self.left_lower_x = self.center_x - self.width / 2.0 * self.resolution
36+
self.left_lower_y = self.center_y - self.height / 2.0 * self.resolution
3937

4038
self.ndata = self.width * self.height
4139
self.data = [init_val] * self.ndata
@@ -51,7 +49,7 @@ def get_value_from_xy_index(self, x_ind, y_ind):
5149

5250
grid_ind = self.calc_grid_index_from_xy_index(x_ind, y_ind)
5351

54-
if 0 <= grid_ind <= self.ndata:
52+
if 0 <= grid_ind < self.ndata:
5553
return self.data[grid_ind]
5654
else:
5755
return None
@@ -99,7 +97,6 @@ def set_value_from_xy_index(self, x_ind, y_ind, val):
9997
"""
10098

10199
if (x_ind is None) or (y_ind is None):
102-
print(x_ind, y_ind)
103100
return False, False
104101

105102
grid_ind = int(y_ind * self.width + x_ind)
@@ -163,7 +160,7 @@ def check_occupied_from_xy_index(self, xind, yind, occupied_val=1.0):
163160

164161
val = self.get_value_from_xy_index(xind, yind)
165162

166-
if val >= occupied_val:
163+
if val is None or val >= occupied_val:
167164
return True
168165
else:
169166
return False
@@ -200,12 +197,22 @@ def check_inside_polygon(iox, ioy, x, y):
200197
if not min_x < iox < max_x:
201198
continue
202199

203-
if (y[i1] + (y[i2] - y[i1]) / (x[i2] - x[i1])
204-
* (iox - x[i1]) - ioy) > 0.0:
200+
tmp1 = (y[i2] - y[i1]) / (x[i2] - x[i1])
201+
if (y[i1] + tmp1 * (iox - x[i1]) - ioy) > 0.0:
205202
inside = not inside
206203

207204
return inside
208205

206+
def print_grid_map_info(self):
207+
print("width:", self.width)
208+
print("height:", self.height)
209+
print("resolution:", self.resolution)
210+
print("center_x:", self.center_x)
211+
print("center_y:", self.center_y)
212+
print("left_lower_x:", self.left_lower_x)
213+
print("left_lower_y:", self.left_lower_y)
214+
print("ndata:", self.ndata)
215+
209216
def plot_grid_map(self, ax=None):
210217

211218
grid_data = np.reshape(np.array(self.data), (self.height, self.width))

PathPlanning/GridBasedSweepCPP/grid_based_sweep_coverage_path_planner.py

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,24 @@ class MovingDirection(IntEnum):
3030
RIGHT = 1
3131
LEFT = -1
3232

33-
def __init__(self, mdirection, sdirection, xinds_goaly, goaly):
34-
self.moving_direction = mdirection
35-
self.sweep_direction = sdirection
33+
def __init__(self,
34+
moving_direction, sweep_direction, x_inds_goal_y, goal_y):
35+
self.moving_direction = moving_direction
36+
self.sweep_direction = sweep_direction
3637
self.turing_window = []
3738
self.update_turning_window()
38-
self.xinds_goaly = xinds_goaly
39-
self.goaly = goaly
39+
self.xinds_goaly = x_inds_goal_y
40+
self.goaly = goal_y
4041

4142
def move_target_grid(self, cxind, cyind, gmap):
4243
nxind = self.moving_direction + cxind
4344
nyind = cyind
4445

4546
# found safe grid
46-
if not gmap.check_occupied_from_xy_index(nxind, nyind, occupied_val=0.5):
47+
if not gmap.check_occupied_from_xy_index(nxind, nyind,
48+
occupied_val=0.5):
4749
return nxind, nyind
48-
else: # occupided
50+
else: # occupied
4951
ncxind, ncyind = self.find_safe_turning_grid(cxind, cyind, gmap)
5052
if (ncxind is None) and (ncyind is None):
5153
# moving backward
@@ -56,33 +58,39 @@ def move_target_grid(self, cxind, cyind, gmap):
5658
return None, None
5759
else:
5860
# keep moving until end
59-
while not gmap.check_occupied_from_xy_index(ncxind + self.moving_direction, ncyind, occupied_val=0.5):
61+
while not gmap.check_occupied_from_xy_index(
62+
ncxind + self.moving_direction, ncyind,
63+
occupied_val=0.5):
6064
ncxind += self.moving_direction
6165
self.swap_moving_direction()
6266
return ncxind, ncyind
6367

6468
def find_safe_turning_grid(self, cxind, cyind, gmap):
6569

66-
for (dxind, dyind) in self.turing_window:
70+
for (d_x_ind, d_y_ind) in self.turing_window:
6771

68-
nxind = dxind + cxind
69-
nyind = dyind + cyind
72+
next_x_ind = d_x_ind + cxind
73+
next_y_ind = d_y_ind + cyind
7074

7175
# found safe grid
72-
if not gmap.check_occupied_from_xy_index(nxind, nyind, occupied_val=0.5):
73-
return nxind, nyind
76+
if not gmap.check_occupied_from_xy_index(next_x_ind, next_y_ind,
77+
occupied_val=0.5):
78+
return next_x_ind, next_y_ind
7479

7580
return None, None
7681

77-
def is_search_done(self, gmap):
82+
def is_search_done(self, grid_map):
7883
for ix in self.xinds_goaly:
79-
if not gmap.check_occupied_from_xy_index(ix, self.goaly, occupied_val=0.5):
84+
if not grid_map.check_occupied_from_xy_index(ix, self.goaly,
85+
occupied_val=0.5):
8086
return False
8187

8288
# all lower grid is occupied
8389
return True
8490

8591
def update_turning_window(self):
92+
# turning window definition
93+
# robot can move grid based on it.
8694
self.turing_window = [
8795
(self.moving_direction, 0.0),
8896
(self.moving_direction, self.sweep_direction),
@@ -95,22 +103,24 @@ def swap_moving_direction(self):
95103
self.update_turning_window()
96104

97105
def search_start_grid(self, grid_map):
98-
xinds = []
106+
x_inds = []
99107
y_ind = 0
100108
if self.sweep_direction == self.SweepDirection.DOWN:
101-
xinds, y_ind = search_free_grid_index_at_edge_y(grid_map, from_upper=True)
109+
x_inds, y_ind = search_free_grid_index_at_edge_y(
110+
grid_map, from_upper=True)
102111
elif self.sweep_direction == self.SweepDirection.UP:
103-
xinds, y_ind = search_free_grid_index_at_edge_y(grid_map, from_upper=False)
112+
x_inds, y_ind = search_free_grid_index_at_edge_y(
113+
grid_map, from_upper=False)
104114

105115
if self.moving_direction == self.MovingDirection.RIGHT:
106-
return min(xinds), y_ind
116+
return min(x_inds), y_ind
107117
elif self.moving_direction == self.MovingDirection.LEFT:
108-
return max(xinds), y_ind
118+
return max(x_inds), y_ind
109119

110120
raise ValueError("self.moving direction is invalid ")
111121

112122

113-
def find_sweep_direction_and_start_posi(ox, oy):
123+
def find_sweep_direction_and_start_position(ox, oy):
114124
# find sweep_direction
115125
max_dist = 0.0
116126
vec = [0.0, 0.0]
@@ -182,61 +192,65 @@ def search_free_grid_index_at_edge_y(grid_map, from_upper=False):
182192
def setup_grid_map(ox, oy, reso, sweep_direction, offset_grid=10):
183193
width = math.ceil((max(ox) - min(ox)) / reso) + offset_grid
184194
height = math.ceil((max(oy) - min(oy)) / reso) + offset_grid
185-
center_x = np.mean(ox)
186-
center_y = np.mean(oy)
195+
center_x = (np.max(ox)+np.min(ox))/2.0
196+
center_y = (np.max(oy)+np.min(oy))/2.0
187197

188198
grid_map = GridMap(width, height, reso, center_x, center_y)
189-
199+
grid_map.print_grid_map_info()
190200
grid_map.set_value_from_polygon(ox, oy, 1.0, inside=False)
191-
192201
grid_map.expand_grid()
193202

194-
xinds_goaly = []
195-
goaly = 0
203+
x_inds_goal_y = []
204+
goal_y = 0
196205
if sweep_direction == SweepSearcher.SweepDirection.UP:
197-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map, from_upper=True)
206+
x_inds_goal_y, goal_y = search_free_grid_index_at_edge_y(grid_map,
207+
from_upper=True)
198208
elif sweep_direction == SweepSearcher.SweepDirection.DOWN:
199-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map, from_upper=False)
209+
x_inds_goal_y, goal_y = search_free_grid_index_at_edge_y(grid_map,
210+
from_upper=False)
200211

201-
return grid_map, xinds_goaly, goaly
212+
return grid_map, x_inds_goal_y, goal_y
202213

203214

204-
def sweep_path_search(sweep_searcher, gmap, grid_search_animation=False):
215+
def sweep_path_search(sweep_searcher, grid_map, grid_search_animation=False):
205216
# search start grid
206-
cxind, cyind = sweep_searcher.search_start_grid(gmap)
207-
if not gmap.set_value_from_xy_index(cxind, cyind, 0.5):
217+
cxind, cyind = sweep_searcher.search_start_grid(grid_map)
218+
if not grid_map.set_value_from_xy_index(cxind, cyind, 0.5):
208219
print("Cannot find start grid")
209220
return [], []
210221

211-
x, y = gmap.calc_grid_central_xy_position_from_xy_index(cxind, cyind)
222+
x, y = grid_map.calc_grid_central_xy_position_from_xy_index(cxind, cyind)
212223
px, py = [x], [y]
213224

225+
fig, ax = None, None
214226
if grid_search_animation:
215227
fig, ax = plt.subplots()
216228
# for stopping simulation with the esc key.
217229
fig.canvas.mpl_connect('key_release_event',
218-
lambda event: [exit(0) if event.key == 'escape' else None])
230+
lambda event: [
231+
exit(0) if event.key == 'escape' else None])
219232

220233
while True:
221-
cxind, cyind = sweep_searcher.move_target_grid(cxind, cyind, gmap)
234+
cxind, cyind = sweep_searcher.move_target_grid(cxind, cyind, grid_map)
222235

223-
if sweep_searcher.is_search_done(gmap) or (cxind is None or cyind is None):
236+
if sweep_searcher.is_search_done(grid_map) or (
237+
cxind is None or cyind is None):
224238
print("Done")
225239
break
226240

227-
x, y = gmap.calc_grid_central_xy_position_from_xy_index(
241+
x, y = grid_map.calc_grid_central_xy_position_from_xy_index(
228242
cxind, cyind)
229243

230244
px.append(x)
231245
py.append(y)
232246

233-
gmap.set_value_from_xy_index(cxind, cyind, 0.5)
247+
grid_map.set_value_from_xy_index(cxind, cyind, 0.5)
234248

235249
if grid_search_animation:
236-
gmap.plot_grid_map(ax=ax)
250+
grid_map.plot_grid_map(ax=ax)
237251
plt.pause(1.0)
238252

239-
gmap.plot_grid_map()
253+
grid_map.plot_grid_map()
240254

241255
return px, py
242256

@@ -245,13 +259,16 @@ def planning(ox, oy, reso,
245259
moving_direction=SweepSearcher.MovingDirection.RIGHT,
246260
sweeping_direction=SweepSearcher.SweepDirection.UP,
247261
):
248-
sweep_vec, sweep_start_posi = find_sweep_direction_and_start_posi(ox, oy)
262+
sweep_vec, sweep_start_posi = find_sweep_direction_and_start_position(
263+
ox, oy)
249264

250265
rox, roy = convert_grid_coordinate(ox, oy, sweep_vec, sweep_start_posi)
251266

252-
gmap, xinds_goaly, goaly = setup_grid_map(rox, roy, reso, sweeping_direction)
267+
gmap, xinds_goaly, goaly = setup_grid_map(rox, roy, reso,
268+
sweeping_direction)
253269

254-
sweep_searcher = SweepSearcher(moving_direction, sweeping_direction, xinds_goaly, goaly)
270+
sweep_searcher = SweepSearcher(moving_direction, sweeping_direction,
271+
xinds_goaly, goaly)
255272

256273
px, py = sweep_path_search(sweep_searcher, gmap)
257274

@@ -270,8 +287,9 @@ def planning_animation(ox, oy, reso): # pragma: no cover
270287
for ipx, ipy in zip(px, py):
271288
plt.cla()
272289
# for stopping simulation with the esc key.
273-
plt.gcf().canvas.mpl_connect('key_release_event',
274-
lambda event: [exit(0) if event.key == 'escape' else None])
290+
plt.gcf().canvas.mpl_connect(
291+
'key_release_event',
292+
lambda event: [exit(0) if event.key == 'escape' else None])
275293
plt.plot(ox, oy, "-xb")
276294
plt.plot(px, py, "-r")
277295
plt.plot(ipx, ipy, "or")
@@ -285,6 +303,7 @@ def planning_animation(ox, oy, reso): # pragma: no cover
285303
plt.axis("equal")
286304
plt.grid(True)
287305
plt.pause(0.1)
306+
plt.close()
288307

289308

290309
def main(): # pragma: no cover
@@ -306,7 +325,6 @@ def main(): # pragma: no cover
306325
planning_animation(ox, oy, reso)
307326

308327
plt.show()
309-
310328
print("done!!")
311329

312330

0 commit comments

Comments
 (0)