action_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 */
13 
14 #ifndef BEHAVIORTREECORE_ACTIONNODE_H
15 #define BEHAVIORTREECORE_ACTIONNODE_H
16 
17 #include <atomic>
18 #include <thread>
19 #include <future>
20 #include <mutex>
21 
22 #include "leaf_node.h"
23 
24 namespace BT
25 {
26 // IMPORTANT: Actions which returned SUCCESS or FAILURE will not be ticked
27 // again unless resetStatus() is called first.
28 // Keep this in mind when writing your custom Control and Decorator nodes.
29 
35 class ActionNodeBase : public LeafNode
36 {
37 public:
38  ActionNodeBase(const std::string& name, const NodeConfiguration& config);
39  ~ActionNodeBase() override = default;
40 
41  virtual NodeType type() const override final
42  {
43  return NodeType::ACTION;
44  }
45 };
46 
53 {
54 public:
55  SyncActionNode(const std::string& name, const NodeConfiguration& config);
56  ~SyncActionNode() override = default;
57 
59  virtual NodeStatus executeTick() override;
60 
62  virtual void halt() override final
63  {}
64 };
65 
79 {
80 public:
81  typedef std::function<NodeStatus(TreeNode&)> TickFunctor;
82 
83  // You must provide the function to call when tick() is invoked
84  SimpleActionNode(const std::string& name, TickFunctor tick_functor,
85  const NodeConfiguration& config);
86 
87  ~SimpleActionNode() override = default;
88 
89 protected:
90  virtual NodeStatus tick() override final;
91 
93 };
94 
115 {
116 public:
117  AsyncActionNode(const std::string& name, const NodeConfiguration& config) :
119  {}
120 
121  bool isHaltRequested() const
122  {
123  return halt_requested_.load();
124  }
125 
126  // This method spawn a new thread. Do NOT remove the "final" keyword.
127  virtual NodeStatus executeTick() override final;
128 
129  virtual void halt() override;
130 
131 private:
132  std::exception_ptr exptr_;
133  std::atomic_bool halt_requested_;
134  std::future<void> thread_handle_;
135  std::mutex mutex_;
136 };
137 
154 {
155 public:
156  StatefulActionNode(const std::string& name, const NodeConfiguration& config) :
158  {}
159 
160  // do not override this method
161  NodeStatus tick() override final;
162  // do not override this method
163  void halt() override final;
164 
167  virtual NodeStatus onStart() = 0;
168 
170  virtual NodeStatus onRunning() = 0;
171 
174  virtual void onHalted() = 0;
175 };
176 
177 #ifndef BT_NO_COROUTINES
178 
187 {
188 public:
189  CoroActionNode(const std::string& name, const NodeConfiguration& config);
190  virtual ~CoroActionNode() override;
191 
193  void setStatusRunningAndYield();
194 
195  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
196  virtual NodeStatus executeTick() override final;
197 
209  void halt() override;
210 
211 protected:
212  struct Pimpl; // The Pimpl idiom
213  std::unique_ptr<Pimpl> _p;
214 };
215 #endif
216 
217 } // namespace BT
218 
219 #endif
BT
Definition: ex01_wrap_legacy.cpp:29
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:50
BT::NodeType
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
BT::CoroActionNode::Pimpl
Definition: action_node.cpp:77
BT::LeafNode
Definition: leaf_node.h:21
minitrace::mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
BT::ActionNodeBase::type
virtual NodeType type() const override final
Definition: action_node.h:41
BT::SyncActionNode::~SyncActionNode
~SyncActionNode() override=default
BT::AsyncActionNode
The AsyncActionNode uses a different thread, where the action will be executed.
Definition: action_node.h:114
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:55
BT::SimpleActionNode::~SimpleActionNode
~SimpleActionNode() override=default
BT::AsyncActionNode::isHaltRequested
bool isHaltRequested() const
Definition: action_node.h:121
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::NodeConfiguration
Definition: tree_node.h:44
BT::StatefulActionNode::StatefulActionNode
StatefulActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:156
BT::SimpleActionNode::TickFunctor
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:81
BT::SyncActionNode::executeTick
virtual NodeStatus executeTick() override
throws if the derived class return RUNNING.
Definition: action_node.cpp:54
BT::ActionNodeBase::~ActionNodeBase
~ActionNodeBase() override=default
BT::NodeType::ACTION
@ ACTION
BT::ActionNodeBase::ActionNodeBase
ActionNodeBase(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:18
BT::SimpleActionNode::tick_functor_
TickFunctor tick_functor_
Definition: action_node.h:92
BT::SimpleActionNode::SimpleActionNode
SimpleActionNode(const std::string &name, TickFunctor tick_functor, const NodeConfiguration &config)
Definition: action_node.cpp:24
BT::SimpleActionNode::tick
virtual NodeStatus tick() override final
Method to be implemented by the user.
Definition: action_node.cpp:30
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
BT::ActionNodeBase
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
BT::CoroActionNode::_p
std::unique_ptr< Pimpl > _p
Definition: action_node.h:212
std
Definition: any.hpp:455
BT::SimpleActionNode
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:78
BT::SyncActionNode::halt
virtual void halt() override final
You don't need to override this.
Definition: action_node.h:62
BT::AsyncActionNode::AsyncActionNode
AsyncActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:117
leaf_node.h
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:35
BT::CoroActionNode
The CoroActionNode class is an a good candidate for asynchronous actions which need to communicate wi...
Definition: action_node.h:186
BT::StatefulActionNode
The ActionNode is the prefered way to implement asynchronous Actions. It is actually easier to use co...
Definition: action_node.h:153


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19