0
$\begingroup$

Rosanswers logo

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?


Originally posted by wallybeam on ROS Answers with karma: 57 on 2019-11-13

Post score: 0


Original comments

Comment by Choco93 on 2019-11-13:
Are you publishing these messages yourself or you have an existing bag that you are using?

Comment by gvdhoorn on 2019-11-13:
Taking a step back: do you actually need to use message_filters here?

Comment by wallybeam on 2019-11-13:\

  • 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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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)

Originally posted by Delb with karma: 3907 on 2019-11-13

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by wallybeam on 2019-11-13:
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.

Comment by Delb on 2019-11-13:\

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).

Comment by wallybeam on 2019-11-13:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.