t04_blackboard.cpp
Go to the documentation of this file.
4 
5 #include "movebase_node.h"
6 
7 using namespace BT;
8 
23 // clang-format off
24 const std::string xml_text = R"(
25 
26  <root main_tree_to_execute = "MainTree" >
27  <BehaviorTree ID="MainTree">
28  <SequenceStar name="root">
29  <CalculateGoalPose/>
30  <MoveBase goal="${GoalPose}" />
31  <SetBlackboard key="OtherGoal" value="-1;3;0.5" />
32  <MoveBase goal="${OtherGoal}" />
33  </SequenceStar>
34  </BehaviorTree>
35  </root>
36  )";
37 
38 // clang-format on
39 
40 // Write into the blackboard key: [GoalPose]
41 // Use this function to create a SimpleActionNode that can access the blackboard
43 {
44  const Pose2D mygoal = {1.1, 2.3, 1.54};
45 
46  // RECOMMENDED: check if the blackboard is nullptr first
47  if (self.blackboard())
48  {
49  // store it in the blackboard
50  self.blackboard()->set("GoalPose", mygoal);
51  return NodeStatus::SUCCESS;
52  }
53  else
54  {
55  return NodeStatus::FAILURE;
56  }
57 }
58 
59 int main()
60 {
61  using namespace BT;
62 
63  BehaviorTreeFactory factory;
64  factory.registerSimpleAction("CalculateGoalPose", CalculateGoalPose);
65  factory.registerNodeType<MoveBaseAction>("MoveBase");
66 
67  // create a Blackboard from BlackboardLocal (simple, not persistent, local storage)
68  auto blackboard = Blackboard::create<BlackboardLocal>();
69 
70  // Important: when the object tree goes out of scope, all the TreeNodes are destroyed
71  auto tree = buildTreeFromText(factory, xml_text, blackboard);
72 
74  while (status == NodeStatus::RUNNING)
75  {
76  status = tree.root_node->executeTick();
77  SleepMS(1); // optional sleep to avoid "busy loops"
78  }
79  return 0;
80 }
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:98
void SleepMS(int ms)
Definition: movebase_node.h:12
int main()
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor)
Register a SimpleActionNode.
Definition: bt_factory.cpp:85
NodeStatus CalculateGoalPose(TreeNode &self)
NodeStatus
Definition: basic_types.h:28
const std::string xml_text
Tree buildTreeFromText(const BehaviorTreeFactory &factory, const std::string &text, const Blackboard::Ptr &blackboard=Blackboard::Ptr())


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53