Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('rcommander_core')
00003 import rospy
00004 import actionlib
00005 import pr2_interactive_manipulation.msg as pim
00006 import graph_model as gm
00007 import sm_thread_runner as smtr
00008 import point_tool as pt
00009 import pr2_utils as pu
00010 import tf
00011
00012 class PoseStampedScriptedActionServer:
00013
00014 def __init__(self, action_name, path_to_action):
00015 rospy.loginfo('Starting server for %s with path %s' %(action_name, path_to_action))
00016 self.tf_listener = tf.TransformListener()
00017 self.pr2 = pu.PR2(self.tf_listener)
00018
00019
00020 self._action_name = action_name
00021 self._as = actionlib.SimpleActionServer(self._action_name, pim.PoseStampedScriptedAction, execute_cb=self.execute_cb, auto_start=False)
00022 self._as.start()
00023
00024 rospy.loginfo('%s server up!' % action_name)
00025
00026 def setup_sm(self):
00027
00028 self.graph_model = gm.GraphModel.load(path_to_action, self.pr2)
00029
00030
00031
00032
00033
00034 self.point_field_name = None
00035 for node_name in self.graph_model.global_nodes(None):
00036 node = self.graph_model.get_smach_state(node_name)
00037 if node.__class__ == pt.Point3DState:
00038 self.point_field_name = node_name
00039 if self.point_field_name == None:
00040 raise RuntimeError('This statemachine doesn\'t have any nodes of type Point3DState')
00041
00042 def execute_cb(self, goal):
00043 self.setup_sm()
00044 r = rospy.Rate(30)
00045 position = [goal.pose_stamped.pose.position.x,
00046 goal.pose_stamped.pose.position.y,
00047 goal.pose_stamped.pose.position.z]
00048 frame = goal.pose_stamped.header.frame_id
00049 print 'Position', position, 'frame', frame
00050
00051 self.graph_model.get_smach_state(self.point_field_name).set_info((position, frame))
00052 state_machine = self.graph_model.create_state_machine()
00053 rthread = smtr.ThreadRunSM(self._action_name, state_machine)
00054 rthread.start()
00055
00056
00057 while True:
00058 if self._as.is_preempt_requested():
00059 self._as.set_preempted()
00060 success = False
00061 break
00062
00063 if rthread.exception:
00064 raise rthread.exception
00065
00066 if rthread.outcome != None:
00067 success = True
00068 break
00069
00070 if not rthread.isAlive():
00071 raise RuntimeError("Thread died unexpectedly.")
00072
00073 r.sleep()
00074
00075 if success:
00076 state_machine_output = rthread.outcome
00077 result = pim.PoseStampedScriptedResult(state_machine_output)
00078 rospy.loginfo("%s: Succeeded" % self._action_name)
00079 self._as.set_succeeded(result)
00080 else:
00081 self._as.set_aborted()
00082
00083
00084 if __name__ == '__main__':
00085 import sys
00086 action_name = sys.argv[1]
00087 path_to_action = sys.argv[2]
00088 rospy.init_node(action_name)
00089 action_server = PoseStampedScriptedActionServer(action_name, path_to_action)
00090 rospy.spin()
00091
00092