Skip to content

Commit 13c30c8

Browse files
committed
fix wakeup
1 parent 2408947 commit 13c30c8

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

include/behaviortree_cpp_v3/bt_factory.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,24 @@ class Tree
143143
Tree(Tree&& other)
144144
{
145145
(*this) = std::move(other);
146-
for(auto& node: nodes)
147-
{
148-
node->setWakeUpInstance( &wake_up_ );
149-
}
150146
}
151147

152148
Tree& operator=(Tree&& other)
153149
{
154150
nodes = std::move(other.nodes);
155151
blackboard_stack = std::move(other.blackboard_stack);
156152
manifests = std::move(other.manifests);
153+
wake_up_ = other.wake_up_;
154+
return *this;
155+
}
157156

157+
void initialize()
158+
{
159+
wake_up_ = std::make_shared<WakeUpSignal>();
158160
for(auto& node: nodes)
159161
{
160-
node->setWakeUpInstance( &wake_up_ );
162+
node->setWakeUpInstance(wake_up_);
161163
}
162-
return *this;
163164
}
164165

165166
void haltTree()
@@ -183,11 +184,16 @@ class Tree
183184

184185
TreeNode* rootNode() const
185186
{
186-
return nodes.empty() ? nullptr : nodes.front().get();
187+
return nodes.empty() ? nullptr : nodes.front().get();
187188
}
188189

189190
NodeStatus tickRoot()
190-
{
191+
{
192+
if(!wake_up_)
193+
{
194+
initialize();
195+
}
196+
191197
if(!rootNode())
192198
{
193199
throw RuntimeError("Empty Tree");
@@ -209,7 +215,7 @@ class Tree
209215
Blackboard::Ptr rootBlackboard();
210216

211217
private:
212-
WakeUpSignal wake_up_;
218+
std::shared_ptr<WakeUpSignal> wake_up_;
213219
};
214220

215221
class Parser;

include/behaviortree_cpp_v3/tree_node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class TreeNode
190190
// Only BehaviorTreeFactory should call this
191191
void setRegistrationID(StringView ID);
192192

193-
void setWakeUpInstance(WakeUpSignal* instance);
193+
void setWakeUpInstance(std::shared_ptr<WakeUpSignal> instance);
194194

195195
void modifyPortsRemapping(const PortsRemapping& new_remapping);
196196

@@ -217,7 +217,7 @@ class TreeNode
217217

218218
PostTickOverrideCallback post_condition_callback_;
219219

220-
WakeUpSignal* wake_up_ = nullptr;
220+
std::shared_ptr<WakeUpSignal> wake_up_;
221221
};
222222

223223
//-------------------------------------------------------

include/behaviortree_cpp_v3/utils/wakeup_signal.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
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-
141
#ifndef BEHAVIORTREECORE_WAKEUP_SIGNAL_HPP
152
#define BEHAVIORTREECORE_WAKEUP_SIGNAL_HPP
163

@@ -29,7 +16,9 @@ class WakeUpSignal
2916
{
3017
std::unique_lock<std::mutex> lk(mutex_);
3118
ready_ = false;
32-
return cv_.wait_for(lk, tm, [this]{ return ready_; });
19+
return cv_.wait_for(lk, tm, [this]{
20+
return ready_;
21+
});
3322
}
3423

3524
void emitSignal()

src/bt_factory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Tree BehaviorTreeFactory::createTree(const std::string &tree_name, Blackboard::P
296296

297297
void Tree::sleep(std::chrono::system_clock::duration timeout)
298298
{
299-
wake_up_.waitFor(timeout);
299+
wake_up_->waitFor(timeout);
300300
}
301301

302302
Tree::~Tree()

src/tree_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void TreeNode::setRegistrationID(StringView ID)
194194
registration_ID_.assign(ID.data(), ID.size());
195195
}
196196

197-
void TreeNode::setWakeUpInstance(WakeUpSignal *instance)
197+
void TreeNode::setWakeUpInstance(std::shared_ptr<WakeUpSignal> instance)
198198
{
199199
wake_up_ = instance;
200200
}

src/xml_parsing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ Tree XMLParser::instantiateTree(const Blackboard::Ptr& root_blackboard,
471471
output_tree,
472472
root_blackboard,
473473
TreeNode::Ptr() );
474+
output_tree.initialize();
474475
return output_tree;
475476
}
476477

tests/gtest_wakeup.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <gtest/gtest.h>
22
#include "behaviortree_cpp_v3/bt_factory.h"
3-
#include "../sample_nodes/dummy_nodes.h"
43

54
using namespace BT;
65

0 commit comments

Comments
 (0)