00001
00002
00003 import time
00004
00005 import roslib
00006 roslib.load_manifest('cob_script_server')
00007 import rospy
00008
00009 from simple_script_server import script
00010 from kinematics_msgs.srv import *
00011
00012 import tf
00013 from tf.transformations import *
00014 from geometry_msgs.msg import *
00015
00016 class GraspScript(script):
00017
00018 def init_ik_interface(self):
00019 rospy.wait_for_service('/arm_controller/get_ik')
00020 rospy.wait_for_service('/arm_controller/get_fk_tcp')
00021 self.iks = rospy.ServiceProxy('/arm_controller/get_ik', GetPositionIK)
00022 self.fks = rospy.ServiceProxy('/arm_controller/get_fk_tcp', GetPositionFK)
00023
00024
00025 def callIKSolver(self, current_pose, goal_pose):
00026 req = GetPositionIKRequest()
00027 req.ik_request.ik_seed_state.joint_state.position = current_pose
00028 req.ik_request.pose_stamped.pose = goal_pose
00029 resp = self.iks(req)
00030 return (resp.solution.joint_state.position, resp.error_code)
00031
00032 def getEmptyPose(self):
00033 relpos = PoseStamped()
00034 relpos.pose.position.x = 0.0
00035 relpos.pose.position.y = 0.0
00036 relpos.pose.position.z = 0.0
00037 relpos.pose.orientation.x = 0.0
00038 relpos.pose.orientation.y = 0.0
00039 relpos.pose.orientation.z = 0.0
00040 relpos.pose.orientation.w = 1.0
00041 return relpos
00042
00043 def Initialize(self):
00044 self.init_ik_interface()
00045
00046
00047
00048
00049
00050
00051
00052 def calcRelIK(self, start, goalpose, goalrotation = [0,0,0]):
00053 result = []
00054 print "Calling IK Server 1"
00055 q = quaternion_from_euler(goalrotation[0], goalrotation[1], goalrotation[2])
00056
00057 req = GetPositionFKRequest()
00058 req.robot_state.joint_state.position = start
00059 resp = self.fks(req)
00060 abspos = resp.pose_stamped[0]
00061 abspos.pose.position.x = abspos.pose.position.x + goalpose[0]
00062 abspos.pose.position.y = abspos.pose.position.y + goalpose[1]
00063 abspos.pose.position.z = abspos.pose.position.z + goalpose[2]
00064 qrel = tf.transformations.quaternion_multiply([abspos.pose.orientation.x, abspos.pose.orientation.y, abspos.pose.orientation.z, abspos.pose.orientation.w], [q[0], q[1], q[2], q[3]])
00065
00066 abspos.pose.orientation.x = qrel[0]
00067 abspos.pose.orientation.y = qrel[1]
00068 abspos.pose.orientation.z = qrel[2]
00069 abspos.pose.orientation.w = qrel[3]
00070 (out, error_code) = self.callIKSolver(start, abspos.pose)
00071 for o in out:
00072 result.append(o)
00073 return (result, error_code)
00074
00075
00076 def Run(self):
00077 if(not self.sss.parse):
00078 self.listener = tf.TransformListener()
00079 time.sleep(0.5)
00080 startposition = [0.32801351164082243, -1.2319244977553321, -0.020446793812738041, 1.7740684566996034, 0.44454129082455984, 1.5863009631773928, 0.66069169492063817]
00081
00082
00083 (grasp_empty_cup_position, error_code) = self.calcRelIK(startposition, [-0.1,0.0,0.0])
00084 if(error_code.val == error_code.SUCCESS):
00085 print grasp_empty_cup_position
00086 else:
00087 print "Ik 1 Failed"
00088 (grasp_cup_out_of_cooler_position, error_code) = self.calcRelIK(grasp_empty_cup_position, [0.1,0.0,0,1])
00089 if(error_code.val == error_code.SUCCESS):
00090 print grasp_empty_cup_position
00091 else:
00092 print "Ik 2 Failed"
00093
00094 self.sss.move("arm", [startposition])
00095 self.sss.move("sdh", [[0.0,1.0472,0.0,0.0,1.0472,0.0,1.0472]])
00096 self.sss.move("arm", [grasp_empty_cup_position])
00097 self.sss.move("arm", [grasp_cup_out_of_cooler_position])
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 if __name__ == "__main__":
00153 SCRIPT = GraspScript()
00154 SCRIPT.Start()