00001 /* 00002 * Abstract class used to create different BT actions. 00003 * Usage: Pass the action's name to this class when constructing your action. 00004 * Nodehandles and other variables should be part of your implementation. 00005 * Maintainer: Anas Abou Allaban 00006 */ 00007 00008 #ifndef BTACTION_CLASS 00009 #define BTACTION_CLASS 00010 00011 #include <ros/ros.h> 00012 #include <actionlib/server/simple_action_server.h> 00013 #include <behavior_tree_core/BTAction.h> 00014 00015 #include <functional> // std::bind 00016 00017 enum Status {RUNNING, SUCCESS, FAILURE}; // BT return statuses 00018 00019 class BTAction 00020 { 00021 protected: 00022 // Create action server 00023 actionlib::SimpleActionServer<behavior_tree_core::BTAction> as_; 00024 std::string action_name_; 00025 behavior_tree_core::BTFeedback feedback_; // Action feedback (SUCCESS, FAILURE) 00026 behavior_tree_core::BTResult result_; // Action feedback (feedback for us) 00027 00028 public: 00029 BTAction(std::string name); 00030 ~BTAction(void) {} 00031 virtual void execute_callback(const behavior_tree_core::BTGoalConstPtr &goal) = 0; 00032 void set_status(int status); // Returns the status to the client (Behavior Tree) 00033 00034 }; 00035 00036 BTAction::BTAction(std::string name): 00037 as_(name, std::bind(&BTAction::execute_callback, this, _1), false), 00038 action_name_(name) 00039 { 00040 as_.start(); // Starts the action server 00041 } 00042 00043 void BTAction::set_status(int status) 00044 { 00045 // Set The feedback and result of BT.action 00046 feedback_.status = status; 00047 result_.status = feedback_.status; 00048 as_.publishFeedback(feedback_); // Publish feedback 00049 // setSucceeded means that it has finished the action (it has returned SUCCESS or FAILURE). 00050 as_.setSucceeded(result_); 00051 00052 switch (status) // Print for convenience 00053 { 00054 case SUCCESS: 00055 ROS_INFO("%s Succeeded", ros::this_node::getName().c_str() ); 00056 break; 00057 case FAILURE: 00058 ROS_INFO("%s Failed", ros::this_node::getName().c_str() ); 00059 break; 00060 default: 00061 break; 00062 } 00063 } 00064 00065 #endif // BTACTION_CLASS