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 
92  TickFunctor tick_functor_;
93 };
94 
115 {
116 public:
117  AsyncActionNode(const std::string& name, const NodeConfiguration& config) :
118  ActionNodeBase(name, 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_;
136 };
137 
154 {
155 public:
156  StatefulActionNode(const std::string& name, const NodeConfiguration& config) :
157  ActionNodeBase(name, 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
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
virtual void halt()=0
virtual void halt() override final
You don&#39;t need to override this.
Definition: action_node.h:62
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
std::atomic_bool halt_requested_
Definition: action_node.h:133
StatefulActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:156
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
bool isHaltRequested() const
Definition: action_node.h:121
The AsyncActionNode uses a different thread, where the action will be executed.
Definition: action_node.h:114
The ActionNode is the prefered way to implement asynchronous Actions. It is actually easier to use co...
Definition: action_node.h:153
The CoroActionNode class is an a good candidate for asynchronous actions which need to communicate wi...
Definition: action_node.h:186
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
~ActionNodeBase() override=default
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
std::unique_ptr< Pimpl > _p
Definition: action_node.h:212
ActionNodeBase(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:18
std::future< void > thread_handle_
Definition: action_node.h:134
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:81
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:78
std::exception_ptr exptr_
Definition: action_node.h:132
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
AsyncActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:117
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
virtual NodeType type() const override final
Definition: action_node.h:41


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14