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

implicit change of message type on callback function

asked 2018-04-26 15:56:51 -0500

vbs gravatar image

Hello,

I have a custom cpp class that works well outside of ROS. Now, I would like to use one of the methods in this class as the callback function for a ros::Subscriber. However, to leave the class untouched and the method generic, I'll need some type conversion from the topic's message type to the method's argument type. Say, from std_msgs::String::ConstPtr to std::string.

Is it possible to do this implicitly during the subscriber declaration? If so, how?

Thanks in advance for any help.

edit retag flag offensive close merge delete

Comments

robotchicken is right. The way to do this is with a wrapper function or class. This is a limitation of the C++ language itself and not ROS.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-04-27 05:11:03 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2018-04-26 16:56:49 -0500

robotchicken gravatar image

updated 2018-04-26 23:31:03 -0500

From a quick glance at the ROS API, I don't think there is a way to do it implicitly. I guess its more of a C++ function redefinition functionaility than a ROS functionaility.

You could create a class that inherits from the class outside ROS.

class ClassInsideROS: public ClassOutsideROS {
public:
    ClassInsideROS{
      // Parent Constructor goes here
      }

    void new_callback(std_msgs::String::ConstPtr msg)  {
        std::string msg_string;
        // Perform Type Conversion from std_msgs::String::ConstPtr to
        old_callback(msg_string)
     }
};

 void main() {
   // Register Callback
   ros::NodeHandle n;
   ClassInsideROSobj;
   ros::Subscriber sub = n.subscribe(topic, 1000, &ClassInsideROS::new_callback, &obj);
}

But if all you need is the member function and no other properties of the class, you could just create a function.

ClassOutsideROS obj_;

void new_callback(std_msgs::String::ConstPtr msg)  {  
   std::string msg_string;
   // Perform Type Conversion from std_msgs::String::ConstPtr to
   obj_.callback(msg_string) 
}

void main() {
  // Register Callback
   ros::NodeHandle n;
   InsideROS obj;
   ros::Subscriber sub = n.subscribe(topic, 1000, &ClassOutsideROS::new_callback, &obj);
}
edit flag offensive delete link more

Comments

Thank you for your reply. Indeed, that is the same solution I thought of. I was just wondering if really there wasn't any way of doing it implicitly, like using a binding function or something in the subscriber declaration. But I guess that even if there was, it would be less efficient.

vbs gravatar image vbs  ( 2018-04-27 10:20:53 -0500 )edit
0

answered 2018-05-01 04:18:58 -0500

vbs gravatar image

updated 2018-05-01 04:41:39 -0500

For future reference of anyone stumbling upon the same problem, I've written a sample roscpp package following the structure mentioned by robotchicken:

https://github.com/vbschettino/roscpp...

EDIT: please don't take this as a preferred or model implementation, as I don't have enough C++ knowledge for that. It's just one possible implementation. See comments below.

edit flag offensive delete link more

Comments

Might be good to mention that you chose to use inheritance to implement the wrapper, which is not the only way to do it (and might even violate at least Liskov subst principle). Composition (ie: making an instance of Echoer a member variable of your wrapper) is more common.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-01 04:22:26 -0500 )edit

Thank you for your comment gvdhoorn, I've updated the answer. If I find the time I might try to update the repo using the composition implementation, as you suggested. Could you point to any common ROS package that uses this implementation, as a reference?

vbs gravatar image vbs  ( 2018-05-01 04:46:00 -0500 )edit

Please don't take my comment as criticism :) +100 for making an example available. I just thought it'd be good to add this one note.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-01 04:46:10 -0500 )edit

I take it as good criticism :) It's always good to learn best practices.

vbs gravatar image vbs  ( 2018-05-14 09:49:48 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-04-26 15:56:51 -0500

Seen: 408 times

Last updated: May 01 '18