how to add 2 or more topics in one subscriber. c++

asked 2020-04-19 17:45:51 -0500

Sanjaya gravatar image

Please can you help me with this, I am new to ROS, I tried many methods. but every time I am getting errors please help me to build a subscriber with 2 or more topics.

edit retag flag offensive close merge delete

Comments

1

You cant add more than one topic in a subscriber.

You can define a custom msg(or data structure ) with all your data embedded in that topic and retrieve the data from that msg.

chrissunny94 gravatar image chrissunny94  ( 2020-04-20 03:04:31 -0500 )edit
1

Please be aware of message_filters, which may allow exactly what the OP is looking for.

You can define a custom msg(or data structure ) with all your data embedded in that topic

Such "union messages" are not necessarily the best choice, as they immediately make it impossible to use standard tools (which will not work with such a message).

gvdhoorn gravatar image gvdhoorn  ( 2020-04-20 03:06:24 -0500 )edit

I tried like like below code, i was able to get all add 2 or more topics, please can you tell me is there any error. PS I received the pub signal from both

class MinimalSubscriber1 : public rclcpp::Node
{
public:
  MinimalSubscriber1() : Node("minimal_subscriber")
  {
    subscription_1 = this->create_subscription<std_msgs::msg::String>(
      "topic1",
      10,
      [this](std_msgs::msg::String::UniquePtr msg) {
        RCLCPP_INFO(this->get_logger(), "I heard: Topic 1'%s'", msg->data.c_str());
      });

        subscription_2 = this->create_subscription<std_msgs::msg::String>(
      "topic2",
      10,
      [this](std_msgs::msg::String::UniquePtr msg) {
        RCLCPP_INFO(this->get_logger(), "I heard: Topic 1'%s'", msg->data.c_str());
      });

   }

private:
  rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_1; 
  rclcpp::Subscription<std_msgs::msg::String>
Sanjaya gravatar image Sanjaya  ( 2020-04-20 11:34:39 -0500 )edit