Repository navigation
Expand file tree
/
Copy pathaction_node.cpp
More file actions
291 lines (252 loc) · 7.42 KB
/
Copy pathaction_node.cpp
File metadata and controls
291 lines (252 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
* Copyright (C) 2018-2025 Davide Faconti - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* 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:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* 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,
* 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.
*/
#define MINICORO_IMPL
#include "behaviortree_cpp/action_node.h"
#include "behaviortree_cpp/basic_types.h"
// minicoro.h includes this inside extern "C": include it first, at global scope.
#ifdef __EMSCRIPTEN__
#include <emscripten/fiber.h>
#endif
#include "minicoro.h"
#include <exception>
#include <iostream>
using namespace BT;
ActionNodeBase::ActionNodeBase(const std::string& name, const NodeConfig& config)
: LeafNode::LeafNode(name, config)
{}
//-------------------------------------------------------
SimpleActionNode::SimpleActionNode(const std::string& name,
SimpleActionNode::TickFunctor tick_functor,
const NodeConfig& config)
: SyncActionNode(name, config), tick_functor_(std::move(tick_functor))
{}
NodeStatus SimpleActionNode::tick()
{
NodeStatus prev_status = status();
if(prev_status == NodeStatus::IDLE)
{
setStatus(NodeStatus::RUNNING);
prev_status = NodeStatus::RUNNING;
}
const NodeStatus status = tick_functor_(*this);
if(status != prev_status)
{
setStatus(status);
}
return status;
}
//-------------------------------------------------------
SyncActionNode::SyncActionNode(const std::string& name, const NodeConfig& config)
: ActionNodeBase(name, config)
{}
NodeStatus SyncActionNode::executeTick()
{
auto stat = ActionNodeBase::executeTick();
if(stat == NodeStatus::RUNNING)
{
throw LogicError("SyncActionNode MUST never return RUNNING");
}
return stat;
}
//-------------------------------------
struct CoroActionNode::Pimpl
{
mco_coro* coro = nullptr;
mco_desc desc = {};
};
namespace
{
void CoroEntry(mco_coro* co)
{
static_cast<CoroActionNode*>(co->user_data)->tickImpl();
}
} // namespace
CoroActionNode::CoroActionNode(const std::string& name, const NodeConfig& config)
: ActionNodeBase(name, config), _p(new Pimpl)
{}
CoroActionNode::~CoroActionNode()
{
try
{
destroyCoroutine();
}
catch(const std::exception& ex)
{
std::cerr << "Exception in ~CoroActionNode(): " << ex.what() << std::endl;
}
}
void CoroActionNode::setStatusRunningAndYield()
{
setStatus(NodeStatus::RUNNING);
mco_yield(_p->coro);
}
NodeStatus CoroActionNode::executeTick()
{
// create a new coroutine, if necessary
if(_p->coro == nullptr)
{
// First initialize a `desc` object through `mco_desc_init`.
_p->desc = mco_desc_init(CoroEntry, 0);
_p->desc.user_data = this;
const mco_result res = mco_create(&_p->coro, &_p->desc);
if(res != MCO_SUCCESS)
{
throw RuntimeError("Can't create coroutine");
}
}
//------------------------
// execute the coroutine
mco_resume(_p->coro);
//------------------------
// check if the coroutine finished. In this case, destroy it
if(mco_status(_p->coro) == MCO_DEAD)
{
destroyCoroutine();
}
return status();
}
void CoroActionNode::tickImpl()
{
setStatus(TreeNode::executeTick());
}
void CoroActionNode::halt()
{
destroyCoroutine();
resetStatus(); // might be redundant
}
void CoroActionNode::destroyCoroutine()
{
if(_p->coro != nullptr)
{
const mco_result res = mco_destroy(_p->coro);
if(res != MCO_SUCCESS)
{
throw RuntimeError("Can't destroy coroutine");
}
_p->coro = nullptr;
}
}
bool StatefulActionNode::isHaltRequested() const
{
return halt_requested_.load();
}
NodeStatus StatefulActionNode::tick()
{
const NodeStatus prev_status = status();
if(prev_status == NodeStatus::IDLE)
{
const NodeStatus new_status = onStart();
if(new_status == NodeStatus::IDLE)
{
throw LogicError("StatefulActionNode::onStart() must not return IDLE");
}
return new_status;
}
//------------------------------------------
if(prev_status == NodeStatus::RUNNING)
{
const NodeStatus new_status = onRunning();
if(new_status == NodeStatus::IDLE)
{
throw LogicError("StatefulActionNode::onRunning() must not return IDLE");
}
return new_status;
}
return prev_status;
}
void StatefulActionNode::halt()
{
halt_requested_.store(true);
if(status() == NodeStatus::RUNNING)
{
onHalted();
}
resetStatus(); // might be redundant
}
NodeStatus BT::ThreadedAction::executeTick()
{
using lock_type = std::unique_lock<std::mutex>;
// This method overrides TreeNode::executeTick(), so it must handle the
// pre-conditions (_skipIf, _failureIf, _successIf, _while) and the
// post-conditions itself, as TreeNode::executeTick() does.
if(auto precond = checkPreConditions())
{
const NodeStatus precond_status = precond.value();
if(isStatusCompleted(precond_status))
{
checkPostConditions(precond_status);
}
// preserve the IDLE state if skipped, but communicate SKIPPED to parent
if(precond_status != NodeStatus::SKIPPED)
{
setStatus(precond_status);
}
return precond_status;
}
//send signal to other thread.
// The other thread is in charge for changing the status
if(status() == NodeStatus::IDLE)
{
setStatus(NodeStatus::RUNNING);
halt_requested_ = false;
thread_handle_ = std::async(std::launch::async, [this]() {
try
{
auto status = tick();
if(!isHaltRequested())
{
setStatus(status);
}
}
catch(std::exception&)
{
std::cerr << "\nUncaught exception from tick(): [" << registrationName() << "/"
<< name() << "]\n"
<< std::endl;
// Set the exception pointer and the status atomically.
const lock_type l(mutex_);
exptr_ = std::current_exception();
setStatus(BT::NodeStatus::IDLE);
}
emitWakeUpSignal();
});
}
NodeStatus new_status = NodeStatus::IDLE;
{
const lock_type l(mutex_);
if(exptr_)
{
// The official interface of std::exception_ptr does not define any move
// semantics. Thus, we copy and reset exptr_ manually.
const auto exptr_copy = exptr_;
exptr_ = nullptr;
std::rethrow_exception(exptr_copy);
}
new_status = status();
}
if(isStatusCompleted(new_status))
{
checkPostConditions(new_status);
}
return new_status;
}
void ThreadedAction::halt()
{
halt_requested_.store(true);
if(thread_handle_.valid())
{
thread_handle_.wait();
}
thread_handle_ = {};
resetStatus(); // might be redundant
}