00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 import numpy as np, math
00016 from threading import RLock, Timer
00017 import sys
00018
00019 import roslib; roslib.load_manifest('hrl_pr2_lib')
00020 import tf
00021
00022 import rospy
00023
00024 import actionlib
00025 from actionlib_msgs.msg import GoalStatus
00026
00027 from pr2_controllers_msgs.msg import Pr2GripperCommandGoal, Pr2GripperCommandAction, Pr2GripperCommand
00028 from geometry_msgs.msg import PoseStamped
00029
00030 from teleop_controllers.msg import JTTeleopControllerState
00031
00032 from std_msgs.msg import Float64
00033 from sensor_msgs.msg import JointState
00034
00035 import hrl_lib.transforms as tr
00036 import time
00037
00038 import tf.transformations as tftrans
00039 import types
00040
00041
00042 node_name = "pr2_arms"
00043
00044 def log(str):
00045 rospy.loginfo(node_name + ": " + str)
00046
00047
00048
00049
00050
00051
00052 class PR2Arms(object):
00053
00054
00055
00056
00057
00058
00059
00060
00061 def __init__(self, send_delay=50000000, gripper_point=(0.23,0.0,0.0),
00062 force_torque = False):
00063 log("Loading PR2Arms")
00064
00065 self.send_delay = send_delay
00066 self.off_point = gripper_point
00067 self.gripper_action_client = [actionlib.SimpleActionClient('r_gripper_controller/gripper_action', Pr2GripperCommandAction),actionlib.SimpleActionClient('l_gripper_controller/gripper_action', Pr2GripperCommandAction)]
00068
00069 self.gripper_action_client[0].wait_for_server()
00070 self.gripper_action_client[1].wait_for_server()
00071
00072 self.arm_state_lock = [RLock(), RLock()]
00073 self.r_arm_cart_pub = rospy.Publisher('/r_cart/command_pose', PoseStamped)
00074 self.l_arm_cart_pub = rospy.Publisher('/l_cart/command_pose', PoseStamped)
00075
00076 rospy.Subscriber('/r_cart/state', JTTeleopControllerState, self.r_cart_state_cb)
00077 rospy.Subscriber('/l_cart/state', JTTeleopControllerState, self.l_cart_state_cb)
00078
00079 self.tf_lstnr = tf.TransformListener()
00080
00081 rospy.sleep(1.)
00082
00083 log("Finished loading SimpleArmManger")
00084
00085 def r_cart_state_cb(self, msg):
00086 trans, quat = self.tf_lstnr.lookupTransform('/torso_lift_link',
00087 'r_gripper_tool_frame', rospy.Time(0))
00088 rot = tr.quaternion_to_matrix(quat)
00089 tip = np.matrix([0.12, 0., 0.]).T
00090 self.r_ee_pos = rot*tip + np.matrix(trans).T
00091 self.r_ee_rot = rot
00092
00093 ros_pt = msg.x_desi_filtered.pose.position
00094 x, y, z = ros_pt.x, ros_pt.y, ros_pt.z
00095 self.r_cep_pos = np.matrix([x, y, z]).T
00096 pt = rot.T * (np.matrix([x,y,z]).T - np.matrix(trans).T)
00097 pt = pt + tip
00098 self.r_cep_pos_hooktip = rot*pt + np.matrix(trans).T
00099 ros_quat = msg.x_desi_filtered.pose.orientation
00100 quat = (ros_quat.x, ros_quat.y, ros_quat.z, ros_quat.w)
00101 self.r_cep_rot = tr.quaternion_to_matrix(quat)
00102
00103 def l_cart_state_cb(self, msg):
00104 ros_pt = msg.x_desi_filtered.pose.position
00105 self.l_cep_pos = np.matrix([ros_pt.x, ros_pt.y, ros_pt.z]).T
00106 ros_quat = msg.x_desi_filtered.pose.orientation
00107 quat = (ros_quat.x, ros_quat.y, ros_quat.z, ros_quat.w)
00108 self.l_cep_rot = tr.quaternion_to_matrix(quat)
00109
00110 def get_ee_jtt(self, arm):
00111 if arm == 0:
00112 return self.r_ee_pos, self.r_ee_rot
00113 else:
00114 return self.l_ee_pos, self.l_ee_rot
00115
00116 def get_cep_jtt(self, arm, hook_tip = False):
00117 if arm == 0:
00118 if hook_tip:
00119 return self.r_cep_pos_hooktip, self.r_cep_rot
00120 else:
00121 return self.r_cep_pos, self.r_cep_rot
00122 else:
00123 return self.l_cep_pos, self.l_cep_rot
00124
00125
00126 def set_cep_jtt(self, arm, p, rot=None):
00127 if arm != 1:
00128 arm = 0
00129 ps = PoseStamped()
00130 ps.header.stamp = rospy.rostime.get_rostime()
00131 ps.header.frame_id = 'torso_lift_link'
00132
00133 ps.pose.position.x = p[0,0]
00134 ps.pose.position.y = p[1,0]
00135 ps.pose.position.z = p[2,0]
00136
00137 if rot == None:
00138 if arm == 0:
00139 rot = self.r_cep_rot
00140 else:
00141 rot = self.l_cep_rot
00142
00143 quat = tr.matrix_to_quaternion(rot)
00144 ps.pose.orientation.x = quat[0]
00145 ps.pose.orientation.y = quat[1]
00146 ps.pose.orientation.z = quat[2]
00147 ps.pose.orientation.w = quat[3]
00148 if arm == 0:
00149 self.r_arm_cart_pub.publish(ps)
00150 else:
00151 self.l_arm_cart_pub.publish(ps)
00152
00153
00154 def go_cep_jtt(self, arm, p):
00155 step_size = 0.01
00156 sleep_time = 0.1
00157 cep_p, cep_rot = self.get_cep_jtt(arm)
00158 unit_vec = (p-cep_p)
00159 unit_vec = unit_vec / np.linalg.norm(unit_vec)
00160 while np.linalg.norm(p-cep_p) > step_size:
00161 cep_p += unit_vec * step_size
00162 self.set_cep_jtt(arm, cep_p)
00163 rospy.sleep(sleep_time)
00164 self.set_cep_jtt(arm, p)
00165 rospy.sleep(sleep_time)
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 def move_gripper(self, arm, amount=0.08, effort = 15):
00178 self.gripper_action_client[arm].send_goal(Pr2GripperCommandGoal(Pr2GripperCommand(position=amount, max_effort = effort)))
00179
00180
00181
00182
00183
00184 def open_gripper(self, arm):
00185 self.move_gripper(arm, 0.08, -1)
00186
00187
00188
00189
00190
00191 def close_gripper(self, arm, effort = 15):
00192 self.move_gripper(arm, 0.0, effort)
00193
00194
00195
00196
00197
00198
00199
00200
00201 if __name__ == '__main__':
00202 rospy.init_node(node_name, anonymous = True)
00203 log("Node initialized")
00204
00205 pr2_arm = PR2Arms()
00206
00207
00208
00209 r_arm, l_arm = 0, 1
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 raw_input('Hit ENTER to move')
00220 p1 = np.matrix([0.62, 0.0, 0.16]).T
00221 pr2_arm.go_cep_jtt(r_arm, p1)
00222
00223
00224
00225 raw_input('Hit ENTER to move')
00226 p2 = np.matrix([0.600+0.06, 0.106, -0.32]).T
00227 pr2_arm.go_cep_jtt(r_arm, p2)
00228
00229 raw_input('Hit ENTER to move')
00230 pr2_arm.go_cep_jtt(r_arm, p1)
00231
00232 raw_input('Hit ENTER to go home')
00233 home = np.matrix([0.23, -0.6, -0.05]).T
00234 pr2_arm.go_cep_jtt(r_arm, home)
00235
00236