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

Subscribe to a topic using an overloaded callback function

asked 2011-10-18 04:23:17 -0500

toniOliver gravatar image

updated 2011-10-18 05:04:41 -0500

I get a compilation error when trying to subscribe to a ROS topic using an overloaded callback function. Look like the compiler doesn't know what function to pick.

Is there any way to solve it?

At compiling time I don't know which of several topics will my node subscribe to. The types of the possible topics are different, so I need different functions to receive the data. Is there any way of knowing the data type of a topic other than calling 'rostopic type [topic]' inside the node?

In the following snippet I want to subscribe to the "inputs" topic, but I don't know which topic will be mapped to it from the command line. I want to consider two possible topics with different data type "sr_robot_msgs::JointControllerState" and "pr2_controllers_msgs::JointControllerState", so how can I retrieve this data type information from the topic in runtime?

    MovementPublisher::MovementPublisher(double min_value, double max_value,
                                   double rate, unsigned int repetition, unsigned int nb_mvt_step, std::string controller_type)
{
...
sub_ = nh_tilde.subscribe("inputs", nb_mvt_step, &MovementPublisher::calculateErrorCallback, this);
}

void MovementPublisher::calculateErrorCallback(const sr_robot_msgs::JointControllerState::ConstPtr& msg)
{
double error = msg->set_point - msg->process_value;
ROS_DEBUG_STREAM("Error: " << error);
}

void MovementPublisher::pr2_calculateErrorCallback(const pr2_controllers_msgs::JointControllerState::ConstPtr& msg)
{
double error = msg->set_point - msg->process_value;
ROS_DEBUG_STREAM("Error: " << error);
}
edit retag flag offensive close merge delete

Comments

Can you edit your answer to add a code snippet as an example? They problem with strongly typed languages is that you can't play with the types at run time. You could play with an if-then-else, but I need a sample code to work with.
Lorenzo Riano gravatar image Lorenzo Riano  ( 2011-10-18 04:41:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2011-10-18 05:51:46 -0500

updated 2011-10-19 01:02:45 -0500

Unfortunately you can't do this in C++.

First of all, if you subscribe to a topic with message type A and receive message type B, ROS will complain about the MD5 sum.

Even if it was possible, your approach can't do any real-time checking, as the C++ templates (implemented in the subscriber) create functions/classes at compile time.

So the main problem is that C++ requires the declaration of the callback function signature at compile time. AFAIK there is no general "Message" class from which all the messages inherit, so polymorphism is not an option.

This example seems to arise from the fact that "sr_robot_msgs::JointControllerState" and "pr2_controllers_msgs::JointControllerState" are basically the same message, so why two different types?

edit flag offensive delete link more

Comments

Can the following thread be of any help regarding runtime checking? http://answers.ros.org/question/804/how-to-obtain-the-msg-format-of-a-topic-on-runtime
Yianni gravatar image Yianni  ( 2011-10-19 00:42:58 -0500 )edit
Right, so Python obviously can! I have edited my answer to reflect this.
Lorenzo Riano gravatar image Lorenzo Riano  ( 2011-10-19 00:58:33 -0500 )edit
Thank you. Finally I added a parameter in the node invocation that tells which format will the topic have. In this case I find it preferable to asking ROS about the data type of the topic.
toniOliver gravatar image toniOliver  ( 2011-10-20 02:59:20 -0500 )edit
Good, that's a sound solution. You might want to post the solution and mark this question as "solved", so future users will know the answer.
Lorenzo Riano gravatar image Lorenzo Riano  ( 2011-10-20 03:51:53 -0500 )edit

Question Tools

Stats

Asked: 2011-10-18 04:23:17 -0500

Seen: 2,058 times

Last updated: Oct 19 '11