t18_waypoints.cpp
Go to the documentation of this file.
4 #include <deque>
5 
6 using namespace BT;
7 
8 /*
9  * In this example we will show how a common design pattern could be implemented.
10  * We want to iterate through the elements of a queue, for instance a list of waypoints.
11  */
12 
13 struct Pose2D
14 {
15  double x, y, theta;
16 };
17 
22 {
23 public:
24  GenerateWaypoints(const std::string& name, const NodeConfig& config)
25  : SyncActionNode(name, config)
26  {}
27 
28  NodeStatus tick() override
29  {
30  auto shared_queue = std::make_shared<std::deque<Pose2D>>();
31  for(int i = 0; i < 5; i++)
32  {
33  shared_queue->push_back(Pose2D{ double(i), double(i), 0 });
34  }
35  setOutput("waypoints", shared_queue);
36  return NodeStatus::SUCCESS;
37  }
38 
40  {
41  return { OutputPort<SharedQueue<Pose2D>>("waypoints") };
42  }
43 };
44 //--------------------------------------------------------------
45 class PrintNumber : public SyncActionNode
46 {
47 public:
48  PrintNumber(const std::string& name, const NodeConfig& config)
49  : SyncActionNode(name, config)
50  {}
51 
52  NodeStatus tick() override
53  {
54  double value;
55  if(getInput("value", value))
56  {
57  std::cout << "PrintNumber: " << value << "\n";
58  return NodeStatus::SUCCESS;
59  }
60  return NodeStatus::FAILURE;
61  }
62 
64  {
65  return { InputPort<double>("value") };
66  }
67 };
68 
69 //--------------------------------------------------------------
70 
75 {
76 public:
77  UseWaypoint(const std::string& name, const NodeConfig& config)
78  : ThreadedAction(name, config)
79  {}
80 
81  NodeStatus tick() override
82  {
83  Pose2D wp;
84  if(getInput("waypoint", wp))
85  {
86  std::this_thread::sleep_for(std::chrono::milliseconds(100));
87  std::cout << "Using waypoint: " << wp.x << "/" << wp.y << std::endl;
88  return NodeStatus::SUCCESS;
89  }
90  else
91  {
92  return NodeStatus::FAILURE;
93  }
94  }
95 
97  {
98  return { InputPort<Pose2D>("waypoint") };
99  }
100 };
101 
102 // clang-format off
103 static const char* xml_tree = R"(
104  <root BTCPP_format="4" >
105  <BehaviorTree ID="TreeA">
106  <Sequence>
107  <LoopDouble queue="1;2;3" value="{number}">
108  <PrintNumber value="{number}" />
109  </LoopDouble>
110 
111  <GenerateWaypoints waypoints="{waypoints}" />
112  <LoopPose queue="{waypoints}" value="{wp}">
113  <UseWaypoint waypoint="{wp}" />
114  </LoopPose>
115  </Sequence>
116  </BehaviorTree>
117  </root>
118  )";
119 
120 // clang-format on
121 
122 int main()
123 {
124  BehaviorTreeFactory factory;
125 
126  factory.registerNodeType<LoopNode<Pose2D>>("LoopPose");
127 
128  factory.registerNodeType<UseWaypoint>("UseWaypoint");
129  factory.registerNodeType<PrintNumber>("PrintNumber");
130  factory.registerNodeType<GenerateWaypoints>("GenerateWaypoints");
131 
132  auto tree = factory.createTreeFromText(xml_tree);
133 
134  StdCoutLogger logger(tree);
135  logger.enableTransitionToIdle(false);
136 
137  tree.tickWhileRunning();
138 
139  return 0;
140 }
PrintNumber::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t18_waypoints.cpp:52
BT
Definition: ex01_wrap_legacy.cpp:29
UseWaypoint::UseWaypoint
UseWaypoint(const std::string &name, const NodeConfig &config)
Definition: t18_waypoints.cpp:77
Pose2D
Definition: t18_waypoints.cpp:13
BT::LoopNode
The LoopNode class is used to pop_front elements from a std::deque. This element is copied into the p...
Definition: loop_node.h:37
bt_factory.h
main
int main()
Definition: t18_waypoints.cpp:122
xml_tree
static const char * xml_tree
Definition: t18_waypoints.cpp:103
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
UseWaypoint
Simple Action that uses the output of PopFromQueue<Pose2D> or ConsumeQueue<Pose2D>
Definition: t18_waypoints.cpp:74
loop_node.h
PrintNumber::providedPorts
static PortsList providedPorts()
Definition: t18_waypoints.cpp:63
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
PrintNumber::PrintNumber
PrintNumber(const std::string &name, const NodeConfig &config)
Definition: t18_waypoints.cpp:48
Pose2D::y
double y
Definition: t18_waypoints.cpp:15
bt_cout_logger.h
convert_v3_to_v4.logger
logger
Definition: convert_v3_to_v4.py:12
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
GenerateWaypoints
Dummy action that generates a list of poses.
Definition: t18_waypoints.cpp:21
BT::NodeStatus::SUCCESS
@ SUCCESS
GenerateWaypoints::GenerateWaypoints
GenerateWaypoints(const std::string &name, const NodeConfig &config)
Definition: t18_waypoints.cpp:24
PrintNumber
Definition: t16_global_blackboard.cpp:49
UseWaypoint::providedPorts
static PortsList providedPorts()
Definition: t18_waypoints.cpp:96
GenerateWaypoints::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t18_waypoints.cpp:28
BT::ThreadedAction
The ThreadedAction executes the tick in a different thread.
Definition: action_node.h:116
UseWaypoint::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t18_waypoints.cpp:81
BT::NodeConfig
Definition: tree_node.h:73
Pose2D::x
double x
Definition: t18_waypoints.cpp:15
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
GenerateWaypoints::providedPorts
static PortsList providedPorts()
Definition: t18_waypoints.cpp:39
BT::StdCoutLogger
StdCoutLogger is a very simple logger that displays all the transitions on the console.
Definition: bt_cout_logger.h:14


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17