00001
00002 import roslib; roslib.load_manifest('cob_manipulator')
00003 import tf
00004 from kinematics_msgs.srv import *
00005 from sensor_msgs.msg import JointState
00006 import threading
00007 import rospy
00008 import time
00009 import actionlib
00010 from pr2_controllers_msgs.msg import *
00011 from cob_srvs.srv import *
00012 from trajectory_msgs.msg import *
00013 from geometry_msgs.msg import *
00014
00015 class move_cart:
00016
00017 def __init__(self):
00018 self.configuration = [0,0,0,0,0,0,0]
00019 self.lock = threading.Lock()
00020 self.received_state = False
00021 self.listener = tf.TransformListener()
00022 time.sleep(0.5)
00023 self.client = actionlib.SimpleActionClient('joint_trajectory_action', JointTrajectoryAction)
00024 self.as_ = actionlib.SimpleActionServer("move_cart", MoveCartAction, execute_cb=self.cbMoveCart)
00025 if not self.client.wait_for_server(rospy.Duration(15)):
00026 rospy.logerr("arm action server not ready within timeout, aborting...")
00027 return
00028 else:
00029 rospy.logdebug("arm action server ready")
00030
00031 rospy.Subscriber('/joint_states', JointState, self.joint_states_callback)
00032 self.thread = threading.Thread(target=self.joint_states_listener)
00033 self.thread.start()
00034 rospy.wait_for_service('get_ik')
00035 self.iks = rospy.ServiceProxy('get_ik', GetPositionIK)
00036 rospy.loginfo("move cart interface is ready")
00037
00038
00039 def joint_states_listener(self):
00040 rospy.Subscriber('/joint_states', JointState, self.joint_states_callback)
00041 rospy.spin()
00042
00043
00044
00045 def joint_states_callback(self, msg):
00046 self.lock.acquire()
00047 for k in range(7):
00048 joint_name = "arm_" + str(k+1) + "_joint"
00049 for i in range(len(msg.name)):
00050 if(msg.name[i] == joint_name):
00051 self.configuration[k] = msg.position[i]
00052 self.name = msg.name
00053 self.position = msg.position
00054 self.velocity = msg.velocity
00055 self.effort = msg.effort
00056 self.received_state = True
00057 self.lock.release()
00058
00059 def cbMoveCart(self,msg):
00060 result = MoveCartResult()
00061 feedback = MoveCartFeedback()
00062
00063 self.listener.waitForTransform("/arm_0_link",msg.goal_pose.header.frame_id,rospy.Time(),rospy.Duration(5))
00064 print "Calling IK Server"
00065 (new_config, error_code) = self.callIKSolver(msg.goal_pose)
00066 if(error_code.val == error_code.SUCCESS):
00067 self.moveArm(new_config)
00068 result.return_value = 0
00069 self.as_.set_succeeded(result)
00070 else:
00071 result.return_value = 1
00072 rospy.logerr("no ik solution found")
00073 self.as_.set_aborted(result);
00074
00075 def moveArm(self, positions):
00076 goal = JointTrajectoryGoal()
00077 goalp = JointTrajectory()
00078 goalp.joint_names = ["arm_1_joint","arm_2_joint","arm_3_joint","arm_4_joint","arm_5_joint","arm_6_joint","arm_7_joint"]
00079 point=JointTrajectoryPoint()
00080 point.positions=positions
00081 point.time_from_start=rospy.Duration(3)
00082 goalp.points.append(point)
00083 goal.trajectory = goalp
00084 goal.trajectory.header.stamp = rospy.Time.now()
00085 self.client.send_goal(goal)
00086 self.client.wait_for_result()
00087
00088 def callIKSolver(self, goal_pose_stamped):
00089 while(not self.received_state):
00090 time.sleep(0.1)
00091 req = GetPositionIKRequest()
00092 req.ik_request.ik_seed_state.joint_state.name = ["arm_%d_joint" % (i+1) for i in range(7)]
00093 req.ik_request.ik_seed_state.joint_state.position = self.configuration
00094 req.ik_request.pose_stamped = goal_pose_stamped
00095 req.timeout = rospy.Duration(5)
00096 resp = self.iks(req)
00097 print resp.error_code
00098 return (resp.solution.joint_state.position, resp.error_code)
00099
00100 if __name__ == "__main__":
00101 rospy.init_node('move_cart')
00102 time.sleep(0.5)
00103 mc = move_cart()
00104 rospy.spin()