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 import roslib; roslib.load_manifest('rosie_pr2_gripper_action')
00023 import rospy
00024 
00025 from actionlib_msgs.msg import *
00026 from pr2_controllers_msgs.msg import *
00027 import actionlib
00028 import cogman_msgs.msg
00029 
00030 
00031 
00032 class RosiePR2GripperActionServer:
00033     def __init__(self):
00034 
00035         self.threshold = 0.05
00036         rospy.loginfo('open/close threshold = %5.3f' % (self.threshold))
00037         
00038         self.l_action_server = actionlib.simple_action_server.SimpleActionServer('/l_gripper_controller/gripper_action',Pr2GripperCommandAction, self.l_executeCB)
00039         self.r_action_server = actionlib.simple_action_server.SimpleActionServer('/r_gripper_controller/gripper_action',Pr2GripperCommandAction, self.r_executeCB)
00040 
00041         self.l_action_client = actionlib.SimpleActionClient('left_arm', cogman_msgs.msg.ArmHandAction)
00042         self.r_action_client = actionlib.SimpleActionClient('right_arm', cogman_msgs.msg.ArmHandAction)
00043 
00044         self.l_action_client.wait_for_server(rospy.Duration(20.0))
00045         self.r_action_client.wait_for_server(rospy.Duration(20.0))
00046         
00047         
00048         self.hand_primitive(self.l_action_client,'open_relax')
00049         self.hand_primitive(self.r_action_client,'open_relax')
00050         
00051         
00052     def r_executeCB(self, goal):
00053         self.executeCB(goal, 'right')
00054         
00055     def l_executeCB(self, goal):
00056         self.executeCB(goal, 'left')
00057         
00058     def executeCB(self, goal, side='right'):
00059         if side == 'left':
00060             client = self.l_action_client
00061             action_server = self.l_action_server
00062         elif side =='right':
00063             client = self.r_action_client
00064             action_server = self.r_action_server
00065         else:
00066             rospy.logerr('Wrong side specified, expected left/right')
00067             return(None)
00068 
00069         
00070         if goal.command.position < self.threshold:
00071             self.hand_primitive(client, '3pinch')
00072         else:
00073             self.hand_primitive(client, 'open_relax')
00074         
00075         client.wait_for_result(rospy.Duration(20.0))  
00076             
00077         result = Pr2GripperCommandResult()
00078         
00079         
00080         
00081         
00082         
00083         self.success = True  
00084         if self.success:
00085             result.position = goal.command.position  
00086             result.effort = 0.5  
00087             result.stalled = False
00088             result.reached_goal = True
00089             action_server.set_succeeded(result)
00090         else:
00091             result.reached_goal = False
00092             rospy.logerr("PR2 gripper action FAILED")
00093             self.action_server.set_aborted(result)
00094     
00095     def hand_primitive(self, client, primitive_name):
00096         goal = cogman_msgs.msg.ArmHandGoal()
00097         goal.command = 'hand_primitive'
00098         goal.hand_primitive = primitive_name
00099         client.send_goal(goal)
00100         
00101         
00102 def main():
00103     rospy.loginfo("rosie_pr2_gripper_action: Starting up!")
00104     rospy.init_node('rosie_pr2_gripper_action')
00105     tuck_arms_action_server = RosiePR2GripperActionServer()
00106     rospy.spin()
00107 
00108 if __name__ == '__main__':
00109     main()