-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavApfNavigation.py
209 lines (175 loc) · 5.66 KB
/
navApfNavigation.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
Potential Field based path planner
Ref:
https://www.cs.cmu.edu/~motionplanning/lecture/Chap4-Potential-Field_howie.pdf
Original work by:
https://github.com/AtsushiSakai/PythonRobotics
"""
import numpy as np
import logging
import time
# Modules
import config
from navMap import Map
class ApfNavigation:
def __init__(self, reso, rr):
# Define model constants
self.KP = 5.0 # attractive potential gain
self.ETA = 100.0 # repulsive potential gain
self.motion = config.general['robot_motion_model']
# Define object properties
self.reso = reso
self.rr = rr
self.pmap = None
self.minx = None
self.miny = None
self.ix = None
self.iy = None
self.gix = None
self.giy = None
self.scount = None
self.scount_limit = 3
self.rcheck = None
self.stuck = False
self.curdirx = None
self.curdiry = None
self.pvec = []
self.indecision = False
def set_cur_pos(self, xp, yp):
self.ix = xp
self.iy = yp
def get_pvec(self):
return self.pvec
def calc_potential_field(self, gx, gy, ox, oy):
myMap = Map()
myMap.set_params(self.reso, gx, gy, ox, oy)
pmap = myMap.create()
for ix in range(myMap.get_xw()):
x = ix * self.reso + myMap.get_minx()
for iy in range(myMap.get_yw()):
y = iy * self.reso + myMap.get_miny()
ug = self.calc_attractive_potential(x, y, gx, gy)
uo = self.calc_repulsive_potential(x, y, ox, oy)
uf = ug + uo
pmap[ix][iy] = uf
myMap.set_map(pmap)
return pmap, myMap.get_minx(), myMap.get_miny()
def calc_attractive_potential(self, x, y, gx, gy):
return 0.5 * self.KP * np.hypot(x - gx, y - gy)
def calc_repulsive_potential(self, x, y, ox, oy):
# search nearest obstacle
minid = -1
minid = -1
dmin = float("inf")
for i in range(len(ox)):
d = np.hypot(x - ox[i], y - oy[i])
if dmin >= d:
dmin = d
minid = i
# calc repulsive potential
dq = np.hypot(x - ox[minid], y - oy[minid])
if dq <= self.rr:
if dq <= 0.1:
dq = 0.1
return 0.5 * self.ETA * (1.0 / dq - 1.0 / self.rr) ** 2
else:
return 0.0
def get_pmap(self):
return self.pmap
def get_motion_model(self):
return self.motion
def get_cur_dir(self):
return self.curdirx, self.curdiry
def set_motion_model(self, motion):
self.motion = motion
def reset_motion_model(self):
self.motion = config.general['robot_motion_model']
def decide_status(self, rx, ry, xp, yp):
self.stuck = False
# Checking if we get stuck...
if len(rx) > 1:
logging.debug("<APF> -- decide_status() | rx,ry [-2]: " + str((rx[-2],ry[-2])) + " | xp,yp: " + str((xp,yp)))
if rx[-2] == xp:
if ry[-2] == yp:
logging.debug("<APF> -- decide_status() | WE ARE STUCK!")
# If we are taking same step again
self.stuck = True
return self.stuck
# decide_status() is not working whith local goals
def decide_status_v2(self, rd):
## Checking if we get stuck...
dif = rd[-2] - rd[-1]
logging.debug("<APF> -- decide_status() | dif: " + str(dif) + " | Indecision: " + str(self.indecision))
# Si aumento la distancia al objetivo
if dif < 0 and self.scount == 0:
self.scount = 1
self.rcheck = rd[-2]
logging.debug("<APF> -- Stuck? : " + str(self.rcheck))
elif self.scount != 0:
if (self.rcheck - rd[-1]) <= self.reso and self.scount <= self.scount_limit:
self.scount += 1
logging.debug("<APF> -- Still stuck... scount: " + str(self.scount))
elif (self.rcheck - rd[-1]) <= self.reso and self.scount > self.scount_limit:
logging.debug("<APF> -- Now we are really stuck!!!")
self.stuck = True
elif (self.rcheck - rd[-1]) > self.reso:
logging.debug("<APF> -- We got out of rcheck+reso area")
self.scount = 0
else:
logging.error("<APF> -- How did we get here? (scount!=0)")
else:
self.scount = 0
return self.stuck
def potential_field_planning(self, sx, sy, gx, gy, ox, oy, start):
if start:
print("<APF> -- calc_potential_field() processing has started.")
t0c=time.process_time()
t0t=time.time()
self.pmap, self.minx, self.miny = self.calc_potential_field(gx, gy, ox, oy)
# search path
d = np.hypot(sx - gx, sy - gy)
# Search variables
self.ix = round((sx - self.minx) / self.reso)
self.iy = round((sy - self.miny) / self.reso)
self.gix = round((gx - self.minx) / self.reso)
self.giy = round((gy - self.miny) / self.reso)
# Results list initialization
self.rx, self.ry, self.rd = [sx], [sy], [d]
# Local minimum detection vars
self.scount = 0
self.rcheck = None
self.stuck = False
t1c = time.process_time()
t1t = time.time()
msg = "<APF> -- Processing finished. Wall time spent: " + str(round(t1t-t0t,2)) + " | CPU time: " + str(round(t1c-t0c,2))
logging.debug(msg)
print(msg)
motion = self.get_motion_model()
minp = float("inf")
self.pvec = []
local_pvec = []
self.minix, self.miniy = -1, -1
for i in range(len(motion)):
inx = int(self.ix + motion[i][0])
iny = int(self.iy + motion[i][1])
if inx >= len(self.pmap) or iny >= len(self.pmap[0]):
p = float("inf") # outside area
else:
p = self.pmap[inx][iny]
self.pvec.append((p, motion[i][0], motion[i][1]))
local_pvec.append((p, (motion[i][0], motion[i][1]), (inx, iny)))
local_pvec = sorted(local_pvec, key=lambda pvec: pvec[0])
if local_pvec[0][0] == local_pvec[1][0]:
# Two directions with same potential, need a better way to decide
self.indecision = True
else:
# Only one best direction, we can go with that one
self.indecision = False
self.curdirx = local_pvec[0][1][0]
self.curdiry = local_pvec[0][1][1]
self.ix = local_pvec[0][2][0]
self.iy = local_pvec[0][2][1]
xp = self.ix * self.reso + self.minx
yp = self.iy * self.reso + self.miny
d = np.hypot(gx - xp, gy - yp)
return d, xp, yp, self.curdirx, self.curdiry