ex03_sqlite_log.cpp
Go to the documentation of this file.
1 #include "dummy_nodes.h"
5 
6 struct TaskA
7 {
8  int type;
9  std::string name;
10 };
11 
12 struct TaskB
13 {
14  double value;
15  std::string name;
16 };
17 
18 using Command = std::variant<TaskA, TaskB>;
19 
20 // Simple Action that updates an instance of Position2D in the blackboard
22 {
23 public:
24  SetTask(const std::string& name, const BT::NodeConfig& config)
26  {}
27 
28  BT::NodeStatus tick() override
29  {
30  auto type = getInput<std::string>("type").value();
31  if(type == "A")
32  {
33  setOutput<Command>("task", TaskA{ 43, type });
34  }
35  else if(type == "B")
36  {
37  setOutput<Command>("task", TaskB{ 3.14, type });
38  }
40  }
41 
43  {
44  return { BT::InputPort<std::string>("type"), BT::OutputPort<Command>("task") };
45  }
46 
47 private:
48 };
49 
50 // clang-format off
51 
52 static const char* xml_text = R"(
53 <root BTCPP_format="4">
54 
55  <BehaviorTree ID="MainTree">
56  <Sequence>
57 
58  <Script code="type:='A'" />
59  <SetTask type="{type}" task="{task}" />
60  <SubTree ID="ExecuteTaskA" task="{task}" _skipIf=" type!='A' " />
61 
62  <Script code="type:='B'" />
63  <SetTask type="{type}" task="{task}" />
64  <SubTree ID="ExecuteTaskB" task="{task}" _skipIf=" type!='B' " />
65 
66  </Sequence>
67  </BehaviorTree>
68 
69  <BehaviorTree ID="ExecuteTaskA">
70  <Sequence>
71  <Sleep msec="1000"/>
72  <SaySomething message="executed command A" />
73  </Sequence>
74  </BehaviorTree>
75 
76  <BehaviorTree ID="ExecuteTaskB">
77  <Sequence>
78  <Sleep msec="1000"/>
79  <SaySomething message="executed command B" />
80  </Sequence>
81  </BehaviorTree>
82 
83 </root>
84  )";
85 
86 // clang-format on
87 
88 int main()
89 {
91 
92  // Nodes registration, as usual
93  factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
94  factory.registerNodeType<SetTask>("SetTask");
95 
96  // Groot2 editor requires a model of your registered Nodes.
97  // You don't need to write that by hand, it can be automatically
98  // generated using the following command.
99  std::string xml_models = BT::writeTreeNodesModelXML(factory);
100 
102 
103  auto tree = factory.createTree("MainTree");
104 
105  std::cout << "----------- XML file ----------\n"
106  << BT::WriteTreeToXML(tree, false, false)
107  << "--------------------------------\n";
108 
109  BT::SqliteLogger sqlite_logger(tree, "ex08_sqlitelog.db3", false);
110 
111  //------------------------------------------------------------------------
112  // Write some data (from the blackboard) and write into the
113  // extra column called "extra_data". We will use JSON serialization
114 
115  auto meta_callback = [&](BT::Duration timestamp, const BT::TreeNode& node,
116  BT::NodeStatus prev_status,
117  BT::NodeStatus status) -> std::string {
118  if(prev_status == BT::NodeStatus::RUNNING && BT::isStatusCompleted(status))
119  {
120  if(node.name() == "ExecuteTaskA")
121  {
122  auto task = node.config().blackboard->get<Command>("task");
123  auto taskA = std::get<TaskA>(task);
125  json["taskA"] = { { "name", taskA.name }, { "type", taskA.type } };
126  return json.dump();
127  }
128  if(node.name() == "ExecuteTaskB")
129  {
130  auto task = node.config().blackboard->get<Command>("task");
131  auto taskB = std::get<TaskB>(task);
133  json["taskB"] = { { "name", taskB.name }, { "value", taskB.value } };
134  return json.dump();
135  }
136  }
137  return {};
138  };
139  sqlite_logger.setAdditionalCallback(meta_callback);
140  //------------------------------------------------------------------------
141  while(1)
142  {
143  std::cout << "Start" << std::endl;
144  tree.tickWhileRunning();
145  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
146  }
147 
148  return 0;
149 }
BT
Definition: ex01_wrap_legacy.cpp:29
SetTask::SetTask
SetTask(const std::string &name, const BT::NodeConfig &config)
Definition: ex03_sqlite_log.cpp:24
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:345
BT::ActionNodeBase::type
virtual NodeType type() const override final
Definition: action_node.h:41
BT::SqliteLogger::setAdditionalCallback
void setAdditionalCallback(ExtraCallback func)
Definition: bt_sqlite_logger.cpp:82
BT::SqliteLogger
The SqliteLogger is a logger that will store the tree and all the status transitions in a SQLite data...
Definition: bt_sqlite_logger.h:47
TaskB
Definition: ex03_sqlite_log.cpp:12
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
bt_sqlite_logger.h
basic_json::type
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:20596
bt_factory.h
basic_json
namespace for Niels Lohmann
Definition: json.hpp:3411
xml_text
static const char * xml_text
Definition: ex03_sqlite_log.cpp:52
BT::Duration
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:628
BT::WriteTreeToXML
std::string WriteTreeToXML(const Tree &tree, bool add_metadata, bool add_builtin_models)
WriteTreeToXML create a string that contains the XML that corresponds to a given tree....
Definition: xml_parsing.cpp:1497
dummy_nodes.h
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
TaskA
Definition: ex03_sqlite_log.cpp:6
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
SetTask::providedPorts
static BT::PortsList providedPorts()
Definition: ex03_sqlite_log.cpp:42
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.cpp:52
TaskA::name
std::string name
Definition: ex03_sqlite_log.cpp:9
xml_parsing.h
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:296
BT::NodeStatus::SUCCESS
@ SUCCESS
SetTask
Definition: ex03_sqlite_log.cpp:21
BT::NodeStatus::RUNNING
@ RUNNING
main
int main()
Definition: ex03_sqlite_log.cpp:88
BT::isStatusCompleted
bool isStatusCompleted(const NodeStatus &status)
Definition: basic_types.h:47
TaskB::name
std::string name
Definition: ex03_sqlite_log.cpp:15
basic_json::value
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:21528
json
basic_json<> json
default specialization
Definition: json.hpp:3422
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277
TaskA::type
int type
Definition: ex03_sqlite_log.cpp:8
SetTask::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: ex03_sqlite_log.cpp:28
TaskB::value
double value
Definition: ex03_sqlite_log.cpp:14
Command
std::variant< TaskA, TaskB > Command
Definition: ex03_sqlite_log.cpp:18
basic_json::dump
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:20574
BT::NodeConfig
Definition: tree_node.h:73
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::writeTreeNodesModelXML
std::string writeTreeNodesModelXML(const BehaviorTreeFactory &factory, bool include_builtin=false)
writeTreeNodesModelXML generates an XMl that contains the manifests in the <TreeNodesModel>
Definition: xml_parsing.cpp:1178


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07