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

How to check that message exists with catkin for conditional compilation (sensor_msgs/BatteryState)

asked 2016-01-10 17:01:56 -0500

vooon gravatar image

Hello, i want to add support for new message but i need some "how to" on detecting available messages with catkin.

I'm thinking to use cmake execute_process() for that. Some thing like that:

    COMMAND ${PYTHON_EXECUTABLE} -c "from sensor_msgs.msg import BatteryState"

But maybe catkin already has some macro for that task?

edit retag flag offensive close merge delete


An alternative way would be to use something like rosmsg show sensor_msgs/BatteryState. Also there seems to be generated variables of the form <package_name>_MESSAGE_FILES which are the lists of paths. However, to get it you probably have to find_package first.

Boris gravatar image Boris  ( 2016-01-10 17:47:47 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2016-01-10 20:04:24 -0500

Dirk Thomas gravatar image

Within CMake you can access the list of message/service files of a package since every message package exports these information in via its CMake extras file. After calling:

find_package(std_msgs REQUIRED)


find_package(catkin REQUIRED COMPONENTS std_msgs)

the list of message files is available in the CMake variable:

edit flag offensive delete link more


Looks like i need that code:

list(FIND sensor_msgs_MESSAGE_FILES "msg/BatteryState.msg" BATTERY_STATE_MSG_IDX)
vooon gravatar image vooon  ( 2016-01-10 23:48:53 -0500 )edit

answered 2016-01-10 18:39:22 -0500

ahendrix gravatar image

This is tricky, because there are not a lot of good ways to do it.

Old versions of rosmsg show does not return an error code if the message does not exist, so that probably isn't the best choice.

The python messages aren't generated until the build step, so they may not exist at configure time, which means that a python script which tries to import messages will always fail the first time, even if the package contains the messages you're looking for.

I think the best way to do this would be to check which version of sensor_msgs you're building against from cmake, and use that as the conditional in your build. You can do this with the VERSION_GREATER or VERSION_LESS comparisons:

# cmake
find_package(catkin REQUIRED COMPONENTS
    # etc

if( ${sensor_msgs_VERSION} VERSION_LESS 2.42.3 ) # substitute the version you care about
    message("sensor_msgs too old; won't build foo")
    # build cool new things

(This SHOULD work; but I haven't tested it. May need a little tweaking)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-01-10 17:01:56 -0500

Seen: 506 times

Last updated: Jan 10 '16