action_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2019 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 "leaf_node.h"
20 
21 namespace BT
22 {
23 
24 // IMPORTANT: Actions which returned SUCCESS or FAILURE will not be ticked
25 // again unless setStatus(IDLE) is called first.
26 // Keep this in mind when writing your custom Control and Decorator nodes.
27 
28 
34 class ActionNodeBase : public LeafNode
35 {
36  public:
37 
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 
56  SyncActionNode(const std::string& name, const NodeConfiguration& config);
57  ~SyncActionNode() override = default;
58 
60  virtual NodeStatus executeTick() override;
61 
63  virtual void halt() override final
64  {
66  }
67 };
68 
82 {
83  public:
84  typedef std::function<NodeStatus(TreeNode&)> TickFunctor;
85 
86  // You must provide the function to call when tick() is invoked
87  SimpleActionNode(const std::string& name, TickFunctor tick_functor,
88  const NodeConfiguration& config);
89 
90  ~SimpleActionNode() override = default;
91 
92  protected:
93  virtual NodeStatus tick() override final;
94 
95  TickFunctor tick_functor_;
96 };
97 
106 {
107  public:
108 
109  AsyncActionNode(const std::string& name, const NodeConfiguration& config);
110  virtual ~AsyncActionNode() override;
111 
112  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
113  virtual NodeStatus executeTick() override final;
114 
115  void stopAndJoinThread();
116 
117  private:
118 
119  // The method that will be executed by the thread
120  void asyncThreadLoop();
121 
122  void waitStart();
123 
124  void notifyStart();
125 
126  std::atomic<bool> keep_thread_alive_;
127 
129 
131 
132  std::condition_variable start_signal_;
133 
134  std::exception_ptr exptr_;
135 
136  std::thread thread_;
137 };
138 
148 {
149  public:
150 
151  CoroActionNode(const std::string& name, const NodeConfiguration& config);
152  virtual ~CoroActionNode() override;
153 
155  void setStatusRunningAndYield();
156 
157  // This method triggers the TickEngine. Do NOT remove the "final" keyword.
158  virtual NodeStatus executeTick() override final;
159 
171  void halt() override;
172 
173  protected:
174 
175  struct Pimpl; // The Pimpl idiom
176  std::unique_ptr<Pimpl> _p;
177 
178 };
179 
180 
181 } //end namespace
182 
183 #endif
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:34
std::thread thread_
Definition: action_node.h:136
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
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
The AsyncActionNode uses a different thread where the action will be executed.
Definition: action_node.h:105
virtual NodeType type() const overridefinal
Definition: action_node.h:41
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
Definition: action_node.h:147
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
std::mutex start_mutex_
Definition: action_node.h:130
std::unique_ptr< Pimpl > _p
Definition: action_node.h:175
std::atomic< bool > keep_thread_alive_
Definition: action_node.h:126
virtual void halt() overridefinal
You don&#39;t need to override this.
Definition: action_node.h:63
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:84
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:81
std::condition_variable start_signal_
Definition: action_node.h:132
ActionNodeBase(const std::string &name, const NodeConfiguration &config)
Definition: action_node.cpp:19
std::exception_ptr exptr_
Definition: action_node.h:134
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
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04