Search code examples
c++protocol-buffersros

Converting Protobuf to ROS messages with similar functionality


I recently inherited a large codebase at work utilizing MOOS & Protobuf messages.

At the request of my project lead, I am porting it to use exclusively ROS where ROS messages are used instead of protobuf. The code base heavily relies on utilizing protobuf functionality such as enumerator min / max, extracting a string from the variable field, ->has_variable() function, ->isValid(), etc.

So far I have only been able to find very basic ROS message functionality from the wiki.

Are there any 'hacks' or the like to have this type of pliability?

Example: Protbufs support enumerators, but ROS messages don't, so I have:

  uint8 TYPE_FAILED        = 0
  uint8 TYPE_OPERATIONAL   = 1
  uint8 TYPE_INITIALIZING  = 2

  uint8 health_state_type

My health_state_type is my 'enumerator' but I don't have a min or max unless I hardcode one, and I can't extract TYPE_FAILED as a string. I've been slowly finding workarounds for this by using

my_message::custom_msg health;
health.health_state_type = health.TYPE_FAILED

But I'm having to modify many different areas that use it as a string, not integer.


Solution

  • Yes there is a hack. But you need to input a some work into it. For using the publisher/subscriber methods in ROS you need to define messages for all topics in .msg files.

    From this file then a C++ class is automatically generated. But you don't want to touch that autogenerated file! What you could do instead is define your class and associate it with the autogenerated class.

    Look here for an example how to do it. You could then expand your custom class with desired methods like isValid.

    Another (perhaps simpler) way would be to declare a helper class that would do the desired work for each type in messages.

    Or you could simply continue to use protobuf. It is also used at least in Gazebo if not also in ROS.