Rosanswers logo

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.

 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 ?

Originally posted by Slachcrash on ROS Answers with karma: 3 on 2020-09-02

Post score: 0

Original comments

Comment by mgruhler on 2020-09-02:
Could it be that your nodehandle is private? I.e. nh_("~")? this would explain why the topic is put in the node namespace.

Otherwise, you could always remap...

Comment by Slachcrash on 2020-09-02:
Hello. Yes i had my nodehandle like this nh_ = ros::NodeHandle("~"); I removed the "~" and it worked. Thanks. But I'm afraid it will bring some problems with it. I have all my code written in node.cpp file. Can you explain the remap tool ? how can i write that in a correct way, because i tried it and it didn't work.

Comment by mgruhler on 2020-09-03:
posted as answer...


1 Answer 1


Rosanswers logo

Make sure to have your publishers and subscribers set up with the correct "namespace". For this, set the NodeHandle correctly. You are using a so-called "private" nodehandle. See the wiki for details.

Using NodeHandles correctly will not bring any problems, you just have to understand what they are and what they are used for. Good practice is to have all pubs/subs in the node namespace (i.e. ros::NodeHandle()) and all parameters in the private namespace (i.e. ros::NodeHandle("~")).

I'm not quite sure what you mean with "explain the remap tool". In the end, this just takes a topic name that is specified in code (the "from" part) and changes it to another (the "to" part). (The "from" needs to be exactly how the topic is named in your node.) So you don't have to change the code if you have, e.g. multiple drivers of the same type and want to now if that particular message is from driver A or driver B...

Originally posted by mgruhler with karma: 12390 on 2020-09-03

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by Slachcrash on 2020-09-03:
Thank you very much. This solved my issue. ;).


Your Answer

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