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 NodeConfig& 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 NodeConfig& config);
56  ~SyncActionNode() override = default;
57 
59  virtual NodeStatus executeTick() override;
60 
62  virtual void halt() override final
63  {
64  resetStatus();
65  }
66 };
67 
80 {
81 public:
82  using TickFunctor = std::function<NodeStatus(TreeNode&)>;
83 
84  // You must provide the function to call when tick() is invoked
85  SimpleActionNode(const std::string& name, TickFunctor tick_functor,
86  const NodeConfig& config);
87 
88  ~SimpleActionNode() override = default;
89 
90 protected:
91  virtual NodeStatus tick() override final;
92 
94 };
95 
117 {
118 public:
119  ThreadedAction(const std::string& name, const NodeConfig& config)
121  {}
122 
123  bool isHaltRequested() const
124  {
125  return halt_requested_.load();
126  }
127 
128  // This method spawn a new thread. Do NOT remove the "final" keyword.
129  virtual NodeStatus executeTick() override final;
130 
131  virtual void halt() override;
132 
133 private:
134  std::exception_ptr exptr_;
135  std::atomic_bool halt_requested_ = false;
136  std::future<void> thread_handle_;
137  std::mutex mutex_;
138 };
139 
140 #ifdef USE_BTCPP3_OLD_NAMES
141 using AsyncActionNode = ThreadedAction;
142 #endif
143 
160 {
161 public:
162  StatefulActionNode(const std::string& name, const NodeConfig& config)
164  {}
165 
168  virtual NodeStatus onStart() = 0;
169 
171  virtual NodeStatus onRunning() = 0;
172 
175  virtual void onHalted() = 0;
176 
177  bool isHaltRequested() const;
178 
179 protected:
180  // do not override this method
181  NodeStatus tick() override final;
182  // do not override this method
183  void halt() override final;
184 
185 private:
186  std::atomic_bool halt_requested_ = false;
187 };
188 
197 {
198 public:
199  CoroActionNode(const std::string& name, const NodeConfig& config);
200  virtual ~CoroActionNode() override;
201 
203  void setStatusRunningAndYield();
204 
205  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
206  virtual NodeStatus executeTick() override final;
207 
208  // Used internally, but it needs to be public
209  void tickImpl();
210 
222  void halt() override;
223 
224 protected:
225  struct Pimpl; // The Pimpl idiom
226  std::unique_ptr<Pimpl> _p;
227 
228  void destroyCoroutine();
229 };
230 
231 } // namespace BT
232 
233 #endif
BT
Definition: ex01_wrap_legacy.cpp:29
BT::NodeType
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:20
BT::CoroActionNode::Pimpl
Definition: action_node.cpp:68
BT::LeafNode
Definition: leaf_node.h:21
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::ActionNodeBase::type
virtual NodeType type() const override final
Definition: action_node.h:41
BT::StatefulActionNode::StatefulActionNode
StatefulActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.h:162
BT::SyncActionNode::~SyncActionNode
~SyncActionNode() override=default
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::SimpleActionNode::~SimpleActionNode
~SimpleActionNode() override=default
BT::ActionNodeBase::ActionNodeBase
ActionNodeBase(const std::string &name, const NodeConfig &config)
Definition: action_node.cpp:20
BT::SyncActionNode::executeTick
virtual NodeStatus executeTick() override
throws if the derived class return RUNNING.
Definition: action_node.cpp:56
mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:77
BT::ActionNodeBase::~ActionNodeBase
~ActionNodeBase() override=default
BT::NodeType::ACTION
@ ACTION
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.cpp:52
BT::SimpleActionNode::tick_functor_
TickFunctor tick_functor_
Definition: action_node.h:93
BT::SimpleActionNode::tick
virtual NodeStatus tick() override final
Method to be implemented by the user.
Definition: action_node.cpp:32
BT::SimpleActionNode::SimpleActionNode
SimpleActionNode(const std::string &name, TickFunctor tick_functor, const NodeConfig &config)
Definition: action_node.cpp:26
BT::ThreadedAction::ThreadedAction
ThreadedAction(const std::string &name, const NodeConfig &config)
Definition: action_node.h:119
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
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:225
BT::TreeNode::resetStatus
void resetStatus()
Set the status to IDLE.
Definition: tree_node.cpp:268
std
Definition: std.hpp:31
BT::SimpleActionNode
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:79
BT::ThreadedAction
The ThreadedAction executes the tick in a different thread.
Definition: action_node.h:116
BT::SyncActionNode::halt
virtual void halt() override final
You don't need to override this.
Definition: action_node.h:62
leaf_node.h
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::SimpleActionNode::TickFunctor
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:82
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::ThreadedAction::isHaltRequested
bool isHaltRequested() const
Definition: action_node.h:123
BT::CoroActionNode
The CoroActionNode class is an a good candidate for asynchronous actions which need to communicate wi...
Definition: action_node.h:196
BT::StatefulActionNode
The StatefulActionNode is the preferred way to implement asynchronous Actions. It is actually easier ...
Definition: action_node.h:159


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