Search code examples
c++ros

Subscribing to the wrong topic


My problem is that I want to subscribe to the Topic "joy" but from a class.

Let's say that i have a class named My_Class.

As protected method i wrote the subscriber.

protected:
   
 ros::Subscriber my_subscriber;

Then i iniatlized it.

my_subscriber = nh_.subscribe("joy", 1, &My_Class::my_subscriber_cb, this);

Then i wrote my Callback Function

void my_subscriber_cb(const sensor_msgs::Joy::ConstPtr& msg){}

Then i initialised my node like this:

    // ROS-Node Initialisation
    ros::init(argc, argv, "TEST_CORE", ros::init_options::NoSigintHandler);

    

The problem is that at the end when i show the topic with rostopic list

I get a topic named TEST_CORE/joy. So I'm subscribing to TEST_CORE/joy and not to /joy .

What can i do or change to get the Data of my gamepad from the right Topic ?


Solution

  • Check out the documentation at ROS Wiki: Names. Names are generally relative, this means they are created based on the node name. Since your node name is TEST_CORE, the result of a subscription to joy is TEST_CORE/joy. To subscribe to a absolute topic, you need to add / in front of the topic. So

    my_subscriber = nh_.subscribe("/joy", 1, &My_Class::my_subscriber_cb, this);
    

    allows the subsctiption which is required by you.