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

How to subscribe to multiple topic that has no header or how to add header into topics?

asked 2019-11-13 03:01:40 -0500

wallybeam gravatar image

ROS Kinetic Ubuntu 16.04 Python 3.5.2

I am trying to use following script to subscribe to 2 topics and combining their data into a json file.

import rospy
from std_msgs.msg import Float32
import message_filters
import json

def callback(rwheel, lwheel):
   # merging and into a json file

def listener():
   rwheel = message_filters.Subscriber("rwheel_angular_vel_motor", Float32)
   lwheel = message_filters.Subscriber("lwheel_angular_vel_motor", Float32)
   ts = message_filters.TimeSynchronizer([rwheel,lwheel], 10)

if __name__ == '__main__':

When I run the script, I am gettin the folowing error:

[ERROR] [1573630972.895704, 1242.480000]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7fd23d298278>>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/", line 75, in callback
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/", line 57, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/", line 216, in add
    my_queue[msg.header.stamp] = msg
AttributeError: 'Float32' object has no attribute 'header"

Acually, error seems pretty straight forward. 'Float32' object has no attribute 'header' so we have to add header to Float32 but I couldn't figure out how to do. So, is there a way to subscribe mutiple topics wihout header or how can we add a header?

edit retag flag offensive close merge delete


Are you publishing these messages yourself or you have an existing bag that you are using?

Choco93 gravatar image Choco93  ( 2019-11-13 03:44:59 -0500 )edit

Taking a step back: do you actually need to use message_filters here?

gvdhoorn gravatar image gvdhoorn  ( 2019-11-13 04:45:45 -0500 )edit
  • I am publishing them myself, yet I am relatively new in ROS. So, I am not sure about what to do.

    • Since I want to simultaneously write data of right and left wheel on a single json file, I thought I should use only one callback function. I searched for a way to subscribe multiple topics and it seems to me message_filter is the only way. I will gladly try any other method.
wallybeam gravatar image wallybeam  ( 2019-11-13 05:21:03 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-11-13 03:55:19 -0500

Delb gravatar image

Not entirely sure but from the python docs :

ApproximateTimeSynchronizer synchronizes incoming message filters by the timestamps contained in their messages’ headers. The API is the same as TimeSynchronizer except for an extra slop parameter in the constructor that defines the delay (in seconds) with which messages can be synchronized. The allow_headerless option specifies whether to allow storing headerless messages with current ROS time instead of timestamp.

So you could use ApproximateTimeSynchronizer instead of TimeSynchronizer like this :

ApproximateTimeSynchronizer([image_sub, camera_sub], queue_size=10, slop=0.1, allow_headerless=True)
edit flag offensive delete link more


I tried your suggestion and I got another error as TypeError: 'zip' object is not subscriptable. It seems error comes from ts.registerCallback(callback), so your suggestion might be working. I will search about the new error and will inform you.

wallybeam gravatar image wallybeam  ( 2019-11-13 05:25:16 -0500 )edit

merging and into a json file

It's probably within the code you haven't shown, you might want to open a new question if you can't debug this because it's not related to your original question anymore (and if it's still ros related).

Delb gravatar image Delb  ( 2019-11-13 05:59:32 -0500 )edit

It seems I am getting another problem which requires a new question as you said. ApproximateTimeSynchronizer solved my issue. By using it I am not getting header error.

wallybeam gravatar image wallybeam  ( 2019-11-13 07:32:37 -0500 )edit

Question Tools

1 follower


Asked: 2019-11-13 03:01:40 -0500

Seen: 951 times

Last updated: Nov 13 '19