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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:09