From 0a661111c9696ace57ab545a23b58cd366eb312f Mon Sep 17 00:00:00 2001 From: Nahuel Espinosa Date: Mon, 15 May 2023 12:32:46 -0300 Subject: [PATCH] Configure timer with common callback group Signed-off-by: Nahuel Espinosa --- beluga_amcl/src/amcl_node.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/beluga_amcl/src/amcl_node.cpp b/beluga_amcl/src/amcl_node.cpp index 3536fdf20..678cd4f37 100644 --- a/beluga_amcl/src/amcl_node.cpp +++ b/beluga_amcl/src/amcl_node.cpp @@ -576,9 +576,6 @@ AmclNode::CallbackReturn AmclNode::on_configure(const rclcpp_lifecycle::State &) { RCLCPP_INFO(get_logger(), "Configuring"); - using namespace std::chrono_literals; - timer_ = create_wall_timer(200ms, std::bind(&AmclNode::timer_callback, this)); - particle_cloud_pub_ = create_publisher( "particle_cloud", rclcpp::SensorDataQoS()); @@ -604,9 +601,19 @@ AmclNode::CallbackReturn AmclNode::on_activate(const rclcpp_lifecycle::State &) RCLCPP_INFO(get_logger(), "Created bond (%s) to lifecycle manager", get_name()); } - auto main_callback_group = create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); + // Accessing the particle filter is not thread safe. + // This ensures that different callbacks are not called concurrently. + auto common_callback_group = create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); auto common_subscription_options = rclcpp::SubscriptionOptions{}; - common_subscription_options.callback_group = main_callback_group; + common_subscription_options.callback_group = common_callback_group; + + { + using namespace std::chrono_literals; + timer_ = create_wall_timer( + 200ms, + std::bind(&AmclNode::timer_callback, this), + common_callback_group); + } { map_sub_ = create_subscription( @@ -676,7 +683,7 @@ AmclNode::CallbackReturn AmclNode::on_activate(const rclcpp_lifecycle::State &) std::placeholders::_2, std::placeholders::_3), rmw_qos_profile_services_default, - main_callback_group); + common_callback_group); RCLCPP_INFO(get_logger(), "Created reinitialize_global_localization service"); }