action_node.cpp
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 
15 #include "coroutine/coroutine.h"
16 
17 namespace BT
18 {
19 ActionNodeBase::ActionNodeBase(const std::string& name, const NodeParameters& parameters)
20  : LeafNode::LeafNode(name, parameters)
21 {
22 }
23 
25 {
27  NodeStatus prev_status = status();
28 
29  if (prev_status == NodeStatus::IDLE || prev_status == NodeStatus::RUNNING)
30  {
31  setStatus(tick());
32  }
33  return status();
34 }
35 
36 //-------------------------------------------------------
37 
39  SimpleActionNode::TickFunctor tick_functor,
40  const NodeParameters& params)
41  : ActionNodeBase(name, params), tick_functor_(std::move(tick_functor))
42 {
43 }
44 
46 {
47  NodeStatus prev_status = status();
48 
49  if (prev_status == NodeStatus::IDLE)
50  {
52  prev_status = NodeStatus::RUNNING;
53  }
54 
56  if (status != prev_status)
57  {
58  setStatus(status);
59  }
60  return status;
61 }
62 
63 //-------------------------------------------------------
64 
65 AsyncActionNode::AsyncActionNode(const std::string& name, const NodeParameters& parameters)
66  : ActionNodeBase(name, parameters), loop_(true)
67 {
68  thread_ = std::thread(&AsyncActionNode::waitForTick, this);
69 }
70 
72 {
73  if (thread_.joinable())
74  {
76  }
77 }
78 
80 {
81  while (loop_.load())
82  {
84 
85  // check loop_ again because the tick_engine_ could be
86  // notified from the method stopAndJoinThread
87  if (loop_ && status() == NodeStatus::IDLE)
88  {
90  setStatus(tick());
91  }
92  }
93 }
94 
96 {
98  //send signal to other thread.
99  // The other thread is in charge for changing the status
100  if (status() == NodeStatus::IDLE)
101  {
103  }
104 
105  // block as long as the state is NodeStatus::IDLE
106  const NodeStatus stat = waitValidStatus();
107  return stat;
108 }
109 
111 {
112  loop_.store(false);
114  if (thread_.joinable())
115  {
116  thread_.join();
117  }
118 }
119 
120 //-------------------------------------
122 {
124  Pimpl(): coro(0) {}
125 };
126 
127 
129  const NodeParameters &parameters):
130  ActionNodeBase (name, parameters),
131  _p(new Pimpl)
132 {
133 }
134 
136 {
137  halt();
138 }
139 
141 {
144 }
145 
147 {
148  initializeOnce();
149  if (status() == NodeStatus::IDLE)
150  {
151  _p->coro = coroutine::create( [this]() { setStatus(tick()); } );
152  }
153 
154  if( _p->coro != 0)
155  {
156  auto res = coroutine::resume(_p->coro);
157 
159  {
160  coroutine::destroy(_p->coro);
161  _p->coro = 0;
162  }
163  }
164  return status();
165 }
166 
168 {
169  if( _p->coro != 0 )
170  {
171  coroutine::destroy(_p->coro);
172  _p->coro = 0;
173  }
174 }
175 
176 SyncActionNode::SyncActionNode(const std::string &name, const NodeParameters &parameters):
177  ActionNodeBase(name, parameters)
178 {}
179 
181 {
182  auto stat = ActionNodeBase::executeTick();
183  if( stat == NodeStatus::RUNNING)
184  {
185  throw std::logic_error("SyncActionNode MUSt never return RUNNING");
186  }
187  return stat;
188 }
189 
190 
191 
192 }
std::thread thread_
Definition: action_node.h:128
const std::string & name() const
Definition: tree_node.cpp:92
void yield()
Definition: coroutine.h:359
virtual NodeStatus executeTick() overridefinal
The method that will be executed to invoke tick(); and setStatus();.
void initializeOnce()
Definition: tree_node.cpp:118
void setStatusRunningAndYield()
Definition: any.hpp:455
routine_t create(std::function< void()> f)
Definition: coroutine.h:275
void destroy(routine_t id)
Definition: coroutine.h:294
unsigned routine_t
Definition: coroutine.h:54
void halt() override
virtual NodeStatus tick() override
Method to be implemented by the user.
Definition: action_node.cpp:45
CoroActionNode(const std::string &name, const NodeParameters &parameters=NodeParameters())
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
std::unique_ptr< Pimpl > _p
Definition: action_node.h:188
ActionNodeBase(const std::string &name, const NodeParameters &parameters=NodeParameters())
Definition: action_node.cpp:19
coroutine::routine_t coro
virtual NodeStatus executeTick() overridefinal
The method that will be executed to invoke tick(); and setStatus();.
Definition: action_node.cpp:95
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
AsyncActionNode(const std::string &name, const NodeParameters &parameters=NodeParameters())
Definition: action_node.cpp:65
TickEngine tick_engine_
Definition: action_node.h:132
virtual NodeStatus executeTick() override
The method that will be executed to invoke tick(); and setStatus();.
SyncActionNode(const std::string &name, const NodeParameters &parameters=NodeParameters())
NodeStatus status() const
Definition: tree_node.cpp:75
SimpleActionNode(const std::string &name, TickFunctor tick_functor, const NodeParameters &params=NodeParameters())
Definition: action_node.cpp:38
virtual ~CoroActionNode() override
ResumeResult resume(routine_t id)
Definition: coroutine.h:314
NodeStatus
Definition: basic_types.h:28
TickFunctor tick_functor_
Definition: action_node.h:96
BT::NodeStatus waitValidStatus()
Definition: tree_node.cpp:81
std::atomic< bool > loop_
Definition: action_node.h:134
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43
virtual ~AsyncActionNode() override
Definition: action_node.cpp:71


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