pr2_reactive_grippers.py
Go to the documentation of this file.
00001 #/usr/bin/python
00002 
00003 import roslib; roslib.load_manifest('assistive_teleop')
00004 import rospy
00005 
00006 import actionlib
00007 from pr2_controllers_msgs.msg import (Pr2GripperCommandGoal, Pr2GripperCommand,
00008                                       Pr2GripperCommandAction)
00009 from pr2_gripper_sensor_msgs.msg import (
00010     PR2GripperGrabAction, PR2GripperGrabGoal, PR2GripperGrabCommand,
00011     PR2GripperReleaseAction, PR2GripperReleaseGoal, PR2GripperReleaseCommand,
00012     PR2GripperEventDetectorCommand
00013     )
00014 
00015 TEST = True
00016 
00017 class PR2Gripper():
00018     def __init__(self, arm):
00019         #TODO: Adapt smoothly to changing gripper controller
00020         self.arm = arm
00021 
00022         ####ACTION CLIENTS####
00023         ##Grab action client
00024         self.grab_ac = actionlib.SimpleActionClient(
00025                                 self.arm[0]+'_gripper_sensor_controller/grab',
00026                                 PR2GripperGrabAction)
00027         rospy.loginfo("Waiting for "+self.arm+" gripper grab server")
00028         if self.grab_ac.wait_for_server(rospy.Duration(1)):
00029             rospy.loginfo("Found " + self.arm + " gripper grab  server")
00030         else:
00031             rospy.logwarn("Cannot find " + self.arm + " gripper grab server")
00032             self.grab_ac = False #flag to use default controller
00033        
00034         ##Release action client
00035         self.release_ac = actionlib.SimpleActionClient(
00036                             self.arm[0]+'_gripper_sensor_controller/release',
00037                             PR2GripperReleaseAction)
00038         rospy.loginfo("Waiting for "+self.arm+" gripper release server")
00039         if self.release_ac.wait_for_server(rospy.Duration(1)):
00040             rospy.loginfo("Found "+self.arm+" gripper release server")
00041         else:
00042             rospy.logwarn("Cannot find "+self.arm+" gripper release server")
00043             self.release_ac = False #flag to use default controller
00044 
00045         ##Gripper action client
00046         self.gripper_action_ac = actionlib.SimpleActionClient(self.arm[0]+
00047                                 '_gripper_sensor_controller/gripper_action',
00048                                 Pr2GripperCommandAction)
00049         rospy.loginfo("Waiting for "+self.arm+" gripper action server")
00050         if self.gripper_action_ac.wait_for_server(rospy.Duration(1)):
00051             rospy.loginfo("Found "+self.arm+" gripper action server")
00052         else:
00053             rospy.logwarn("Cannot find "+self.arm+" gripper action server")
00054             self.gripper_action_ac = False # flag to use default controller
00055 
00056         if not (self.grab_ac and self.release_ac and self.gripper_action_ac):
00057             self.def_gripper_ac = actionlib.SimpleActionClient(
00058                              self.arm[0]+"_gripper_controller/gripper_action",
00059                              Pr2GripperCommandAction)
00060             if self.def_gripper_ac.wait_for_server(rospy.Duration(30)):
00061                 rospy.loginfo("Found default "+self.arm+"_gripper action")
00062             else:
00063                 rospy.logwarn("Cannot find default"+self.arm+"_gripper action")
00064                 rospy.logwarn(self.arm+" GRIPPER ACTIONS NOT FOUND")
00065 
00066     
00067     def grab(self, gain=0.03, blocking=False, block_timeout=20):
00068         print "Performing Gripper Grab"
00069         if self.grab_ac:
00070             grab_goal = PR2GripperGrabGoal(PR2GripperGrabCommand(gain))
00071             self.grab_ac.send_goal(grab_goal)
00072             if blocking:
00073                 return self.grab_ac.wait_for_result(
00074                                                 rospy.Duration(block_timeout))
00075         else:
00076             self.def_gripper_ac.send_goal(Pr2GripperCommand(0.0, -1.0))
00077             if blocking:
00078                 return self.def_gripper_ac.wait_for_result(
00079                                                 rospy.Duration(block_timeout))
00080 
00081     def release(self, event=0, acc_thresh=3, slip_thresh=0.005,
00082                 blocking=False, block_timeout=20):
00083         print "Performing Gripper Release"
00084         if self.release_ac:
00085             release_event=PR2GripperEventDetectorCommand()
00086             release_event.trigger_conditions = event
00087             release_event.acceleration_trigger_magnitude = acc_thresh
00088             release_event.slip_trigger_magnitude = slip_thresh
00089             release_command = PR2GripperReleaseCommand(release_event)
00090             self.release_ac.send_goal(PR2GripperReleaseGoal(release_command))
00091             if blocking:
00092                 return self.release_ac.wait_for_result(
00093                                                 rospy.Duration(block_timeout))
00094         else:
00095             self.def_gripper_ac.send_goal(Pr2GripperCommandGoal(
00096                                             Pr2GripperCommand(0.09, -1.0)))
00097             if blocking:
00098                 return self.def_gripper_ac.wait_for_result(
00099                                                 rospy.Duration(block_timeout))
00100 
00101 
00102     def gripper_action(self, position, max_effort=-1, 
00103                         blocking=False, block_timeout=20.0):
00104         print "Performing Gripper Action"
00105         if self.gripper_action_ac:    
00106             command = Pr2GripperCommand(position, max_effort)
00107             goal = Pr2GripperCommandGoal(command)
00108             self.gripper_action_ac.send_goal(goal)
00109             if blocking:
00110                 return self.gripper_action_ac.wait_for_result(
00111                                                 rospy.Duration(block_timeout))
00112         else:
00113             self.def_gripper_ac.send_goal(Pr2GripperCommand(position,
00114                                                             max_effort))
00115             if blocking:
00116                 return self.def_gripper_ac.wait_for_result(
00117                                                 rospy.Duration(block_timeout))
00118         
00119 if __name__=='__main__':
00120     rospy.init_node('gripper_sensor_intermediary')
00121     if TEST:
00122         gripper = PR2Gripper('right')
00123         print "Initialized!"
00124         print "Attempting to close to 0.01"
00125         gripper.gripper_action(0.01,blocking=True)
00126         print "Done closing, performing grab"
00127         gripper.grab(blocking=True, block_timeout=2)
00128         print "Done Grabbing, performing close to 0.005"
00129         gripper.gripper_action(0.005,blocking=True)
00130         print "Done closing, performing release"
00131         gripper.release(blocking=True)
00132         print "Done Realeasing: Test Complete"
00133 
00134     while not rospy.is_shutdown():
00135         rospy.spin()


assistive_teleop
Author(s): Phillip M. Grice, Advisor: Prof. Charlie Kemp, Lab: The Healthcare Robotoics Lab at Georgia Tech.
autogenerated on Wed Nov 27 2013 11:35:34