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

Python API rosbag write custom message

asked 2021-03-16 15:14:04 -0500

Asdewar gravatar image

Hi everyone,using the Python API I would like to write a message in a rosbag file.

The message's format is described in this file

Someone knows how to write it in the bag file?

With the library std_msgs I can write standar types like str or unint, but i don't know how to do it with a custom message.

edit retag flag offensive close merge delete



Do you have the custom message installed already?

tryan gravatar image tryan  ( 2021-03-16 20:04:01 -0500 )edit

Probably not, what do you mean? I have to install it like a python module? I only have that .msg file but I did not know that it could be installed.

Asdewar gravatar image Asdewar  ( 2021-03-17 07:11:23 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-17 08:54:13 -0500

tryan gravatar image

updated 2021-03-17 08:55:59 -0500

To use your own custom message, you have to tell ROS about it when you build your package. More specifically, ROS needs to generate the message headers and put them in the proper location (depends on build tool). The Creating a Msg and Srv tutorial has the steps for doing this, and it's explained in the msg wiki.

If it's a custom message as part of a third-party package, the message generation should happen automatically when you build/install that package. To check if it's installed properly and ROS can find it, you can run rosmsg show <package_name>/<msg_name> in a terminal. In this case, it would be:

rosmsg show dvs_msgs/Event

The output should match the message definition:

uint16 x
uint16 y
time ts
bool polarity

Once the headers are available, you can use the message just like any other. Here's the Event message added to the rosbag Python API example:

import rosbag
import rospy  # Need this to create a rospy.Time object for Event.ts
from std_msgs.msg import Int32, String
from dvs_msgs.msg import Event  # Imports the custom message from its package

bag = rosbag.Bag('test.bag', 'w')

    s = String() = 'foo'

    i = Int32() = 42

    e = Event()
    e.x = 23
    e.y = 17
    e.ts =
    e.polarity = False

    bag.write('chatter', s)
    bag.write('numbers', i)
    bag.write('events', e)

I didn't actually test this code, so let me know if you run into problems.

edit flag offensive delete link more


Hi again, I understand what you mean but, I want to do it without installing ROS in mi computer. There is another way to write a custom message without using ROS? I want to make a program that can write some messages but without the user having to worry about installing ROS But thanks a lot, I will continue searching but if I do not find a solution, I'll use yours

Asdewar gravatar image Asdewar  ( 2021-03-25 09:31:28 -0500 )edit

If you don't want to use ROS, why not just use the lower-level library for your device (libcaer, I think) instead of ROS messages and bag files, which have inherent dependencies on ROS components?

tryan gravatar image tryan  ( 2021-03-25 09:56:19 -0500 )edit

Its because I want to make a bridge beetween a rosbag file and a txt file, and the only way I know to write in the bag file is to use the rosbag API for python

Asdewar gravatar image Asdewar  ( 2021-03-25 10:48:23 -0500 )edit

You may have valid reasons, but it's still unclear to me why a bag is involved if you don't expect to use ROS. To avoid ROS dependencies, you could (de)serialize the data yourself or using some other tool to work with a non-bag format. If you want to use a bag without ROS, you can still do so by (de)serializeng according to the bag format, though.

tryan gravatar image tryan  ( 2021-03-29 09:47:32 -0500 )edit

Question Tools



Asked: 2021-03-16 15:14:04 -0500

Seen: 2,363 times

Last updated: Mar 17 '21