Skip to content

Commit 47b1ffe

Browse files
committed
quick and dirty solution
1 parent 14edd54 commit 47b1ffe

File tree

9 files changed

+155
-17
lines changed

9 files changed

+155
-17
lines changed

include/behaviortree_cpp/control_node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ControlNode : public TreeNode
5050
{
5151
return NodeType::CONTROL;
5252
}
53+
void propagateHalt(unsigned i);
5354
};
5455
}
5556

include/behaviortree_cpp/tree_node.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
1+
/* Copyright (C) 2015-2019 Michele Colledanchise - All Rights Reserved
22
* Copyright (C) 2018-2019 Davide Faconti, Eurecat - All Rights Reserved
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
@@ -21,6 +21,7 @@
2121
#include "behaviortree_cpp/basic_types.h"
2222
#include "behaviortree_cpp/blackboard.h"
2323
#include "behaviortree_cpp/utils/strcat.hpp"
24+
//#include <behaviortree_cpp/control_node.h>
2425

2526
#ifdef _MSC_VER
2627
#pragma warning(disable : 4127)
@@ -147,8 +148,13 @@ class TreeNode
147148
static StringView stripBlackboardPointer(StringView str);
148149

149150
static Optional<StringView> getRemappedKey(StringView port_name, StringView remapping_value);
151+
void set_parent_prt(TreeNode *parent_ptr);
150152

151-
protected:
153+
TreeNode* parent_prt() const;
154+
unsigned int child_index() const;
155+
void set_child_index(unsigned int child_index);
156+
157+
protected:
152158
/// Method to be implemented by the user
153159
virtual BT::NodeStatus tick() = 0;
154160

@@ -161,6 +167,9 @@ class TreeNode
161167
}
162168

163169
void modifyPortsRemapping(const PortsRemapping& new_remapping);
170+
unsigned int child_index_;
171+
172+
TreeNode* parent_prt_ = nullptr;
164173

165174
private:
166175
const std::string name_;
@@ -178,6 +187,8 @@ class TreeNode
178187
NodeConfiguration config_;
179188

180189
std::string registration_ID_;
190+
191+
181192
};
182193

183194
//-------------------------------------------------------

src/control_node.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ ControlNode::ControlNode(const std::string& name, const NodeConfiguration& confi
2222

2323
void ControlNode::addChild(TreeNode* child)
2424
{
25+
child->set_child_index(children_nodes_.size());
26+
child->set_parent_prt(this);
2527
children_nodes_.push_back(child);
2628
}
2729

30+
31+
32+
2833
unsigned ControlNode::childrenCount() const
2934
{
3035
return unsigned(children_nodes_.size());
@@ -54,4 +59,16 @@ void ControlNode::haltChildren(unsigned i)
5459
}
5560
}
5661

62+
void ControlNode::propagateHalt(unsigned i)
63+
{
64+
// std::cout << name() << " received a request to propagate halt from index " << i + 1 << std::endl;
65+
66+
haltChildren(i + 1);
67+
if (parent_prt_ != nullptr)
68+
{
69+
((ControlNode*)parent_prt_)->propagateHalt(child_index_);
70+
}
71+
}
72+
73+
5774
} // end namespace

src/controls/reactive_fallback.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,27 @@ NodeStatus ReactiveFallback::tick()
3434
if (current_child_node->status() != NodeStatus::RUNNING)
3535
{
3636
// if not running already, tick it
37+
38+
if (parent_prt_ == nullptr)
39+
{
40+
// std::cout << name() << " is root" << std::endl;
41+
42+
}
43+
else
44+
{
45+
46+
// std::cout << name() << " is propagating halt to" << ((ControlNode*)parent_prt_)->name() << std::endl;
47+
48+
49+
((ControlNode*)parent_prt_)->propagateHalt(child_index_);
50+
}
51+
52+
53+
3754
current_child_node->executeTick();
3855
do
3956
{
57+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
4058

4159
child_status = current_child_node->status();
4260

src/controls/reactive_sequence.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,27 @@ NodeStatus ReactiveSequence::tick()
3737
if (current_child_node->status() != NodeStatus::RUNNING)
3838
{
3939
// if not running already, tick it
40+
//before ticking it, make sure to halt the other running child.
41+
42+
if (parent_prt_ == nullptr)
43+
{
44+
// std::cout << name() << " is root" << std::endl;
45+
46+
}
47+
else
48+
{
49+
50+
// std::cout << name() << " is propagating halt to" << ((ControlNode*)parent_prt_)->name() << std::endl;
51+
52+
53+
((ControlNode*)parent_prt_)->propagateHalt(child_index_);
54+
}
55+
4056
current_child_node->executeTick();
4157
do
4258
{
59+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
60+
4361
child_status = current_child_node->status();
4462
} while (child_status == NodeStatus::IDLE);
4563
}

src/tree_node.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
1+
/* Copyright (C) 2015-2019 Michele Colledanchise - All Rights Reserved
22
* Copyright (C) 2018-2019 Davide Faconti, Eurecat - All Rights Reserved
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
@@ -13,6 +13,7 @@
1313

1414
#include "behaviortree_cpp/tree_node.h"
1515
#include <cstring>
16+
//#include <behaviortree_cpp/control_node.h>
1617

1718
namespace BT
1819
{
@@ -161,4 +162,24 @@ void TreeNode::modifyPortsRemapping(const PortsRemapping &new_remapping)
161162
}
162163
}
163164

165+
unsigned int TreeNode::child_index() const
166+
{
167+
return child_index_;
168+
}
169+
170+
void TreeNode::set_child_index(unsigned int child_index)
171+
{
172+
child_index_ = child_index;
173+
}
174+
175+
void TreeNode::set_parent_prt(TreeNode *parent_ptr)
176+
{
177+
parent_prt_ = parent_ptr;
178+
}
179+
180+
TreeNode *TreeNode::parent_prt() const
181+
{
182+
return parent_prt_;
183+
}
184+
164185
} // end namespace

tests/gtest_reactive_tree.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ struct ComplexReactiveTree : testing::Test
6464
TEST_F(ComplexReactiveTree, ConditionsFalse)
6565
{
6666

67+
68+
// std::cout << fal_1.name() << " is the child of " << fal_1.parent_prt_->name() << std::endl;
69+
// std::cout << fal_2.name() << " is the child of " << fal_2.parent_prt_->name() << std::endl;
70+
71+
// std::cout << action_1.name() << " is the child of " << action_1.parent_prt_->name() << std::endl;
72+
// std::cout << action_2.name() << " is the child of " << action_2.parent_prt_->name() << std::endl;
73+
74+
// std::cout << condition_1.name() << " is the child of " << condition_1.parent_prt_->name() << std::endl;
75+
// std::cout << condition_2.name() << " is the child of " << condition_2.parent_prt_->name() << std::endl;
76+
77+
78+
if (root.parent_prt() == nullptr)
79+
{
80+
std::cout << root.name() << " is root" << std::endl;
81+
82+
}
83+
else
84+
{
85+
std::cout << root.name() << " is the child of " << root.parent_prt()->name() << std::endl;
86+
}
87+
88+
89+
6790
auto t0 = std::chrono::high_resolution_clock::now();
6891

6992

@@ -82,6 +105,8 @@ TEST_F(ComplexReactiveTree, ConditionsFalse)
82105

83106
std::this_thread::sleep_for(milliseconds(300));
84107

108+
std::cout << "condition 1 set to true" << std::endl;
109+
85110
condition_1.setBoolean(true);
86111

87112
state = root.executeTick();
@@ -98,16 +123,20 @@ TEST_F(ComplexReactiveTree, ConditionsFalse)
98123

99124
std::this_thread::sleep_for(milliseconds(300));
100125
condition_1.setBoolean(false);
101-
std::cout << "Ticking..." << std::endl;
126+
127+
std::cout << "condition 1 set to false" << std::endl;
128+
129+
130+
// std::cout << "Ticking..." << std::endl;
102131
state = root.executeTick();
103-
std::cout << "...done" << std::endl;
132+
// std::cout << "...done" << std::endl;
104133

105134

106135

107136
std::this_thread::sleep_for(milliseconds(300));
108137

109-
std::cout << action_1.startTimePoint().time_since_epoch().count() << std::endl;
110-
std::cout << action_2.stopTimePoint().time_since_epoch().count() << std::endl;
138+
// std::cout << action_1.startTimePoint().time_since_epoch().count() << std::endl;
139+
// std::cout << action_2.stopTimePoint().time_since_epoch().count() << std::endl;
111140

112141
ASSERT_TRUE(action_1.startTimePoint().time_since_epoch().count() >
113142
action_2.stopTimePoint().time_since_epoch().count() );

tests/include/action_test_node.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <ctime>
55
#include <chrono>
66
#include "behaviortree_cpp/action_node.h"
7+
#include <mutex>
78

89
namespace BT
910
{
@@ -60,29 +61,33 @@ class AsyncActionTest : public AsyncActionNode
6061

6162
void setStartTimePoint(std::chrono::high_resolution_clock::time_point now)
6263
{
63-
// TODO add lock guard
64-
std::cout << this->name() << " Setting Start Time: " << now.time_since_epoch().count() << std::endl;
64+
std::lock_guard<std::mutex> lock(start_time_mutex_);
65+
// std::cout << this->name() << " Setting Start Time: " << now.time_since_epoch().count() << std::endl;
6566

6667
start_time_ = now;
6768
}
6869

69-
std::chrono::high_resolution_clock::time_point startTimePoint() const
70+
std::chrono::high_resolution_clock::time_point startTimePoint()
7071
{
72+
std::lock_guard<std::mutex> lock(start_time_mutex_);
73+
7174
return start_time_;
7275
}
7376

7477

7578
void setStopTimePoint(std::chrono::high_resolution_clock::time_point now)
7679
{
77-
// TODO add lock guard
80+
std::lock_guard<std::mutex> lock(stop_time_mutex_);
7881

79-
std::cout << this->name() << " Setting Stop Time: " << now.time_since_epoch().count() << std::endl;
82+
// std::cout << this->name() << " Setting Stop Time: " << now.time_since_epoch().count() << std::endl;
8083

8184
stop_time_ = now;
8285
}
8386

84-
std::chrono::high_resolution_clock::time_point stopTimePoint() const
87+
std::chrono::high_resolution_clock::time_point stopTimePoint()
8588
{
89+
std::lock_guard<std::mutex> lock(stop_time_mutex_);
90+
8691
return stop_time_;
8792
}
8893

@@ -94,6 +99,9 @@ class AsyncActionTest : public AsyncActionNode
9499
std::atomic<int> tick_count_;
95100
std::atomic_bool stop_loop_;
96101
std::chrono::high_resolution_clock::time_point start_time_, stop_time_;
102+
103+
std::mutex start_time_mutex_, stop_time_mutex_;
104+
97105
};
98106
}
99107

tests/src/action_test_node.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ BT::AsyncActionTest::~AsyncActionTest()
3636

3737
BT::NodeStatus BT::AsyncActionTest::tick()
3838
{
39-
setStartTimePoint(high_resolution_clock::now());
39+
40+
high_resolution_clock::time_point t2 = high_resolution_clock::now();
41+
42+
// std::cout << name() << " STARTED at" << t2.time_since_epoch().count() << std::endl;
43+
44+
setStartTimePoint(t2);
4045
setStatus(NodeStatus::RUNNING);
4146
using std::chrono::high_resolution_clock;
4247
tick_count_++;
@@ -46,12 +51,10 @@ BT::NodeStatus BT::AsyncActionTest::tick()
4651

4752
while (!stop_loop_ && high_resolution_clock::now() < initial_time + time_)
4853
{
49-
// high_resolution_clock::time_point t2 = high_resolution_clock::now();
50-
51-
// std::cout << this->name() << " Running. Time: " << std::chrono::system_clock::to_time_t(t2) << std::endl;
5254

5355

5456
std::this_thread::sleep_for(std::chrono::milliseconds(100));
57+
5558
}
5659

5760
setStopTimePoint(high_resolution_clock::now());
@@ -70,7 +73,19 @@ BT::NodeStatus BT::AsyncActionTest::tick()
7073

7174
void BT::AsyncActionTest::halt()
7275
{
76+
high_resolution_clock::time_point t2 = high_resolution_clock::now();
77+
78+
// std::cout << name() << " HALTED at " << t2.time_since_epoch().count() << std::endl;
79+
7380
stop_loop_ = true;
81+
NodeStatus node_status;
82+
do
83+
{
84+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
85+
86+
node_status = status();
87+
} while (node_status == NodeStatus::RUNNING);
88+
7489
}
7590

7691
void BT::AsyncActionTest::setTime(BT::Duration time)

0 commit comments

Comments
 (0)