Skip to content

Commit d31eb1f

Browse files
committed
fix: Get last message on late subscription and ensure connection to correct subscriber on topic_name change
1 parent ac2e0ac commit d31eb1f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

behaviortree_ros2/include/behaviortree_ros2/bt_topic_sub_node.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class RosTopicSubNode : public BT::ConditionNode
6666
// The callback will broadcast to all the instances of RosTopicSubNode<T>
6767
auto callback = [this](const std::shared_ptr<TopicT> msg)
6868
{
69+
last_msg = msg;
6970
broadcaster(msg);
7071
};
7172
subscriber = node->create_subscription<TopicT>(topic_name, 1, callback, option);
@@ -75,6 +76,7 @@ class RosTopicSubNode : public BT::ConditionNode
7576
rclcpp::CallbackGroup::SharedPtr callback_group;
7677
rclcpp::executors::SingleThreadedExecutor callback_group_executor;
7778
boost::signals2::signal<void (const std::shared_ptr<TopicT>)> broadcaster;
79+
std::shared_ptr<TopicT> last_msg = nullptr;
7880

7981

8082
};
@@ -270,6 +272,11 @@ template<class T> inline
270272
sub_instance_ = it->second;
271273
}
272274

275+
// Check if there was a message received before the creation of this subscriber action
276+
if (sub_instance_.last_msg)
277+
{
278+
last_msg_ = sub_instance_.last_msg;
279+
}
273280

274281
// add "this" as received of the broadcaster
275282
signal_connection_ = sub_instance_->broadcaster.connect(
@@ -286,12 +293,18 @@ template<class T> inline
286293
// First, check if the subscriber_ is valid and that the name of the
287294
// topic_name in the port didn't change.
288295
// otherwise, create a new subscriber
296+
std::string topic_name;
297+
getInput("topic_name", topic_name);
298+
289299
if(!sub_instance_)
290300
{
291-
std::string topic_name;
292-
getInput("topic_name", topic_name);
293301
createSubscriber(topic_name);
294302
}
303+
else if(topic_name_ != topic_name)
304+
{
305+
sub_instance_.reset();
306+
createSubscriber(topic_name_);
307+
}
295308

296309
auto CheckStatus = [](NodeStatus status)
297310
{

0 commit comments

Comments
 (0)