0
$\begingroup$

Rosanswers logo

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!


Originally posted by jxl on ROS Answers with karma: 252 on 2023-01-09

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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


Originally posted by tfoote with karma: 58457 on 2023-01-10

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$
0
$\begingroup$

Rosanswers logo

        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)

Originally posted by jxl with karma: 252 on 2023-02-05

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.