How can I use the topics of a rosbag with custom messages?

asked 2021-03-29 00:56:18 -0500

Kansai gravatar image

I have a rosbag. When played, this rosbag publishes several topics. When I do rosbag info to the rosbag I got for example

topics:
       /some_interface/themessage       115msgs : custom_protocol_msgs/TheMessage

As you can see, the rosbag publishes a topic with a custom type (that was produced by a different program)

I am writing a python scripts and I want to receive these messages and use them. However my workspace and my package is different (not custom_protocol_msgs or some_interface).

How can I subscribe to this topic?

My work: I have done the following:

from mypackage.msg import TheMessage

subs= rospy.Subscriber("/some_interface/themessage", TheMessage,callback)

This does not work because when I do this, rostopic /some_interface/themessage I got

ERROR: Cannot load message class for [custom_protocol_msgs/TheMessage]. Are your messages built?

If I do

  subs= rospy.Subscriber("/mypackage/themessage", TheMessage,callback)

and I do mypackage /some_interface/themessage

I got no new messages.

How can I read these customs messages and operate on them?

edit retag flag offensive close merge delete

Comments

2

You have two options I believe:

  1. use rospy.AnyMsg and look at _connection_header['message_definition'], similar to how #q230676 does it
  2. use the script in #q230120 to extract the message definitions, store them in files, recreate the package(s), build your workspace and then treat them as you'd do other messages

The first is a probably more advanced than the second, and would also require actually building a node, rosbag playing the .bag and then running everything. The first simply opens the bag directly.

Depending on what you want to do, opening the .bag directly and iterating over the messages may be more efficient anyway.

gvdhoorn gravatar image gvdhoorn  ( 2021-03-29 01:34:27 -0500 )edit