00001 #include "crossdoor_nodes.h" 00002 #include "behaviortree_cpp/xml_parsing.h" 00003 #include "behaviortree_cpp/loggers/bt_cout_logger.h" 00004 #include "behaviortree_cpp/loggers/bt_minitrace_logger.h" 00005 #include "behaviortree_cpp/loggers/bt_file_logger.h" 00006 #include "behaviortree_cpp/blackboard/blackboard_local.h" 00007 00008 #ifdef ZMQ_FOUND 00009 #include "behaviortree_cpp/loggers/bt_zmq_publisher.h" 00010 #endif 00011 00012 // clang-format off 00013 00014 const std::string xml_text = R"( 00015 <root main_tree_to_execute = "MainTree"> 00016 <!---------------------------------------> 00017 <BehaviorTree ID="DoorClosed"> 00018 <Sequence name="door_closed_sequence"> 00019 <Inverter> 00020 <Condition ID="IsDoorOpen"/> 00021 </Inverter> 00022 <RetryUntilSuccesful num_attempts="4"> 00023 <OpenDoor/> 00024 </RetryUntilSuccesful> 00025 <PassThroughDoor/> 00026 </Sequence> 00027 </BehaviorTree> 00028 <!---------------------------------------> 00029 <BehaviorTree ID="MainTree"> 00030 <Fallback name="root_Fallback"> 00031 <Sequence name="door_open_sequence"> 00032 <IsDoorOpen/> 00033 <PassThroughDoor/> 00034 </Sequence> 00035 <SubTree ID="DoorClosed"/> 00036 <PassThroughWindow/> 00037 </Fallback> 00038 </BehaviorTree> 00039 <!---------------------------------------> 00040 </root> 00041 )"; 00042 00043 // clang-format on 00044 00045 using namespace BT; 00046 00047 int main() 00048 { 00049 BT::BehaviorTreeFactory factory; 00050 00051 // The state of the door is read/written using these keys of the blackboard. 00052 auto blackboard = Blackboard::create<BlackboardLocal>(); 00053 blackboard->set("door_open", false); 00054 blackboard->set("door_locked", true); 00055 00056 // register all the actions into the factory 00057 CrossDoor::RegisterNodes(factory); 00058 00059 // Important: when the object tree goes out of scope, all the TreeNodes are destroyed 00060 auto tree = buildTreeFromText(factory, xml_text, blackboard); 00061 00062 // Create some loggers 00063 StdCoutLogger logger_cout(tree.root_node); 00064 MinitraceLogger logger_minitrace(tree.root_node, "bt_trace.json"); 00065 FileLogger logger_file(tree.root_node, "bt_trace.fbl"); 00066 #ifdef ZMQ_FOUND 00067 PublisherZMQ publisher_zmq(tree.root_node); 00068 #endif 00069 00070 printTreeRecursively(tree.root_node); 00071 00072 //while (1) 00073 { 00074 NodeStatus status = NodeStatus::RUNNING; 00075 // Keep on ticking until you get either a SUCCESS or FAILURE state 00076 while( status == NodeStatus::RUNNING) 00077 { 00078 status = tree.root_node->executeTick(); 00079 CrossDoor::SleepMS(1); // optional sleep to avoid "busy loops" 00080 } 00081 CrossDoor::SleepMS(2000); 00082 } 00083 return 0; 00084 }