ArmMover.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2013, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the Willow Garage nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
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         # Get controller name and start joint trajectory action clients
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         # Wait for joint clients to connect with timeout
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         # string[] name
00087         # float64[] position
00088         # float64[] velocity
00089         # float64[] effort
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)


pr2_delivery
Author(s): Dave Hershberger, Maintainer=Devon Ash
autogenerated on Sat Jun 8 2019 19:56:27