-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore.py
267 lines (235 loc) · 12.5 KB
/
explore.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import numpy as np
class Frontier():
'''
Frontier Class Struct to store data
'''
def __init__(self):
self.size = None
self.min_distance = None
self.cost = None
self.initial = None
self.middle = None
self.points = []
self.centroid = None
self.force = None
self.direct = None
class HeatMap():
'''
HeatMap Class to Create heatmap and update
'''
def __init__(self, workspace_limits, resolutions):
self.heatmap = np.zeros((resolutions, resolutions))
self.explore_complete = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
self.workspace_limits = workspace_limits
self.resolutions = resolutions
self.range_ = int(resolutions/50)
self.sub_range0 = {}
self.sub_range1 = {}
self.sub_range2 = {}
self.sub_range3 = {}
self.sub_range4 = {}
self.sub_range5 = {}
self.sub_range6 = {}
self.sub_range7 = {}
self.sub_range8 = {}
def MapToWorld(self, idx):
'''
using workspace limits to compute the world position of the map postion
map_pos -> world_pos
'''
world_x = (idx[0] * np.fabs(self.workspace_limits[0][0]-self.workspace_limits[0][1]))/self.resolutions + self.workspace_limits[0][0]
world_y = (idx[1] * np.fabs(self.workspace_limits[1][0]-self.workspace_limits[1][1]))/self.resolutions + self.workspace_limits[1][0]
return([world_x, world_y])
def WorldToMap(self, idx):
'''
using workspace limits to compute the map position of the world position
world_pos -> map_pos
'''
map_x = int((idx[0] - self.workspace_limits[0][0]) * self.resolutions/np.fabs(self.workspace_limits[0][0]-self.workspace_limits[0][1]))
map_y = int((idx[1] - self.workspace_limits[1][0]) * self.resolutions/np.fabs(self.workspace_limits[1][0]-self.workspace_limits[1][1]))
return([map_x, map_y])
def update_explore_complete(self, pos):
'''
After the explore step,
update the explore complete of the nine space
'''
if (pos[0]>=0 and pos[0] < int(self.resolutions/3)) and(pos[1]>=0 and pos[1] < int(self.resolutions/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range0:
self.sub_range0[state] = 1
self.explore_complete[0] = (self.explore_complete[0]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=0 and pos[0] < int(self.resolutions/3)) and(pos[1]>=int(self.resolutions/3) and pos[1] < int(self.resolutions*2/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range1:
self.sub_range1[state] = 1
self.explore_complete[1] = (self.explore_complete[1]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=0 and pos[0] < int(self.resolutions/3)) and(pos[1]>=int(self.resolutions*2/3) and pos[1] < int(self.resolutions)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range2:
self.sub_range2[state] = 1
self.explore_complete[2] = (self.explore_complete[2]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions/3) and pos[0] < int(self.resolutions*2/3)) and(pos[1]>=0 and pos[1] < int(self.resolutions/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range3:
self.sub_range3[state] = 1
self.explore_complete[3] = (self.explore_complete[3]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions/3) and pos[0] < int(self.resolutions*2/3)) and(pos[1]>=int(self.resolutions/3) and pos[1] < int(self.resolutions*2/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range4:
self.sub_range4[state] = 1
self.explore_complete[4] = (self.explore_complete[4]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions/3) and pos[0] < int(self.resolutions*2/3)) and(pos[1]>=int(self.resolutions*2/3) and pos[1] < int(self.resolutions)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range5:
self.sub_range5[state] = 1
self.explore_complete[5] = (self.explore_complete[5]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions*2/3) and pos[0] < int(self.resolutions)) and(pos[1]>=0 and pos[1] < int(self.resolutions/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range6:
self.sub_range6[state] = 1
self.explore_complete[6] = (self.explore_complete[6]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions*2/3) and pos[0] < int(self.resolutions)) and(pos[1]>=int(self.resolutions/3) and pos[1] < int(self.resolutions*2/3)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range7:
self.sub_range7[state] = 1
self.explore_complete[7] = (self.explore_complete[7]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
elif (pos[0]>=int(self.resolutions*2/3) and pos[0] < int(self.resolutions)) and(pos[1]>=int(self.resolutions*2/3) and pos[1] < int(self.resolutions)):
state = str(pos[0])+','+str(pos[1])
if state not in self.sub_range8:
self.sub_range0[state] = 1
self.explore_complete[8] = (self.explore_complete[8]*(self.resolutions*self.resolutions)/9 + 1)/((self.resolutions*self.resolutions)/9)
def updatemap(self, x, y, value):
'''
Update the heatmap with given value
'''
if (x >= 0 and x < self.heatmap.shape[0]) and (y >=0 and y < self.heatmap.shape[1]):
self.heatmap[x][y] = value
def updateFree(self, pos, angle):
'''
determine the position of the robot end,
and update the heatmap with free value
'''
self.update_explore_complete(pos)
for i in range(self.range_):
for j in range(self.range_):
#TODO: Add some if condition to judge in the map limits
x = int(pos[0]+ (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]+ (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 120)
x = int(pos[0]- (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]- (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 120)
x = int(pos[0]+ (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]- (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 120)
x = int(pos[0]- (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]+ (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 120)
def updateFrontier(self, pos, angle):
'''
determine the position of the robot end,
and update the heatmap with frontier value
'''
self.update_explore_complete(pos)
for i in range(self.range_):
for j in range(self.range_):
#TODO: Add some if condition to judge in the map limits
x = int(pos[0]+ (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]+ (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 255)
x = int(pos[0]+ (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]- (i * np.sin(angle) + j * np.cos(angle)))
self.heatmap[x][y] = 255
x = int(pos[0]- (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]- (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 255)
x = int(pos[0]- (i * np.cos(angle) - j * np.sin(angle)))
y = int(pos[1]+ (i * np.sin(angle) + j * np.cos(angle)))
self.updatemap(x, y, 255)
class FrontierSearch():
'''
FrontierSearch Class with heatmap
and act the action
'''
def __init__(self,workspace_limits, resolutions):
self.map = HeatMap(workspace_limits, resolutions)
self.action_space = ['x+', 'x-', 'y+', 'y-']
self.n_actions = len(self.action_space)
self.points = []
self.derive_points = []
def buildNewFree(self, initial_cell, initial_angle):
'''
built a New Free space
'''
# initialize frontier structure
frontier = Frontier()
frontier.centroid = self.map.WorldToMap(initial_cell)
frontier.direct = initial_angle
# build the initial frontier
self.map.updateFree(frontier.centroid, frontier.direct)
def buildNewFrontier(self, initial_cell, initial_force, initial_angle):
'''
built a New Frontier space
'''
# initialize frontier structure
frontier = Frontier()
frontier.centroid = self.map.WorldToMap(initial_cell)
frontier.force = initial_force
frontier.direct = initial_angle
# build the initial frontier
self.map.updateFrontier(frontier.centroid, frontier.direct)
self.points.append(frontier.centroid)
# build 2 neighboor
# for i in range(1):
# frontier_ = Frontier()
# frontier_.centroid = (
# frontier.centroid[0]-self.map.range_*(i+1)*2*np.cos(np.arctan2(frontier.force[0],frontier.force[1])-frontier.direct),
# frontier.centroid[1]+self.map.range_*(i+1)*2*np.sin(np.arctan2(frontier.force[0],frontier.force[1])-frontier.direct))
# self.map.updateFrontier(frontier_.centroid, frontier.direct)
# self.derive_points.append(frontier_.centroid)
# frontier_.centroid = (
# frontier.centroid[0]-self.map.range_*(i+1)*2*-np.cos(np.arctan2(frontier.force[0],frontier.force[1])-frontier.direct),
# frontier.centroid[1]+self.map.range_*(i+1)*2*-np.sin(np.arctan2(frontier.force[0],frontier.force[1])-frontier.direct))
# self.map.updateFrontier(frontier_.centroid, frontier.direct)
# self.derive_points.append(frontier_.centroid)
def step(self, action, current_pos, unit):
'''
action: | x+ | x- | y+ | y- |
'''
act_pos = [current_pos[0],current_pos[1]]
if action == 0: # x+
if ((current_pos[0] + unit) >= self.map.workspace_limits[0][0] and (current_pos[0] + unit) <= self.map.workspace_limits[0][1]):
act_pos[0] = current_pos[0] + unit
elif action == 1: # x-
if ((current_pos[0] - unit) >= self.map.workspace_limits[0][0] and (current_pos[0] - unit) <= self.map.workspace_limits[0][1]):
act_pos[0] = current_pos[0] - unit
elif action == 2: # x+
if ((current_pos[1] + unit) >= self.map.workspace_limits[1][0] and (current_pos[1] + unit) <= self.map.workspace_limits[1][1]):
act_pos[1] = current_pos[1] + unit
elif action == 3: # x-
if ((current_pos[1] - unit) >= self.map.workspace_limits[1][0] and (current_pos[1] - unit) <= self.map.workspace_limits[1][1]):
act_pos[1] = current_pos[1] - unit
return act_pos
def grasp_point_angle(self):
'''
Return the desired grasp position and angle
'''
grasp_point = [0.0, 0.0]
for i in range(len(self.points)):
grasp_point[0] += self.points[i][0]
grasp_point[1] += self.points[i][1]
grasp_point[0] = grasp_point[0]/len(self.points)
grasp_point[1] = grasp_point[1]/len(self.points)
grasp_angle = 0
for i in range(len(self.points)):
k = (grasp_point[1]-self.points[i][1])/(grasp_point[0]-self.points[i][0])
grasp_angle += k
grasp_angle = grasp_angle/len(self.points)
print(grasp_angle)
grasp_angle = np.arctan((grasp_angle))
grasp_point = self.map.MapToWorld(grasp_point)
print(grasp_point)
grasp_point[0] = grasp_point[0] - 0.04*np.sin(grasp_angle)
grasp_point[1] = grasp_point[1] - 0.04*np.cos(grasp_angle)
print(grasp_point)
return(grasp_point, grasp_angle)