pr2_arm_controller2.py
Go to the documentation of this file.
00001 #######################################################################
00002 #
00003 #   USE pr2_object_manipulation/pr2_gripper_reactive_approach/controller_manager.py
00004 #   That code has much of the ideas at the bottom, with more.
00005 #
00006 #######################################################################
00007 
00008 
00009 
00010 
00011 
00012 
00013 # TODO Update code to throw points one at a time.  Sections are labled: "Hack"
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 # Class for simple management of the arms and grippers.
00049 # Provides functionality for moving the arms, opening and closing
00050 # the grippers, performing IK, and other functionality as it is
00051 # developed.
00052 class PR2Arms(object):
00053 
00054     ##
00055     # Initializes all of the servers, clients, and variables
00056     #
00057     # @param send_delay send trajectory points send_delay nanoseconds into the future
00058     # @param gripper_point given the frame of the wrist_roll_link, this point offsets
00059     #                      the location used in FK and IK, preferably to the tip of the
00060     #                      gripper
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     # set a cep using the Jacobian Transpose controller.
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     # rotational interpolation unimplemented.
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 # TODO Evaluate gripper functions and parameters
00170 
00171     ##
00172     # Move the gripper the given amount with given amount of effort
00173     #
00174     # @param arm 0 for right, 1 for left
00175     # @param amount the amount the gripper should be opened
00176     # @param effort - supposed to be in Newtons. (-ve number => max effort)
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     # Open the gripper
00182     #
00183     # @param arm 0 for right, 1 for left
00184     def open_gripper(self, arm):
00185         self.move_gripper(arm, 0.08, -1)
00186 
00187     ##
00188     # Close the gripper
00189     #
00190     # @param arm 0 for right, 1 for left
00191     def close_gripper(self, arm, effort = 15):
00192         self.move_gripper(arm, 0.0, effort)
00193 
00194     # def get_wrist_force(self, arm):
00195     #     pass
00196 
00197     ######################################################
00198     # More specific functionality
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 #    #------- testing set JEP ---------------
00208 #    raw_input('Hit ENTER to begin')
00209     r_arm, l_arm = 0, 1
00210 #    cep_p, cep_rot = pr2_arm.get_cep_jtt(r_arm)
00211 #    print 'cep_p:', cep_p.A1
00212 #
00213 #    for i in range(5):
00214 #        cep_p[0,0] += 0.01
00215 #        raw_input('Hit ENTER to move')
00216 #        pr2_arm.set_cep_jtt(r_arm, cep_p)
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     #rospy.sleep(10)
00224     #pr2_arm.close_gripper(r_arm, effort = -1)
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 


pr2_playpen
Author(s): Marc Killpack / mkillpack3@gatech.edu, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 12:18:32