-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobo_gripper.py
96 lines (84 loc) · 3.77 KB
/
robo_gripper.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
#@markdown Gripper (Robotiq 2F85) code
import pybullet
import numpy as np
import threading
import time
class Robotiq2F85:
"""Gripper handling for Robotiq 2F85."""
def __init__(self, robot, tool):
self.robot = robot
self.tool = tool
pos = [0.1339999999999999, -0.49199999999872496, 0.5]
rot = pybullet.getQuaternionFromEuler([np.pi, 0, np.pi])
urdf = "robotiq_2f_85/robotiq_2f_85.urdf"
self.body = pybullet.loadURDF(urdf, pos, rot)
self.n_joints = pybullet.getNumJoints(self.body)
self.activated = False
# Connect gripper base to robot tool.
pybullet.createConstraint(self.robot, tool, self.body, 0, jointType=pybullet.JOINT_FIXED, jointAxis=[0, 0, 0], parentFramePosition=[0, 0, 0], childFramePosition=[0, 0, -0.07], childFrameOrientation=pybullet.getQuaternionFromEuler([0, 0, np.pi / 2]))
# Set friction coefficients for gripper fingers.
for i in range(pybullet.getNumJoints(self.body)):
pybullet.changeDynamics(self.body, i, lateralFriction=10.0, spinningFriction=1.0, rollingFriction=1.0, frictionAnchor=True)
# Start thread to handle additional gripper constraints.
self.motor_joint = 1
self.constraints_thread = threading.Thread(target=self.step)
self.constraints_thread.daemon = True
self.constraints_thread.start()
# Control joint positions by enforcing hard contraints on gripper behavior.
# Set one joint as the open/close motor joint (other joints should mimic).
def step(self):
while True:
try:
currj = [pybullet.getJointState(self.body, i)[0] for i in range(self.n_joints)]
indj = [6, 3, 8, 5, 10]
targj = [currj[1], -currj[1], -currj[1], currj[1], currj[1]]
pybullet.setJointMotorControlArray(self.body, indj, pybullet.POSITION_CONTROL, targj, positionGains=np.ones(5))
except:
return
time.sleep(0.001)
# Close gripper fingers.
def activate(self):
pybullet.setJointMotorControl2(self.body, self.motor_joint, pybullet.VELOCITY_CONTROL, targetVelocity=1, force=10)
self.activated = True
# Open gripper fingers.
def release(self):
pybullet.setJointMotorControl2(self.body, self.motor_joint, pybullet.VELOCITY_CONTROL, targetVelocity=-1, force=10)
self.activated = False
# If activated and object in gripper: check object contact.
# If activated and nothing in gripper: check gripper contact.
# If released: check proximity to surface (disabled).
def detect_contact(self):
obj, _, ray_frac = self.check_proximity()
if self.activated:
empty = self.grasp_width() < 0.01
cbody = self.body if empty else obj
if obj == self.body or obj == 0:
return False
return self.external_contact(cbody)
# else:
# return ray_frac < 0.14 or self.external_contact()
# Return if body is in contact with something other than gripper
def external_contact(self, body=None):
if body is None:
body = self.body
pts = pybullet.getContactPoints(bodyA=body)
pts = [pt for pt in pts if pt[2] != self.body]
return len(pts) > 0 # pylint: disable=g-explicit-length-test
def check_grasp(self):
while self.moving():
time.sleep(0.001)
success = self.grasp_width() > 0.01
return success
def grasp_width(self):
lpad = np.array(pybullet.getLinkState(self.body, 4)[0])
rpad = np.array(pybullet.getLinkState(self.body, 9)[0])
dist = np.linalg.norm(lpad - rpad) - 0.047813
return dist
def check_proximity(self):
ee_pos = np.array(pybullet.getLinkState(self.robot, self.tool)[0])
tool_pos = np.array(pybullet.getLinkState(self.body, 0)[0])
vec = (tool_pos - ee_pos) / np.linalg.norm((tool_pos - ee_pos))
ee_targ = ee_pos + vec
ray_data = pybullet.rayTest(ee_pos, ee_targ)[0]
obj, link, ray_frac = ray_data[0], ray_data[1], ray_data[2]
return obj, link, ray_frac