-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmm_planner.py
132 lines (105 loc) · 4.5 KB
/
fmm_planner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import cv2
import numpy as np
import skfmm
import skimage
from numpy import ma
def get_mask(sx, sy, scale, step_size):
size = int(step_size // scale) * 2 + 1
mask = np.zeros((size, size))
for i in range(size):
for j in range(size):
if ((i + 0.5) - (size // 2 + sx)) ** 2 + \
((j + 0.5) - (size // 2 + sy)) ** 2 <= \
step_size ** 2 \
and ((i + 0.5) - (size // 2 + sx)) ** 2 + \
((j + 0.5) - (size // 2 + sy)) ** 2 > \
(step_size - 1) ** 2:
mask[i, j] = 1
mask[size // 2, size // 2] = 1
return mask
def get_dist(sx, sy, scale, step_size):
size = int(step_size // scale) * 2 + 1
mask = np.zeros((size, size)) + 1e-10
for i in range(size):
for j in range(size):
if ((i + 0.5) - (size // 2 + sx)) ** 2 + \
((j + 0.5) - (size // 2 + sy)) ** 2 <= \
step_size ** 2:
mask[i, j] = max(5,
(((i + 0.5) - (size // 2 + sx)) ** 2 +
((j + 0.5) - (size // 2 + sy)) ** 2) ** 0.5)
return mask
class FMMPlanner():
def __init__(self, traversible, scale=1, step_size=5):
self.scale = scale
self.step_size = step_size
if scale != 1.:
self.traversible = cv2.resize(traversible,
(traversible.shape[1] // scale,
traversible.shape[0] // scale),
interpolation=cv2.INTER_NEAREST)
self.traversible = np.rint(self.traversible)
else:
self.traversible = traversible
self.du = int(self.step_size / (self.scale * 1.))
self.fmm_dist = None
def set_goal(self, goal, auto_improve=False):
traversible_ma = ma.masked_values(self.traversible * 1, 0)
goal_x, goal_y = int(goal[0] / (self.scale * 1.)), \
int(goal[1] / (self.scale * 1.))
if self.traversible[goal_x, goal_y] == 0. and auto_improve:
goal_x, goal_y = self._find_nearest_goal([goal_x, goal_y])
traversible_ma[goal_x, goal_y] = 0
dd = skfmm.distance(traversible_ma, dx=1)
dd = ma.filled(dd, np.max(dd) + 1)
self.fmm_dist = dd
return
def set_multi_goal(self, goal_map):
traversible_ma = ma.masked_values(self.traversible * 1, 0)
traversible_ma[goal_map == 1] = 0
dd = skfmm.distance(traversible_ma, dx=1)
dd = ma.filled(dd, np.max(dd) + 1)
self.fmm_dist = dd
return
def get_short_term_goal(self, state):
scale = self.scale * 1.
state = [x / scale for x in state]
dx, dy = state[0] - int(state[0]), state[1] - int(state[1])
mask = get_mask(dx, dy, scale, self.step_size)
dist_mask = get_dist(dx, dy, scale, self.step_size)
state = [int(x) for x in state]
dist = np.pad(self.fmm_dist, self.du,
'constant', constant_values=self.fmm_dist.shape[0] ** 2)
subset = dist[state[0]:state[0] + 2 * self.du + 1,
state[1]:state[1] + 2 * self.du + 1]
assert subset.shape[0] == 2 * self.du + 1 and \
subset.shape[1] == 2 * self.du + 1, \
"Planning error: unexpected subset shape {}".format(subset.shape)
subset *= mask
subset += (1 - mask) * self.fmm_dist.shape[0] ** 2
if subset[self.du, self.du] < 0.25 * 100 / 5.: # 25cm
stop = True
else:
stop = False
subset -= subset[self.du, self.du]
ratio1 = subset / dist_mask
subset[ratio1 < -1.5] = 1
(stg_x, stg_y) = np.unravel_index(np.argmin(subset), subset.shape)
if subset[stg_x, stg_y] > -0.0001:
replan = True
else:
replan = False
return (stg_x + state[0] - self.du) * scale, \
(stg_y + state[1] - self.du) * scale, replan, stop
def _find_nearest_goal(self, goal):
traversible = skimage.morphology.binary_dilation(
np.zeros(self.traversible.shape),
skimage.morphology.disk(2)) != True
traversible = traversible * 1.
planner = FMMPlanner(traversible)
planner.set_goal(goal)
mask = self.traversible
dist_map = planner.fmm_dist * mask
dist_map[dist_map == 0] = dist_map.max()
goal = np.unravel_index(dist_map.argmin(), dist_map.shape)
return goal