DeliverServer.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2013, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 PACKAGE='pr2_delivery'
00034 
00035 import roslib; roslib.load_manifest(PACKAGE)
00036 import subprocess
00037 import rospy
00038 import actionlib
00039 import geometry_msgs.msg
00040 import pr2_common_action_msgs.msg
00041 from pr2_delivery.msg import DeliverAction, DeliverGoal
00042 from ArmMover import ArmMover
00043 from pr2_gripper_sensor_msgs.msg import PR2GripperEventDetectorAction, PR2GripperEventDetectorGoal
00044 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
00045 
00046 class DeliverServer:
00047 
00048     tucked_with_object_pose = [-0.0175476818422744, 1.1720448201611564, -1.3268105758514066, -1.288722079574422, -31.28968470078213, -2.0089650532319836, -5.841424529413016]
00049     tuck_approach_pose = [0.039, 1.1072, 0.0, -2.067, -1.231, -1.998, 0.369]
00050     accept_object_pose = [ -0.07666010001780543,   0.1622352230632809,   -0.31320771836735584,   -1.374860652847621,   -3.1324415863359545,   -1.078194355846691,   1.857217828689617]
00051     tucked_with_object_approach_pose = [-0.01829384139848722, 0.6712428753827848, -1.3264898661986668, -0.6078654239376914, 0.601472182148825, -1.3278329090728338, -5.83346239703479]
00052 
00053     def __init__(self):
00054         self.tuck_arm_client = actionlib.SimpleActionClient("tuck_arms", pr2_common_action_msgs.msg.TuckArmsAction)
00055         self.gripper_wiggle_detector_client = actionlib.SimpleActionClient('r_gripper_sensor_controller/event_detector', PR2GripperEventDetectorAction)
00056         self.move_base_client = actionlib.SimpleActionClient('move_base', MoveBaseAction)
00057 
00058         # Load phrases
00059         self.lang = rospy.get_param('~lang', 'en')
00060         
00061         self.request_item_phrase = rospy.get_param('~request_item_phrase', 'Please give me the delivery.')
00062         self.item_received_phrase = rospy.get_param('~item_received_phrase', 'Thank you. I will deliver this.')
00063         self.give_item_phrase = rospy.get_param('~give_item_phrase', 'I have a delivery for you.')
00064         self.item_delivered_phrase = rospy.get_param('~item_delivered_phrase', 'Thank you. Have a nice day.')
00065 
00066         self.arm_mover = ArmMover()
00067 
00068         rospy.loginfo("Waiting for tuck_arms action server")
00069         self.tuck_arm_client.wait_for_server(rospy.Duration(10.0))
00070         rospy.loginfo("Waiting for wiggle_detector action server")
00071         self.gripper_wiggle_detector_client.wait_for_server(rospy.Duration(10.0))
00072         rospy.loginfo("Waiting for move_base action server")
00073         self.move_base_client.wait_for_server(rospy.Duration(10.0))
00074 
00075         self.server = actionlib.SimpleActionServer('deliver', DeliverAction, self.execute, False)
00076         self.server.start()
00077         
00078         rospy.loginfo("Ready")
00079 
00080     def execute(self, goal):
00081         """This is the main sequence of the delivery action."""
00082         self.tuck_arms()
00083         self.navigate_to(goal.get_object_pose)
00084         self.say(self.request_item_phrase)
00085         self.get_object()
00086         self.say(self.item_received_phrase)
00087         self.navigate_to(goal.give_object_pose)
00088         self.say(self.give_item_phrase)
00089         self.give_object()
00090         self.say(self.item_delivered_phrase)
00091         self.tuck_arms()
00092         self.navigate_to(goal.return_home_pose)
00093 
00094         self.server.set_succeeded()
00095 
00096     def say(self, thing_to_say):
00097         #rospy.loginfo("saying '%s'" % thing_to_say)
00098         # say something
00099         speed = 130
00100         subprocess.call([ 'espeak', 
00101                         "-v", self.lang,
00102                         "-s", "%d"%speed,
00103                         thing_to_say ])
00104 
00105     def tuck_arms(self):
00106         rospy.loginfo("tucking arms")
00107         goal = pr2_common_action_msgs.msg.TuckArmsGoal()
00108         goal.tuck_left = True
00109         goal.tuck_right = True
00110         self.tuck_arm_client.send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0))
00111 
00112     def navigate_to(self, nav_goal_pose):
00113         rospy.loginfo("navigating to %f %f" % (nav_goal_pose.pose.position.x, 
00114                       nav_goal_pose.pose.position.y))
00115         goal = MoveBaseGoal()
00116         goal.target_pose = nav_goal_pose
00117         self.move_base_client.send_goal_and_wait(goal, rospy.Duration(60*5), rospy.Duration(5))
00118 
00119     def wait_for_gripper_wiggle(self, accel):
00120         """Waits for one year.  accel is in m/s^2, normal values are 6 and 10"""
00121         # contents of function ported from pr2_props
00122         goal = PR2GripperEventDetectorGoal()
00123         goal.command.trigger_conditions = 4 # use just acceleration as our contact signal
00124         goal.command.acceleration_trigger_magnitude = accel
00125         goal.command.slip_trigger_magnitude = 0.008 # slip gain
00126         self.gripper_wiggle_detector_client.send_goal_and_wait(goal, rospy.Duration(3600*24*365), rospy.Duration(5.0))
00127 
00128     def get_object(self):
00129         rospy.loginfo("getting object")
00130         # - move right arm to accept-object pose
00131         self.arm_mover.go('r', self.tuck_approach_pose, 1)
00132         self.arm_mover.go('r', self.accept_object_pose, 2)
00133 
00134         object_gripped = False
00135         while not object_gripped:
00136             # - open gripper all the way
00137             self.arm_mover.open_right()
00138             rospy.sleep(2)
00139             # - wait for externally-applied hand motion detected (ala "fist-pump" demo)
00140             self.wait_for_gripper_wiggle(10) # m/s^2
00141             # - close gripper all the way
00142             self.arm_mover.close_right()
00143             # - if gripper closes all the way, no object is gripped
00144             gripper_pos = self.arm_mover.joints['r_gripper_joint'].position
00145             if gripper_pos > 0.02:
00146                 rospy.loginfo("gripper has object: %f", gripper_pos)
00147                 object_gripped = True
00148             else:
00149                 rospy.loginfo("gripper does not have object: %f", gripper_pos)
00150 
00151         self.arm_mover.go('r', self.tucked_with_object_approach_pose, 2)
00152         self.arm_mover.go('r', self.tucked_with_object_pose, 1)
00153 
00154     def give_object(self):
00155         rospy.loginfo("giving object")
00156         # move out to tucked-with-object approach pose for right arm
00157         self.arm_mover.go('r', self.tucked_with_object_approach_pose, 2)
00158         # - move right arm to give-object pose
00159         self.arm_mover.go('r', self.accept_object_pose, 2) # give-object pose is the same as accept-object pose.
00160         # - let arm motion settle
00161         rospy.sleep(2) 
00162         # - wait for externally-applied hand motion detected (ala "fist-pump" demo)
00163         self.wait_for_gripper_wiggle(5) # m/s^2
00164         # - open gripper
00165         self.arm_mover.open_right()
00166         rospy.sleep(1) 
00167 
00168 # Question: what if sequence is pre-empted while robot is holding object?
00169 # - maybe set the object on the floor?
00170 # - v1 is certainly just "do nothing" so robot is still holding the object.


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