Skip to content

Commit 41d744c

Browse files
committed
add error handling. But there is still bug.
1 parent 06438d4 commit 41d744c

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

Mapping/grid_map_lib/grid_map_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_value_from_xy_index(self, x_ind, y_ind):
5151

5252
grid_ind = self.calc_grid_index_from_xy_index(x_ind, y_ind)
5353

54-
if 0 <= grid_ind <= self.ndata:
54+
if 0 <= grid_ind < self.ndata:
5555
return self.data[grid_ind]
5656
else:
5757
return None
@@ -163,7 +163,7 @@ def check_occupied_from_xy_index(self, xind, yind, occupied_val=1.0):
163163

164164
val = self.get_value_from_xy_index(xind, yind)
165165

166-
if val >= occupied_val:
166+
if val is None or val >= occupied_val:
167167
return True
168168
else:
169169
return False

PathPlanning/GridBasedSweepCPP/grid_based_sweep_coverage_path_planner.py

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,23 @@ 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, moving_direction, sweep_direction, x_inds_goal_y, goal_y):
34+
self.moving_direction = moving_direction
35+
self.sweep_direction = sweep_direction
3636
self.turing_window = []
3737
self.update_turning_window()
38-
self.xinds_goaly = xinds_goaly
39-
self.goaly = goaly
38+
self.xinds_goaly = x_inds_goal_y
39+
self.goaly = goal_y
4040

4141
def move_target_grid(self, cxind, cyind, gmap):
4242
nxind = self.moving_direction + cxind
4343
nyind = cyind
4444

4545
# found safe grid
46-
if not gmap.check_occupied_from_xy_index(nxind, nyind, occupied_val=0.5):
46+
if not gmap.check_occupied_from_xy_index(nxind, nyind,
47+
occupied_val=0.5):
4748
return nxind, nyind
48-
else: # occupided
49+
else: # occupied
4950
ncxind, ncyind = self.find_safe_turning_grid(cxind, cyind, gmap)
5051
if (ncxind is None) and (ncyind is None):
5152
# moving backward
@@ -56,27 +57,31 @@ def move_target_grid(self, cxind, cyind, gmap):
5657
return None, None
5758
else:
5859
# keep moving until end
59-
while not gmap.check_occupied_from_xy_index(ncxind + self.moving_direction, ncyind, occupied_val=0.5):
60+
while not gmap.check_occupied_from_xy_index(
61+
ncxind + self.moving_direction, ncyind,
62+
occupied_val=0.5):
6063
ncxind += self.moving_direction
6164
self.swap_moving_direction()
6265
return ncxind, ncyind
6366

6467
def find_safe_turning_grid(self, cxind, cyind, gmap):
6568

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

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

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

7579
return None, None
7680

77-
def is_search_done(self, gmap):
81+
def is_search_done(self, grid_map):
7882
for ix in self.xinds_goaly:
79-
if not gmap.check_occupied_from_xy_index(ix, self.goaly, occupied_val=0.5):
83+
if not grid_map.check_occupied_from_xy_index(ix, self.goaly,
84+
occupied_val=0.5):
8085
return False
8186

8287
# all lower grid is occupied
@@ -95,22 +100,24 @@ def swap_moving_direction(self):
95100
self.update_turning_window()
96101

97102
def search_start_grid(self, grid_map):
98-
xinds = []
103+
x_inds = []
99104
y_ind = 0
100105
if self.sweep_direction == self.SweepDirection.DOWN:
101-
xinds, y_ind = search_free_grid_index_at_edge_y(grid_map, from_upper=True)
106+
x_inds, y_ind = search_free_grid_index_at_edge_y(
107+
grid_map, from_upper=True)
102108
elif self.sweep_direction == self.SweepDirection.UP:
103-
xinds, y_ind = search_free_grid_index_at_edge_y(grid_map, from_upper=False)
109+
x_inds, y_ind = search_free_grid_index_at_edge_y(
110+
grid_map, from_upper=False)
104111

105112
if self.moving_direction == self.MovingDirection.RIGHT:
106-
return min(xinds), y_ind
113+
return min(x_inds), y_ind
107114
elif self.moving_direction == self.MovingDirection.LEFT:
108-
return max(xinds), y_ind
115+
return max(x_inds), y_ind
109116

110117
raise ValueError("self.moving direction is invalid ")
111118

112119

113-
def find_sweep_direction_and_start_posi(ox, oy):
120+
def find_sweep_direction_and_start_position(ox, oy):
114121
# find sweep_direction
115122
max_dist = 0.0
116123
vec = [0.0, 0.0]
@@ -194,9 +201,11 @@ def setup_grid_map(ox, oy, reso, sweep_direction, offset_grid=10):
194201
xinds_goaly = []
195202
goaly = 0
196203
if sweep_direction == SweepSearcher.SweepDirection.UP:
197-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map, from_upper=True)
204+
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map,
205+
from_upper=True)
198206
elif sweep_direction == SweepSearcher.SweepDirection.DOWN:
199-
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map, from_upper=False)
207+
xinds_goaly, goaly = search_free_grid_index_at_edge_y(grid_map,
208+
from_upper=False)
200209

201210
return grid_map, xinds_goaly, goaly
202211

@@ -211,16 +220,19 @@ def sweep_path_search(sweep_searcher, gmap, grid_search_animation=False):
211220
x, y = gmap.calc_grid_central_xy_position_from_xy_index(cxind, cyind)
212221
px, py = [x], [y]
213222

223+
fig, ax = None, None
214224
if grid_search_animation:
215225
fig, ax = plt.subplots()
216226
# for stopping simulation with the esc key.
217227
fig.canvas.mpl_connect('key_release_event',
218-
lambda event: [exit(0) if event.key == 'escape' else None])
228+
lambda event: [
229+
exit(0) if event.key == 'escape' else None])
219230

220231
while True:
221232
cxind, cyind = sweep_searcher.move_target_grid(cxind, cyind, gmap)
222233

223-
if sweep_searcher.is_search_done(gmap) or (cxind is None or cyind is None):
234+
if sweep_searcher.is_search_done(gmap) or (
235+
cxind is None or cyind is None):
224236
print("Done")
225237
break
226238

@@ -245,13 +257,16 @@ def planning(ox, oy, reso,
245257
moving_direction=SweepSearcher.MovingDirection.RIGHT,
246258
sweeping_direction=SweepSearcher.SweepDirection.UP,
247259
):
248-
sweep_vec, sweep_start_posi = find_sweep_direction_and_start_posi(ox, oy)
260+
sweep_vec, sweep_start_posi = find_sweep_direction_and_start_position(
261+
ox, oy)
249262

250263
rox, roy = convert_grid_coordinate(ox, oy, sweep_vec, sweep_start_posi)
251264

252-
gmap, xinds_goaly, goaly = setup_grid_map(rox, roy, reso, sweeping_direction)
265+
gmap, xinds_goaly, goaly = setup_grid_map(rox, roy, reso,
266+
sweeping_direction)
253267

254-
sweep_searcher = SweepSearcher(moving_direction, sweeping_direction, xinds_goaly, goaly)
268+
sweep_searcher = SweepSearcher(moving_direction, sweeping_direction,
269+
xinds_goaly, goaly)
255270

256271
px, py = sweep_path_search(sweep_searcher, gmap)
257272

@@ -270,8 +285,9 @@ def planning_animation(ox, oy, reso): # pragma: no cover
270285
for ipx, ipy in zip(px, py):
271286
plt.cla()
272287
# 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])
288+
plt.gcf().canvas.mpl_connect(
289+
'key_release_event',
290+
lambda event: [exit(0) if event.key == 'escape' else None])
275291
plt.plot(ox, oy, "-xb")
276292
plt.plot(px, py, "-r")
277293
plt.plot(ipx, ipy, "or")
@@ -285,11 +301,17 @@ def planning_animation(ox, oy, reso): # pragma: no cover
285301
plt.axis("equal")
286302
plt.grid(True)
287303
plt.pause(0.1)
304+
plt.close()
288305

289306

290307
def main(): # pragma: no cover
291308
print("start!!")
292309

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+
293315
ox = [0.0, 20.0, 50.0, 100.0, 130.0, 40.0, 0.0]
294316
oy = [0.0, -20.0, 0.0, 30.0, 60.0, 80.0, 0.0]
295317
reso = 5.0

0 commit comments

Comments
 (0)