action_example.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 
00004 import rospy
00005 
00006 import actionlib
00007 
00008 import behavior_tree_core.msg
00009 
00010 
00011 
00012 
00013 class BTAction(object):
00014   # create messages that are used to publish feedback/result
00015   _feedback = behavior_tree_core.msg.BTFeedback()
00016   _result   = behavior_tree_core.msg.BTResult()
00017 
00018   def __init__(self, name):
00019     self._action_name = name
00020     self._as = actionlib.SimpleActionServer(self._action_name, behavior_tree_core.msg.BTAction, execute_cb=self.execute_cb, auto_start = False)
00021     self._as.start()
00022     
00023   def execute_cb(self, goal):
00024     # helper variables
00025     r = rospy.Rate(1)
00026     success = True
00027     
00028 
00029     
00030     # publish info to the console for the user
00031     rospy.loginfo('Starting Action')
00032     
00033     # start executing the action
00034     for i in xrange(1, 5):
00035       # check that preempt has not been requested by the client
00036       if self._as.is_preempt_requested():
00037         rospy.loginfo('Action Halted')
00038         self._as.set_preempted()
00039         success = False
00040         break
00041 
00042       rospy.loginfo('Executing Action')
00043 
00044       # this step is not necessary, the sequence is computed at 1 Hz for demonstration purposes
00045       r.sleep()
00046       
00047     if success:
00048       self.set_status('FAILURE')
00049 
00050     
00051 
00052   def set_status(self,status):
00053       if status == 'SUCCESS':
00054         self._feedback.status = 1
00055         self._result.status = self._feedback.status
00056         rospy.loginfo('Action %s: Succeeded' % self._action_name)
00057         self._as.set_succeeded(self._result)
00058       elif status == 'FAILURE':
00059         self._feedback.status = 2
00060         self._result.status = self._feedback.status
00061         rospy.loginfo('Action %s: Failed' % self._action_name)
00062         self._as.set_succeeded(self._result)
00063       else:
00064         rospy.logerr('Action %s: has a wrong return status' % self._action_name)
00065 
00066 
00067 
00068 if __name__ == '__main__':
00069   rospy.init_node('action')
00070   BTAction(rospy.get_name())
00071   rospy.spin()


behavior_tree_leaves
Author(s): Michele Colledanchise
autogenerated on Sun Sep 10 2017 02:31:55