ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

the same msg type of multiple topic names can use only one callback on ros2?

asked 2023-01-09 21:14:36 -0500

jxl gravatar image

updated 2023-01-10 01:09:19 -0500

ljaniec gravatar image

Hello, I have /camera_1, /camera_2, /camera_3 and /camera_4 which are published by 4 client nodes. The 4 topics process are all the same (save received msg into one buffer variable ). Could I bind only one callback to 4 rclcpp::Subscription objects? Thanks for your help a lot!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2023-02-05 21:12:06 -0500

jxl gravatar image
        m_client_topic_subs_.clear(); //std::vector<rclcpp::SubscriptionBase::SharedPtr> m_client_topic_subs_;
        for(int i = 0; i < client_robots_num; i++){
            auto topic_name = dynamicmap_topic_name + std::to_string(i);
            std::function<void(const nav_interfaces::msg::DynamicMap::SharedPtr)> callback = 
                 std::bind(&DynamicMapManager::dynamicMapCB, this, std::placeholders::_1, topic_name);
            auto sub = m_node->create_subscription<nav_interfaces::msg::DynamicMap>(topic_name, 10, callback);
            m_client_topic_subs_.push_back(sub);
        }

callback defination is here, in callback we can print topic_name, it comes from 4 cameras.

void DynamicMapManager::dynamicMapCB(const nav_interfaces::msg::DynamicMap::SharedPtr dynamic_map_ptr, const std::string& topic_name)
edit flag offensive delete link more
2

answered 2023-01-10 01:19:18 -0500

tfoote gravatar image

Yes, you can use the same callback function for multiple subscriptions.

edit flag offensive delete link more
0

answered 2023-01-10 02:36:29 -0500

ljaniec gravatar image

Yes, it is possible. Does your buffer variable have a mutex lock for concurrent writing? If no, maybe you should use callback groups:

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-01-09 21:14:36 -0500

Seen: 122 times

Last updated: Feb 05 '23