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

Callback class member

asked 2013-06-27 00:46:29 -0500

Verane gravatar image

updated 2014-01-28 17:17:03 -0500

ngrennan gravatar image

Hi. Is it possible to use a member class method as a callback? Something like this:

void Node::chatterCallback(const std_msgs::String::ConstPtr& msg){
ROS_INFO("I heard: [%s]", msg->data.c_str());
}

void Node::Subscribe(){
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("alives", 1000, &Node::chatterCallback, this);
}

At the examples (http://www.ros.org/wiki/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks) , I've seen:

Listener listener;
ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, &listener);

But I don't want to instantiate another object. I've the callback function as a method in my class.

edit retag flag offensive close merge delete

Comments

I think it is possible, as I do it with Python. Just try and see. Just found this question : answers.ros.org/question/64446/problem-using-class-function-as-callback/ which might include the answer you're looking for

Lucile gravatar image Lucile  ( 2013-06-27 01:55:33 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2013-06-27 05:09:14 -0500

From your question, it seems like you already know that it's possible to use class methods as callbacks. Your question seems to be, more specifically: "Is it possible to use methods of the current class object from which subscribe() is called?". In other words, can you pass in this as the class object parameter to the subscribe call.

The answer to that question is also yes. ROS doesn't care where the object comes from. The turtlesim turtle class creates a subscriber with a local member-function callback, just as you are asking about (see here).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-27 00:46:29 -0500

Seen: 1,595 times

Last updated: Jun 27 '13