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

Subscribing to the wrong Topic

asked 2020-09-02 05:53:54 -0500

Slachcrash gravatar image

updated 2022-05-23 09:20:55 -0500

lucasw gravatar image

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 ?

edit retag flag offensive close merge delete

Comments

1

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...

mgruhler gravatar image mgruhler  ( 2020-09-02 06:26:33 -0500 )edit

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.

Slachcrash gravatar image Slachcrash  ( 2020-09-02 10:22:11 -0500 )edit

posted as answer...

mgruhler gravatar image mgruhler  ( 2020-09-03 04:27:34 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-09-03 04:27:25 -0500

mgruhler gravatar image

updated 2020-09-03 04:28:26 -0500

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...

edit flag offensive delete link more

Comments

Thank you very much. This solved my issue. ;).

Slachcrash gravatar image Slachcrash  ( 2020-09-03 04:45:27 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-09-02 05:53:54 -0500

Seen: 232 times

Last updated: Sep 05 '20