Robot.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 pr2_gripper_sensor_msgs.msg import *
00043 import actionlib
00044 
00045 # // Our Action interface type, provided as a typedef for convenience
00046 # typedef actionlib::SimpleActionClient<pr2_gripper_sensor_msgs::PR2GripperFindContactAction> ContactClient;
00047 # // Our Action interface type, provided as a typedef for convenience                   
00048 # typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient;
00049 # // Our Action interface type, provided as a typedef for convenience
00050 # typedef actionlib::SimpleActionClient<pr2_gripper_sensor_msgs::PR2GripperForceServoAction> ForceClient;
00051 
00052 
00053 class Robot:
00054     def __init__(self):
00055         self.joint_names = ["shoulder_pan", 
00056                             "shoulder_lift",
00057                             "upper_arm_roll",
00058                             "elbow_flex", 
00059                             "forearm_roll",
00060                             "wrist_flex", 
00061                             "wrist_roll" ]
00062 
00063         # Get controller name and start joint trajectory action clients
00064         self.left_joint_client = actionlib.SimpleActionClient('l_arm_controller/joint_trajectory_action', JointTrajectoryAction)
00065         self.right_joint_client = actionlib.SimpleActionClient('r_arm_controller/joint_trajectory_action', JointTrajectoryAction)
00066         self.right_gripper_client = actionlib.SimpleActionClient('r_gripper_sensor_controller/gripper_action', Pr2GripperCommandAction)
00067         self.right_contact_client = actionlib.SimpleActionClient('r_gripper_sensor_controller/find_contact', PR2GripperFindContactAction)
00068         self.right_force_client = actionlib.SimpleActionClient('r_gripper_sensor_controller/force_servo', PR2GripperForceServoAction)
00069 
00070         # Wait for joint clients to connect with timeout
00071         for client in [self.left_joint_client,
00072                        self.right_joint_client,
00073                        self.right_gripper_client,
00074                        self.right_contact_client,
00075                        self.right_force_client]:
00076             if not client.wait_for_server(rospy.Duration(30)):
00077                 rospy.logerr("Robot.py: '%s' action server did not come up within timelimit" % client.action_client.ns)
00078 
00079     def move_arm_to(self, side, joint_values, duration):
00080         """side is 'l' or 'r'.
00081            joint_values is an array of 7 joint values."""
00082         goal = JointTrajectoryGoal()
00083         goal.trajectory.joint_names = [side+"_"+name+"_joint" for name in self.joint_names]
00084         goal.trajectory.points = []
00085         goal.trajectory.points.append(JointTrajectoryPoint( positions = joint_values,
00086                                                             velocities = [],
00087                                                             accelerations = [],
00088                                                             time_from_start = rospy.Duration(duration)))
00089         goal.trajectory.header.stamp = rospy.get_rostime() + rospy.Duration(0.01)
00090         client = {'l': self.left_joint_client, 'r': self.right_joint_client}[side]
00091         return client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00092 
00093     def open_right_gripper(self)
00094         goal = Pr2GripperCommandGoal()
00095         goal.command.position = 0.08
00096         goal.command.max_effort = -1
00097         return self.right_gripper_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00098 
00099     def grab_with_right_gripper(self):
00100         """Close gripper until we sense contact on both fingers."""
00101         goal = PR2GripperFindContactGoal()
00102         goal.command.contact_conditions = goal.command.BOTH # close until both fingers contact
00103         goal.command.zero_fingertip_sensors = True # zero fingertip sensor values before moving
00104         return self.right_contact_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00105 
00106     def hold_with_right_gripper(self, hold_force = 10.0):
00107         """Hold somethign with a constant force (in Newtons) in the gripper"""
00108         goal = PR2GripperForceServoGoal()
00109         goal.command.fingertip_force = hold_force
00110         return self.right_force_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00111 
00112     def right_grip(self, position, max_effort = -1):
00113         goal = Pr2GripperCommandGoal()
00114         goal.command.position = position
00115         goal.command.max_effort = max_effort
00116         return self.right_gripper_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00117 
00118 
00119 int main(int argc, char** argv){
00120   ros::init(argc, argv, "simple_gripper");
00121 
00122   Gripper gripper;
00123 
00124   gripper.open();
00125   sleep(2.0);
00126 
00127   gripper.findTwoContacts();
00128   gripper.hold(10.0);   // hold with 10 N of force
00129 
00130   return 0;
00131 }


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