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

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

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

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 rwheel.data and lwheel.data into a json file

def listener():
   rospy.init_node('node_name')
   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)
   ts.registerCallback(callback)
   rospy.spin()

if __name__ == '__main__':
  listener();

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/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 75, in callback
    self.signalMessage(msg)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", 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

Comments

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 -0600 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2019-11-13 04:45:45 -0600 )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 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

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

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

Comments

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 -0600 )edit

merging rwheel.data and lwheel.data 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 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,164 times

Last updated: Nov 13 '19