Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import roslib
00035 roslib.load_manifest('pr2_delivery')
00036
00037 import rospy
00038
00039 from trajectory_msgs.msg import *
00040 from actionlib_msgs.msg import *
00041 from pr2_controllers_msgs.msg import *
00042 from sensor_msgs.msg import JointState
00043 import actionlib
00044
00045 class Joint:
00046 def __init__(self, name, position, velocity, effort):
00047 self.name = name
00048 self.position = position
00049 self.velocity = velocity
00050 self.effort = effort
00051
00052 class ArmMover:
00053 def __init__(self):
00054 self.joint_names = ["shoulder_pan",
00055 "shoulder_lift",
00056 "upper_arm_roll",
00057 "elbow_flex",
00058 "forearm_roll",
00059 "wrist_flex",
00060 "wrist_roll" ]
00061
00062
00063 self.r_action_name = 'r_arm_controller/joint_trajectory_action'
00064 self.l_action_name = 'l_arm_controller/joint_trajectory_action'
00065 self.left_joint_client = actionlib.SimpleActionClient(self.l_action_name, JointTrajectoryAction)
00066 self.right_joint_client = actionlib.SimpleActionClient(self.r_action_name, JointTrajectoryAction)
00067
00068
00069 rospy.loginfo("Waiting for " + self.l_action_name)
00070 if not self.left_joint_client.wait_for_server(rospy.Duration(30)):
00071 rospy.logerr("ArmMover.py: left_joint_client action server did not come up within timelimit")
00072 rospy.loginfo("Waiting for " + self.r_action_name)
00073 if not self.right_joint_client.wait_for_server(rospy.Duration(30)):
00074 rospy.logerr("ArmMover.py: right_joint_client action server did not come up within timelimit")
00075
00076 self.r_gripper_action_name = 'r_gripper_controller/gripper_action'
00077 self.right_gripper_client = actionlib.SimpleActionClient(self.r_gripper_action_name, Pr2GripperCommandAction)
00078
00079 if not self.right_gripper_client.wait_for_server(rospy.Duration(30)):
00080 rospy.logerr("ArmMover.py: right_gripper_client action server did not come up within timelimit")
00081
00082 self.joints = {}
00083 rospy.Subscriber('/joint_states', JointState, self.joint_states_callback)
00084
00085 def joint_states_callback(self, msg):
00086
00087
00088
00089
00090 i = 0
00091 for name in msg.name:
00092 state = Joint(name, msg.position[i], msg.velocity[i], msg.effort[i])
00093 self.joints[name] = state
00094 i += 1
00095
00096 def go(self, side, joint_values, duration):
00097 """side is 'l' or 'r'.
00098 joint_values is an array of 7 joint values."""
00099 goal = JointTrajectoryGoal()
00100 goal.trajectory.joint_names = [side+"_"+name+"_joint" for name in self.joint_names]
00101 goal.trajectory.points = []
00102 goal.trajectory.points.append(JointTrajectoryPoint( positions = joint_values,
00103 velocities = [],
00104 accelerations = [],
00105 time_from_start = rospy.Duration(duration)))
00106 goal.trajectory.header.stamp = rospy.get_rostime() + rospy.Duration(0.01)
00107 client = {'l': self.left_joint_client, 'r': self.right_joint_client}[side]
00108 return client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00109
00110 def open_right(self):
00111 return self.right_grip(0.08)
00112
00113 def close_right(self):
00114 return self.right_grip(0, 200.0)
00115
00116 def right_grip(self, position, max_effort = -1):
00117 goal = Pr2GripperCommandGoal()
00118 goal.command.position = position
00119 goal.command.max_effort = max_effort
00120 return self.right_gripper_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00121
00122 def print_arm_pose(self, which_arm):
00123 joint_pose = []
00124 for joint in self.joint_names:
00125 full_name = which_arm + "_" + joint + "_joint"
00126 joint_pose.append( self.joints[full_name].position )
00127 print(which_arm, "arm pose", joint_pose)