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 "leaf_node.h"
21 
22 namespace BT
23 {
24 
25 // IMPORTANT: Actions which returned SUCCESS or FAILURE will not be ticked
26 // again unless setStatus(IDLE) is called first.
27 // Keep this in mind when writing your custom Control and Decorator nodes.
28 
29 
35 class ActionNodeBase : public LeafNode
36 {
37  public:
38 
39  ActionNodeBase(const std::string& name, const NodeConfiguration& config);
40  ~ActionNodeBase() override = default;
41 
42  virtual NodeType type() const override final
43  {
44  return NodeType::ACTION;
45  }
46 };
47 
54 {
55  public:
56 
57  SyncActionNode(const std::string& name, const NodeConfiguration& config);
58  ~SyncActionNode() override = default;
59 
61  virtual NodeStatus executeTick() override;
62 
64  virtual void halt() override final
65  {
67  }
68 };
69 
83 {
84  public:
85  typedef std::function<NodeStatus(TreeNode&)> TickFunctor;
86 
87  // You must provide the function to call when tick() is invoked
88  SimpleActionNode(const std::string& name, TickFunctor tick_functor,
89  const NodeConfiguration& config);
90 
91  ~SimpleActionNode() override = default;
92 
93  protected:
94  virtual NodeStatus tick() override final;
95 
96  TickFunctor tick_functor_;
97 };
98 
116 {
117  public:
118 
119  AsyncActionNode(const std::string& name, const NodeConfiguration& config):ActionNodeBase(name, config)
120  {
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 
135  std::exception_ptr exptr_;
136  std::atomic_bool halt_requested_;
137  std::future<NodeStatus> thread_handle_;
138 };
139 
156 {
157  public:
158  StatefulActionNode(const std::string& name, const NodeConfiguration& config):
159  ActionNodeBase(name,config)
160  {}
161 
162  // do not override this method
163  NodeStatus tick() override final;
164  // do not override this method
165  void halt() override final;
166 
169  virtual NodeStatus onStart() = 0;
170 
172  virtual NodeStatus onRunning() = 0;
173 
176  virtual void onHalted() = 0;
177 };
178 
179 
180 #ifndef BT_NO_COROUTINES
181 
191 {
192  public:
193 
194  CoroActionNode(const std::string& name, const NodeConfiguration& config);
195  virtual ~CoroActionNode() override;
196 
198  void setStatusRunningAndYield();
199 
200  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
201  virtual NodeStatus executeTick() override final;
202 
214  void halt() override;
215 
216  protected:
217 
218  struct Pimpl; // The Pimpl idiom
219  std::unique_ptr<Pimpl> _p;
220 };
221 #endif
222 
223 } //end namespace
224 
225 #endif
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
virtual void halt()=0
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
bool isHaltRequested() const
Definition: action_node.h:123
std::atomic_bool halt_requested_
Definition: action_node.h:136
StatefulActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:158
The AsyncActionNode uses a different thread, where the action will be executed.
Definition: action_node.h:115
The ActionNode is the goto option for, but it is actually much easier to use correctly.
Definition: action_node.h:155
virtual NodeType type() const overridefinal
Definition: action_node.h:42
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
Definition: action_node.h:190
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:53
~ActionNodeBase() override=default
std::unique_ptr< Pimpl > _p
Definition: action_node.h:218
virtual void halt() overridefinal
You don&#39;t need to override this.
Definition: action_node.h:64
ActionNodeBase(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:18
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:85
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:82
std::exception_ptr exptr_
Definition: action_node.h:135
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:33
AsyncActionNode(const std::string &name, const NodeConfiguration &config)
Definition: action_node.h:119
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
std::future< NodeStatus > thread_handle_
Definition: action_node.h:137
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24