action_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018 Davide Faconti - 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 "leaf_node.h"
19 
20 namespace BT
21 {
30 class ActionNodeBase : public LeafNode
31 {
32  public:
33 
34  ActionNodeBase(const std::string& name, const NodeParameters& parameters = NodeParameters());
35  ~ActionNodeBase() override = default;
36 
37  virtual NodeStatus executeTick() override;
38 
39  virtual NodeType type() const override final
40  {
41  return NodeType::ACTION;
42  }
43 };
44 
51 {
52  public:
53 
54  SyncActionNode(const std::string& name, const NodeParameters& parameters = NodeParameters());
55  ~SyncActionNode() override = default;
56 
57  virtual NodeStatus executeTick() override;
58 
59  virtual void halt() override final // don't need to override this
60  {
62  }
63 };
64 
78 {
79  public:
80  typedef std::function<NodeStatus(TreeNode&)> TickFunctor;
81 
82  // Constructor: you must provide the function to call when tick() is invoked
83  SimpleActionNode(const std::string& name, TickFunctor tick_functor,
84  const NodeParameters &params = NodeParameters());
85 
86  ~SimpleActionNode() override = default;
87 
88  virtual void halt() override
89  {
90  // not supported
91  }
92 
93  protected:
94  virtual NodeStatus tick() override;
95 
96  TickFunctor tick_functor_;
97 };
98 
111 {
112  public:
113 
114  AsyncActionNode(const std::string& name, const NodeParameters& parameters = NodeParameters());
115  virtual ~AsyncActionNode() override;
116 
117  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
118  virtual NodeStatus executeTick() override final;
119 
120  void stopAndJoinThread();
121 
122  protected:
123 
124  // The method that is going to be executed by the thread
125  void waitForTick();
126 
127  // The thread that will execute the node
128  std::thread thread_;
129 
130  // Node semaphore to simulate the tick
131  // (and to synchronize fathers and children)
133 
134  std::atomic<bool> loop_;
135 };
136 
137 // Why is the name "ActionNode" deprecated?
138 //
139 // ActionNode was renamed "AsyncActionNode" because it's implementation, i.e. one thread
140 // per action, is too wastefull in terms of resources.
141 // The name ActionNode seems to imply that it is the default Node to use for Actions.
142 // But, in my opinion, the user should think twice if using it and carefully consider the cost of abstraction.
143 // For this reason, AsyncActionNode is a much better name.
144 
145 
146 // The right class to use for synchronous Actions is SyncActionBase
147 [[deprecated]]
149 
159 {
160  public:
161  // Constructor
162  CoroActionNode(const std::string& name, const NodeParameters& parameters = NodeParameters());
163  virtual ~CoroActionNode() override;
164 
168  void setStatusRunningAndYield();
169 
170  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
171  virtual NodeStatus executeTick() override final;
172 
184  void halt() override;
185 
186  protected:
187 
188  struct Pimpl; // The Pimpl idiom
189  std::unique_ptr<Pimpl> _p;
190 
191 };
192 
193 
194 } //end namespace
195 
196 #endif
std::thread thread_
Definition: action_node.h:128
const std::string & name() const
Definition: tree_node.cpp:92
virtual void halt()=0
The method used to interrupt the execution of a RUNNING node.
The AsyncActionNode a different thread where the action will be executed.
Definition: action_node.h:110
virtual NodeType type() const overridefinal
Definition: action_node.h:39
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
Definition: action_node.h:158
The SyncActionNode is an helper derived class that explicitly forbids the status RUNNING and doesn&#39;t ...
Definition: action_node.h:50
~ActionNodeBase() override=default
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
std::unique_ptr< Pimpl > _p
Definition: action_node.h:188
virtual void halt() overridefinal
The method used to interrupt the execution of a RUNNING node.
Definition: action_node.h:59
ActionNodeBase(const std::string &name, const NodeParameters &parameters=NodeParameters())
Definition: action_node.cpp:19
virtual NodeStatus executeTick() override
The method that will be executed to invoke tick(); and setStatus();.
Definition: action_node.cpp:24
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:80
The SimpleActionNode provides an easy to use ActionNode. The user should simply provide a callback wi...
Definition: action_node.h:77
TickEngine tick_engine_
Definition: action_node.h:132
NodeStatus
Definition: basic_types.h:28
TickFunctor tick_functor_
Definition: action_node.h:96
virtual void halt() override
The method used to interrupt the execution of a RUNNING node.
Definition: action_node.h:88
NodeType
Definition: basic_types.h:16
std::atomic< bool > loop_
Definition: action_node.h:134
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
AsyncActionNode ActionNode
Definition: action_node.h:148
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53