Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <ros/ros.h>
00014 #include <actionlib/server/simple_action_server.h>
00015 #include <behavior_tree_core/BTAction.h>
00016
00017 #include <string>
00018
00019
00020 enum Status {RUNNING, SUCCESS, FAILURE};
00021
00022
00023 class BTAction
00024 {
00025 protected:
00026 ros::NodeHandle nh_;
00027
00028 actionlib::SimpleActionServer<behavior_tree_core::BTAction> as_;
00029 std::string action_name_;
00030
00031 behavior_tree_core::BTFeedback feedback_;
00032 behavior_tree_core::BTResult result_;
00033
00034
00035 public:
00036 explicit BTAction(std::string name) :
00037 as_(nh_, name, boost::bind(&BTAction::execute_callback, this, _1), false),
00038 action_name_(name)
00039 {
00040
00041 as_.start();
00042 }
00043
00044 ~BTAction(void)
00045 {}
00046
00047 void execute_callback(const behavior_tree_core::BTGoalConstPtr &goal)
00048 {
00049
00050 ROS_INFO("Starting Action");
00051
00052
00053 int i = 0;
00054 while (i < 5)
00055 {
00056
00057 if (as_.isPreemptRequested())
00058 {
00059 ROS_INFO("Action Halted");
00060
00061
00062 as_.setPreempted();
00063 break;
00064 }
00065 ROS_INFO("Executing Action");
00066
00067 ros::Duration(0.5).sleep();
00068 i++;
00069 }
00070
00071 if (i == 5)
00072 {
00073 set_status(SUCCESS);
00074 }
00075 }
00076
00077
00078
00079 void set_status(int status)
00080 {
00081
00082 feedback_.status = status;
00083 result_.status = feedback_.status;
00084
00085 as_.publishFeedback(feedback_);
00086
00087 as_.setSucceeded(result_);
00088
00089 switch (status)
00090 {
00091 case SUCCESS:
00092 ROS_INFO("Action %s Succeeded", ros::this_node::getName().c_str() );
00093 break;
00094 case FAILURE:
00095 ROS_INFO("Action %s Failed", ros::this_node::getName().c_str() );
00096 break;
00097 default:
00098 break;
00099 }
00100 }
00101 };
00102
00103
00104 int main(int argc, char** argv)
00105 {
00106 ros::init(argc, argv, "action");
00107 ROS_INFO(" Enum: %d", RUNNING);
00108 ROS_INFO(" Action Ready for Ticks");
00109 BTAction bt_action(ros::this_node::getName());
00110 ros::spin();
00111
00112 return 0;
00113 }