Subscribe to a topic using an overloaded callback function
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);
}