-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrasp_choosing.py
255 lines (200 loc) · 8.78 KB
/
grasp_choosing.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
import open3d as o3d
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
import numpy as np
import pybullet as pb
from pytransform3d.transformations import (
screw_axis_from_screw_parameters, transform_from_pq,
transform_from_exponential_coordinates, pq_from_transform)
def joint_info_ext(object):
# List for saving only movable joints
non_fixed_joints = []
for i in range(pb.getNumJoints(object)):
info = pb.getJointInfo(object, i)
jointID = info[0]
# jointName = info[1].decode("utf-8")
jointType = info[2] # JOINT_REVOLUTE, JOINT_PRISMATIC, JOINT_SPHERICAL, JOINT_PLANAR, JOINT_FIXED
# jointDamping = info[6]
# jointFriction = info[7]
jointLowerLimit = info[8]
jointUpperLimit = info[9]
# jointMaxForce = info[10]
# jointMaxVelocity = info[11]
# LinkName = info[12]
jointAxis = info[13]
parentFramePos = info[14]
parentFrameOrn = info[15]
# parentIndex = info[16]
if jointType != pb.JOINT_FIXED:
state = pb.getLinkState(object, jointID)
linkWorldPosition = state[0]
linkWorldOrientation = state[1]
non_fixed_joints.append([jointID, jointAxis,
parentFramePos, parentFrameOrn, linkWorldPosition, linkWorldOrientation,
jointUpperLimit, jointLowerLimit, jointType])
for j in non_fixed_joints:
pq_world2center = np.concatenate(([0, 0, 0], linkWorldOrientation)) # COM not defined in URDF. modify if it is
T_world2center = transform_from_pq(pq_world2center)
s_axis = np.matmul(T_world2center, np.concatenate((jointAxis, np.zeros(1))))[:3]
q = np.array(linkWorldPosition)
return non_fixed_joints[0][8], s_axis, q # type
# indices : list, index of grasps that are wanted to be visualized
# grasps : list of SE(3) grasps, num X 4 X 4
def visualizer(indices, grasps, scores, pcd):
points = [
[0, 0, 0],
[0, 0, 0.0624],
[0.0399, 0, 0.0624],
[-0.0399, 0, 0.0624],
[0.0399, 0, 0.1034],
[-0.0399, 0, 0.1034]
]
lines = [
[0, 1],
[1, 2],
[1, 3],
[2, 4],
[3, 5]
]
grasp_list = []
# drawing the world coordinate
mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.05, origin=[0, 0, 0])
num_draw = len(indices)
# getting grasp poses of the object part #1 (handle)
for i in range(num_draw):
this_grasp = grasps[indices[i], :, :]
this_score = scores[indices[i]]
if this_score > 0.5:
colors = [[0, 0, 1] for j in range(len(lines))]
elif this_score > 0.4:
colors = [[0, 1, 0] for j in range(len(lines))]
elif this_score > 0.3:
colors = [[1, 0, 0] for j in range(len(lines))]
else:
colors = [[0, 0, 0] for j in range(len(lines))]
# drawing the robot gripper
line_set = o3d.geometry.LineSet(points=o3d.utility.Vector3dVector(points),
lines=o3d.utility.Vector2iVector(lines))
line_set.colors = o3d.utility.Vector3dVector(colors)
# rotating and translating the robot gripper to grasp poses
line_set.rotate(this_grasp[:3, :3], center=[0, 0, 0])
line_set.translate(this_grasp[:3, 3])
grasp_list.append(line_set)
# visualize the point cloud and grasp poses
o3d.visualization.draw_geometries(grasp_list + [pcd, mesh_frame])
# visualize all grasps, green has high scores, blue has low scores
def visualizer_all(indices, grasps, scores):
points = [
[0, 0, 0],
[0, 0, 0.0624],
[0.0399, 0, 0.0624],
[-0.0399, 0, 0.0624],
[0.0399, 0, 0.1034],
[-0.0399, 0, 0.1034]
]
lines = [
[0, 1],
[1, 2],
[1, 3],
[2, 4],
[3, 5]
]
grasp_list = []
# drawing the world coordinate
mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.05, origin=[0, 0, 0])
num_draw = len(indices)
# getting grasp poses of the object part #1 (handle)
for i in range(num_draw):
this_grasp = grasps[indices[i], :, :]
this_score = scores[indices[i]]
# Black, Red, Green, Blue : getting better grasp scores
if this_score > 0.5:
colors = [[0, 0, 1] for j in range(len(lines))]
elif this_score > 0.4:
colors = [[0, 1, 0] for j in range(len(lines))]
elif this_score > 0.3:
colors = [[1, 0, 0] for j in range(len(lines))]
else:
colors = [[0, 0, 0] for j in range(len(lines))]
# drawing the robot gripper
line_set = o3d.geometry.LineSet(points=o3d.utility.Vector3dVector(points),
lines=o3d.utility.Vector2iVector(lines))
line_set.colors = o3d.utility.Vector3dVector(colors)
# rotating and translating the robot gripper to grasp poses
line_set.rotate(this_grasp[:3, :3], center=[0, 0, 0])
line_set.translate(this_grasp[:3, 3])
grasp_list.append(line_set)
# visualize the point cloud and grasp poses
o3d.visualization.draw_geometries(grasp_list + [pcd, mesh_frame])
def grasp_chooser(candidates, scores, num_grasp, w, q):
def find_distance(grasp_origin, screw_w, screw_q): # finding distance between gripper finger center and joint axis
distance = np.cross(grasp_origin - screw_q, screw_w)
distance = np.linalg.norm(distance)
distance = distance / np.linalg.norm(screw_w)
return distance
tmpscores = scores.copy()
index = []
for i in range(num_grasp):
tmpindex = tmpscores.argmax()
index.append(tmpindex)
tmpscores[tmpindex] = 0
current_max_distance = 0
current_max_index = -1
for j in index:
T_world2grasp = candidates[j, :, :]
G_grasp = np.array([0, 0, 0.1034, 1]) # position of gripper center seen from end-effector frame
G_world = (T_world2grasp @ G_grasp)[:3] # position of gripper center seen from world frame
d = find_distance(G_world, w, q)
if d > current_max_distance:
current_max_index = j
current_max_distance = d
print('current_max_distance')
print(current_max_distance)
return current_max_index
def grasp_chooser_maxgraspscore(candidates, scores, num_grasp):
tmpscores = scores.copy()
index = []
for i in range(num_grasp):
tmpindex = tmpscores.argmax()
index.append(tmpindex)
tmpscores[tmpindex] = 0
return index
if __name__ == '__main__':
object_num = str(148)
child_num = 1
# changed
# loading point cloud of the object
pcd = o3d.io.read_point_cloud(f'./{object_num}/point_sample/ply-10000.ply')
pcd.scale(0.1, [0, 0, 0])
# loading grasp poses of the object
grasp_data = np.load(f'./{object_num}/point_sample/grasp_poses.npz', allow_pickle=True)
lst = grasp_data.files
# load joint information
physicsClient = pb.connect(pb.DIRECT)
artobj = pb.loadURDF(f'./{object_num}/mobility.urdf', flags=pb.URDF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS,
useFixedBase=True, globalScaling=0.1)
# 699 grasps for link0, 101 grasps for link1
"""
#for i in lst:
#print(i)
for i in grasp_data['pred_grasps_cam'].item():
print(i, grasp_data['pred_grasps_cam'].item()[i].shape) #is (699,4,4) (101,4,4)
print(i, grasp_data['scores'].item()[i].shape) #is (699,) (101,)
"""
joint_type, w, q = joint_info_ext(artobj)
# all SE(3) grasp poses for child link, Num_grasp X 4 X 4
grasp_candidates = grasp_data['pred_grasps_cam'].item()[child_num]
grasp_scores = grasp_data['scores'].item()[child_num] # corresponding scores, vector of Num_grasp
# takes top from_topk grasps according to grasp scores, and choose best operability score among them
from_topk = 20 # may have to be modified
if joint_type: # prismatic, distance doesn't matter significantly in this case
best_index = grasp_chooser(grasp_candidates, grasp_scores, 5, w, q)
else: # revolute
best_index = grasp_chooser(grasp_candidates, grasp_scores, from_topk, w, q)
print(best_index)
print(grasp_candidates[best_index])
visualizer([best_index], grasp_candidates, grasp_scores, pcd)
"""
top_grasp_score_indices = grasp_chooser_maxgraspscore(grasp_candidates, grasp_scores, 50)
visualizer(top_grasp_score_indices, grasp_candidates, grasp_scores)
"""