Skip to content

Commit 7c8a940

Browse files
committed
fixed the bug and code clean up.
1 parent 41d744c commit 7c8a940

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

Mapping/grid_map_lib/grid_map_lib.py

Lines changed: 14 additions & 7 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
@@ -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)
@@ -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: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class MovingDirection(IntEnum):
3030
RIGHT = 1
3131
LEFT = -1
3232

33-
def __init__(self, moving_direction, sweep_direction, x_inds_goal_y, goal_y):
33+
def __init__(self,
34+
moving_direction, sweep_direction, x_inds_goal_y, goal_y):
3435
self.moving_direction = moving_direction
3536
self.sweep_direction = sweep_direction
3637
self.turing_window = []
@@ -88,6 +89,8 @@ def is_search_done(self, grid_map):
8889
return True
8990

9091
def update_turning_window(self):
92+
# turning window definition
93+
# robot can move grid based on it.
9194
self.turing_window = [
9295
(self.moving_direction, 0.0),
9396
(self.moving_direction, self.sweep_direction),
@@ -189,35 +192,34 @@ def search_free_grid_index_at_edge_y(grid_map, from_upper=False):
189192
def setup_grid_map(ox, oy, reso, sweep_direction, offset_grid=10):
190193
width = math.ceil((max(ox) - min(ox)) / reso) + offset_grid
191194
height = math.ceil((max(oy) - min(oy)) / reso) + offset_grid
192-
center_x = np.mean(ox)
193-
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
194197

195198
grid_map = GridMap(width, height, reso, center_x, center_y)
196-
199+
grid_map.print_grid_map_info()
197200
grid_map.set_value_from_polygon(ox, oy, 1.0, inside=False)
198-
199201
grid_map.expand_grid()
200202

201-
xinds_goaly = []
202-
goaly = 0
203+
x_inds_goal_y = []
204+
goal_y = 0
203205
if sweep_direction == SweepSearcher.SweepDirection.UP:
204-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map,
206+
x_inds_goal_y, goal_y = search_free_grid_index_at_edge_y(grid_map,
205207
from_upper=True)
206208
elif sweep_direction == SweepSearcher.SweepDirection.DOWN:
207-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map,
209+
x_inds_goal_y, goal_y = search_free_grid_index_at_edge_y(grid_map,
208210
from_upper=False)
209211

210-
return grid_map, xinds_goaly, goaly
212+
return grid_map, x_inds_goal_y, goal_y
211213

212214

213-
def sweep_path_search(sweep_searcher, gmap, grid_search_animation=False):
215+
def sweep_path_search(sweep_searcher, grid_map, grid_search_animation=False):
214216
# search start grid
215-
cxind, cyind = sweep_searcher.search_start_grid(gmap)
216-
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):
217219
print("Cannot find start grid")
218220
return [], []
219221

220-
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)
221223
px, py = [x], [y]
222224

223225
fig, ax = None, None
@@ -229,26 +231,26 @@ def sweep_path_search(sweep_searcher, gmap, grid_search_animation=False):
229231
exit(0) if event.key == 'escape' else None])
230232

231233
while True:
232-
cxind, cyind = sweep_searcher.move_target_grid(cxind, cyind, gmap)
234+
cxind, cyind = sweep_searcher.move_target_grid(cxind, cyind, grid_map)
233235

234-
if sweep_searcher.is_search_done(gmap) or (
236+
if sweep_searcher.is_search_done(grid_map) or (
235237
cxind is None or cyind is None):
236238
print("Done")
237239
break
238240

239-
x, y = gmap.calc_grid_central_xy_position_from_xy_index(
241+
x, y = grid_map.calc_grid_central_xy_position_from_xy_index(
240242
cxind, cyind)
241243

242244
px.append(x)
243245
py.append(y)
244246

245-
gmap.set_value_from_xy_index(cxind, cyind, 0.5)
247+
grid_map.set_value_from_xy_index(cxind, cyind, 0.5)
246248

247249
if grid_search_animation:
248-
gmap.plot_grid_map(ax=ax)
250+
grid_map.plot_grid_map(ax=ax)
249251
plt.pause(1.0)
250252

251-
gmap.plot_grid_map()
253+
grid_map.plot_grid_map()
252254

253255
return px, py
254256

@@ -307,11 +309,6 @@ def planning_animation(ox, oy, reso): # pragma: no cover
307309
def main(): # pragma: no cover
308310
print("start!!")
309311

310-
ox = [0.0, 50.0, 50.0, 0.0, 0.0]
311-
oy = [0.0, 0.0, 50.0, 50.0, 0.0]
312-
reso = 0.4
313-
planning_animation(ox, oy, reso)
314-
315312
ox = [0.0, 20.0, 50.0, 100.0, 130.0, 40.0, 0.0]
316313
oy = [0.0, -20.0, 0.0, 30.0, 60.0, 80.0, 0.0]
317314
reso = 5.0
@@ -328,7 +325,6 @@ def main(): # pragma: no cover
328325
planning_animation(ox, oy, reso)
329326

330327
plt.show()
331-
332328
print("done!!")
333329

334330

0 commit comments

Comments
 (0)