forked from ElectronicElephant/pybullet_ur5_robotiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
264 lines (233 loc) · 11.3 KB
/
utilities.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
import pybullet as p
import glob
from collections import namedtuple
from attrdict import AttrDict
import functools
import torch
import cv2
from scipy import ndimage
import numpy as np
class Models:
def load_objects(self):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
def __getitem__(self, item):
return NotImplementedError
class YCBModels(Models):
def __init__(self, root, selected_names: tuple = ()):
self.obj_files = glob.glob(root)
self.selected_names = selected_names
self.visual_shapes = []
self.collision_shapes = []
def load_objects(self):
shift = [0, 0, 0]
mesh_scale = [1, 1, 1]
for filename in self.obj_files:
# Check selected_names
if self.selected_names:
in_selected = False
for name in self.selected_names:
if name in filename:
in_selected = True
if not in_selected:
continue
print('Loading %s' % filename)
self.collision_shapes.append(
p.createCollisionShape(shapeType=p.GEOM_MESH,
fileName=filename,
collisionFramePosition=shift,
meshScale=mesh_scale))
self.visual_shapes.append(
p.createVisualShape(shapeType=p.GEOM_MESH,
fileName=filename,
visualFramePosition=shift,
meshScale=mesh_scale))
def __len__(self):
return len(self.collision_shapes)
def __getitem__(self, idx):
return self.visual_shapes[idx], self.collision_shapes[idx]
def setup_sisbot(p, robotID, gripper_type):
controlJoints = ["shoulder_pan_joint", "shoulder_lift_joint",
"elbow_joint", "wrist_1_joint",
"wrist_2_joint", "wrist_3_joint",
"finger_joint"]
jointTypeList = ["REVOLUTE", "PRISMATIC", "SPHERICAL", "PLANAR", "FIXED"]
numJoints = p.getNumJoints(robotID)
jointInfo = namedtuple("jointInfo",
["id", "name", "type", "lowerLimit", "upperLimit", "maxForce", "maxVelocity",
"controllable"])
joints = AttrDict()
for i in range(numJoints):
info = p.getJointInfo(robotID, i)
jointID = info[0]
jointName = info[1].decode("utf-8")
jointType = jointTypeList[info[2]]
jointLowerLimit = info[8]
jointUpperLimit = info[9]
jointMaxForce = info[10]
jointMaxVelocity = info[11]
controllable = True if jointName in controlJoints else False
info = jointInfo(jointID, jointName, jointType, jointLowerLimit,
jointUpperLimit, jointMaxForce, jointMaxVelocity, controllable)
if info.type == "REVOLUTE": # set revolute joint to static
p.setJointMotorControl2(robotID, info.id, p.VELOCITY_CONTROL, targetVelocity=0, force=0)
joints[info.name] = info
# explicitly deal with mimic joints
def controlGripper(robotID, parent, children, mul, **kwargs):
controlMode = kwargs.pop("controlMode")
if controlMode == p.POSITION_CONTROL:
pose = kwargs.pop("targetPosition")
# move parent joint
p.setJointMotorControl2(robotID, parent.id, controlMode, targetPosition=pose,
force=parent.maxForce, maxVelocity=parent.maxVelocity)
# move child joints
for name in children:
child = children[name]
childPose = pose * mul[child.name]
p.setJointMotorControl2(robotID, child.id, controlMode, targetPosition=childPose,
force=child.maxForce, maxVelocity=child.maxVelocity)
else:
raise NotImplementedError("controlGripper does not support \"{}\" control mode".format(controlMode))
# check if there
if len(kwargs) is not 0:
raise KeyError("No keys {} in controlGripper".format(", ".join(kwargs.keys())))
assert gripper_type in ['85', '140']
mimicParentName = "finger_joint"
if gripper_type == '85':
mimicChildren = {"right_outer_knuckle_joint": 1,
"left_inner_knuckle_joint": 1,
"right_inner_knuckle_joint": 1,
"left_inner_finger_joint": -1,
"right_inner_finger_joint": -1}
else:
mimicChildren = {
"right_outer_knuckle_joint": -1,
"left_inner_knuckle_joint": -1,
"right_inner_knuckle_joint": -1,
"left_inner_finger_joint": 1,
"right_inner_finger_joint": 1}
parent = joints[mimicParentName]
children = AttrDict((j, joints[j]) for j in joints if j in mimicChildren.keys())
controlRobotiqC2 = functools.partial(controlGripper, robotID, parent, children, mimicChildren)
return joints, controlRobotiqC2, controlJoints, mimicParentName
def setup_sisbot_force(p, robotID, gripper_type):
controlJoints = ["shoulder_pan_joint", "shoulder_lift_joint",
"elbow_joint", "wrist_1_joint",
"wrist_2_joint", "wrist_3_joint",
"finger_joint"]
jointTypeList = ["REVOLUTE", "PRISMATIC", "SPHERICAL", "PLANAR", "FIXED"]
numJoints = p.getNumJoints(robotID)
jointInfo = namedtuple("jointInfo",
["id", "name", "type", "lowerLimit", "upperLimit", "maxForce", "maxVelocity",
"controllable", "jointAxis", "parentFramePos", "parentFrameOrn"])
joints = AttrDict()
for i in range(numJoints):
info = p.getJointInfo(robotID, i)
jointID = info[0]
jointName = info[1].decode("utf-8")
jointType = jointTypeList[info[2]]
jointLowerLimit = info[8]
jointUpperLimit = info[9]
jointMaxForce = info[10]
jointMaxVelocity = info[11]
jointAxis = info[13]
parentFramePos = info[14]
parentFrameOrn = info[15]
controllable = True if jointName in controlJoints else False
info = jointInfo(jointID, jointName, jointType, jointLowerLimit,
jointUpperLimit, jointMaxForce, jointMaxVelocity, controllable,
jointAxis, parentFramePos, parentFrameOrn)
if info.type == "REVOLUTE": # set revolute joint to static
p.setJointMotorControl2(robotID, info.id, p.VELOCITY_CONTROL, targetVelocity=0, force=0)
joints[info.name] = info
for j in joints:
print(joints[j])
# explicitly deal with mimic joints
def controlGripper(robotID, parent, children, mul, **kwargs):
controlMode = kwargs.pop("controlMode")
if controlMode == p.POSITION_CONTROL:
pose = kwargs.pop("targetPosition")
# move parent joint
p.setJointMotorControl2(robotID, parent.id, controlMode, targetPosition=pose,
force=parent.maxForce, maxVelocity=parent.maxVelocity)
# p.setJointMotorControl2(robotID, parent.id, p.TORQUE_CONTROL,
# force=10, maxVelocity=parent.maxVelocity)
return
# move child joints
for name in children:
child = children[name]
childPose = pose * mul[child.name]
p.setJointMotorControl2(robotID, child.id, controlMode, targetPosition=childPose,
force=child.maxForce, maxVelocity=child.maxVelocity)
else:
raise NotImplementedError("controlGripper does not support \"{}\" control mode".format(controlMode))
# check if there
if len(kwargs) is not 0:
raise KeyError("No keys {} in controlGripper".format(", ".join(kwargs.keys())))
assert gripper_type in ['85', '140']
mimicParentName = "finger_joint"
if gripper_type == '85':
mimicChildren = {"right_outer_knuckle_joint": 1,
"left_inner_knuckle_joint": 1,
"right_inner_knuckle_joint": 1,
"left_inner_finger_joint": -1,
"right_inner_finger_joint": -1}
else:
mimicChildren = {
"right_outer_knuckle_joint": -1,
"left_inner_knuckle_joint": -1,
"right_inner_knuckle_joint": -1,
"left_inner_finger_joint": 1,
"right_inner_finger_joint": 1}
parent = joints[mimicParentName]
children = AttrDict((j, joints[j]) for j in joints if j in mimicChildren.keys())
# Create all the gear constraint
for name in children:
child = children[name]
c = p.createConstraint(robotID, parent.id, robotID, child.id, p.JOINT_GEAR, child.jointAxis,
# child.parentFramePos, (0, 0, 0), child.parentFrameOrn, (0, 0, 0))
(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0))
p.changeConstraint(c, gearRatio=-mimicChildren[name], maxForce=10000)
controlRobotiqC2 = functools.partial(controlGripper, robotID, parent, children, mimicChildren)
return joints, controlRobotiqC2, controlJoints, mimicParentName
class Camera:
def __init__(self, cam_pos, near, far, size, fov):
self.x, self.y, self.z = cam_pos
self.width, self.height = size
self.near, self.far = near, far
self.fov = fov
aspect = self.width / self.height
self.view_matrix = p.computeViewMatrix([self.x, self.y, self.z],
[self.x - 1e-5, self.y, 0],
[-1, 0, 0])
self.projection_matrix = p.computeProjectionMatrixFOV(self.fov, aspect, self.near, self.far)
_view_matrix = np.array(self.view_matrix).reshape((4, 4), order='F')
_projection_matrix = np.array(self.projection_matrix).reshape((4, 4), order='F')
self.tran_pix_world = np.linalg.inv(_projection_matrix @ _view_matrix)
def rgbd_2_world(self, w, h, d):
x = (2 * w - self.width) / self.width
y = -(2 * h - self.height) / self.height
z = 2 * d - 1
pix_pos = np.array((x, y, z, 1))
position = self.tran_pix_world @ pix_pos
position /= position[3]
return position[:3]
def shot(self):
# Get depth values using the OpenGL renderer
_w, _h, rgb, depth, seg = p.getCameraImage(self.width, self.height,
self.view_matrix, self.projection_matrix,
)
return rgb, depth, seg
def rgbd_2_world_batch(self, depth):
x = (2 * np.arange(0, self.width) - self.width) / self.width
x = np.repeat(x[None, :], self.height, axis=0)
y = -(2 * np.arange(0, self.height) - self.height) / self.height
y = np.repeat(y[:, None], self.width, axis=1)
z = 2 * depth - 1
pix_pos = np.array([x.flatten(), y.flatten(), z.flatten(), np.ones_like(z.flatten())]).T
position = self.tran_pix_world @ pix_pos.T
position = position.T
# print(position)
position[:, :] /= position[:, 3:4]
return position[:, :3].reshape(*x.shape, -1)