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

Subscribing to templated message type

asked 2016-02-02 21:42:36 -0500

djsw gravatar image

updated 2016-02-03 17:14:11 -0500

I have a templated class that accepts a message type as a parameter and a topic as an argument. I define the subscriber in the constructor:

    subscriber = n.subscribe(sub_topic, 1000, &TemplatedClass::callbackFunction, this);

and the callback function is defined as:

template<class T>
void TemplatedClass<T>::callbackFunction(const typename T::ConstPtr& incoming_msg) 
{
     ...
}

But the callback function is never called. When I subscribe to the topic from main using a subscriber that isn't from a templated class, the callback function is called, so the topic is being published to. I considered that it could be that the signature const typename ... might be causing issues, but of course the program won't compile without typename. Any ideas?


Turns out the issue was that I'd instantiated two subscribers in my constructor, but forgot to adjust the name of the second one. I had:

    subscriber1 = n.subscribe(sub_topic1, 1000, &TemplatedClass::callbackFunction1, this);
    subscriber1 = n.subscribe(sub_topic2, 1000, &TemplatedClass::callbackFunction2, this);

so subscriber1 was being overwritten and subscriber2 was never defined. My code was otherwise right, so I'm leaving it here in case anybody else needs the syntax for subscribers in templated classes.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-02-02 23:48:21 -0500

ahendrix gravatar image

Use rostopic or rqt_graph to introspect and make sure that your subscriber is actually subscribed to the right topic, and have a look at the publisher and subscriber logs to make sure that there isn't a topic type mismatch.

Also note that the subscriber only exists for as long as the subscriber object exists; if the subscriber object goes out of scope and is destructed, it unsubscribes from that topic.

edit flag offensive delete link more

Comments

It's not a scope issue - the class subscribes to two topics: one subscriber is const typename T::ConstPtr& and one is const std_msgs::Empty::ConstPtr&, and the second subscriber does exist. I checked with rqt_graph and rostopic info and the first subscriber does not exist.

djsw gravatar image djsw  ( 2016-02-03 16:58:04 -0500 )edit

Turns out it was a silly mistake. Thanks for the answer.

djsw gravatar image djsw  ( 2016-02-03 17:14:23 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-02-02 21:42:36 -0500

Seen: 873 times

Last updated: Feb 03 '16