t04_blackboard.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/xml_parsing.h"
00002 #include "behaviortree_cpp/loggers/bt_cout_logger.h"
00003 #include "behaviortree_cpp/blackboard/blackboard_local.h"
00004 
00005 #include "movebase_node.h"
00006 
00007 using namespace BT;
00008 
00023 // clang-format off
00024 const std::string xml_text = R"(
00025 
00026  <root main_tree_to_execute = "MainTree" >
00027      <BehaviorTree ID="MainTree">
00028         <SequenceStar name="root">
00029             <CalculateGoalPose/>
00030             <MoveBase  goal="${GoalPose}" />
00031             <SetBlackboard key="OtherGoal" value="-1;3;0.5" />
00032             <MoveBase  goal="${OtherGoal}" />
00033         </SequenceStar>
00034      </BehaviorTree>
00035  </root>
00036  )";
00037 
00038 // clang-format on
00039 
00040 // Write into the blackboard key: [GoalPose]
00041 // Use this function to create a SimpleActionNode that can access the blackboard
00042 NodeStatus CalculateGoalPose(TreeNode& self)
00043 {
00044     const Pose2D mygoal = {1.1, 2.3, 1.54};
00045 
00046     // RECOMMENDED: check if the blackboard is nullptr first
00047     if (self.blackboard())
00048     {
00049         // store it in the blackboard
00050         self.blackboard()->set("GoalPose", mygoal);
00051         return NodeStatus::SUCCESS;
00052     }
00053     else
00054     {
00055         return NodeStatus::FAILURE;
00056     }
00057 }
00058 
00059 int main()
00060 {
00061     using namespace BT;
00062 
00063     BehaviorTreeFactory factory;
00064     factory.registerSimpleAction("CalculateGoalPose", CalculateGoalPose);
00065     factory.registerNodeType<MoveBaseAction>("MoveBase");
00066 
00067     // create a Blackboard from BlackboardLocal (simple, not persistent, local storage)
00068     auto blackboard = Blackboard::create<BlackboardLocal>();
00069 
00070     // Important: when the object tree goes out of scope, all the TreeNodes are destroyed
00071     auto tree = buildTreeFromText(factory, xml_text, blackboard);
00072 
00073     NodeStatus status = NodeStatus::RUNNING;
00074     while (status == NodeStatus::RUNNING)
00075     {
00076         status = tree.root_node->executeTick();
00077         SleepMS(1);   // optional sleep to avoid "busy loops"
00078     }
00079     return 0;
00080 }


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