00001 #include "behaviortree_cpp/behavior_tree.h" 00002 #include "behaviortree_cpp/bt_factory.h" 00003 00004 using namespace BT; 00005 00006 NodeStatus SayHello() 00007 { 00008 printf("hello\n"); 00009 return NodeStatus::SUCCESS; 00010 } 00011 00012 class ActionTestNode : public ActionNode 00013 { 00014 public: 00015 ActionTestNode(const std::string& name) : ActionNode(name) 00016 { 00017 } 00018 00019 NodeStatus tick() override 00020 { 00021 time_ = 5; 00022 stop_loop_ = false; 00023 int i = 0; 00024 while (!stop_loop_ && i++ < time_) 00025 { 00026 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 00027 } 00028 return NodeStatus::SUCCESS; 00029 } 00030 00031 virtual void halt() override 00032 { 00033 stop_loop_ = true; 00034 setStatus(NodeStatus::IDLE); 00035 } 00036 00037 private: 00038 int time_; 00039 std::atomic_bool stop_loop_; 00040 }; 00041 00042 int main() 00043 { 00044 BT::SequenceNode root("root"); 00045 BT::SimpleActionNode action1("say_hello", std::bind(SayHello)); 00046 ActionTestNode action2("async_action"); 00047 00048 root.addChild(&action1); 00049 root.addChild(&action2); 00050 00051 int count = 0; 00052 00053 NodeStatus status = NodeStatus::RUNNING; 00054 00055 while (status == NodeStatus::RUNNING) 00056 { 00057 status = root.executeTick(); 00058 00059 std::cout << count++ << " : " << root.status() << " / " << action1.status() << " / " 00060 << action2.status() << std::endl; 00061 00062 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 00063 } 00064 00065 haltAllActions(&root); 00066 00067 return 0; 00068 }