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

Equivalent of rospy.AnyMsg but only for messages with a Header?

asked 2016-03-31 13:32:29 -0600

lucasw gravatar image

updated 2020-11-21 12:06:41 -0600

Is there an equivalent of rospy.AnyMsg that would work for all message types with a Header?

I'd like to have a message_filters.TimeSynchronizer subscribe to a generic message type, but it has to have a Header.

Trying to put AnyMsg into the synchronizer yields:

[ERROR] [/demux_topic] [/opt/ros/jade/lib/python2.7/dist-packages/rospy/]:[723] [bad callback: <bound method Subscriber.callback of <message_filters.Subscriber instance at 0x7f6d333c75f0>>
Traceback (most recent call last):
  File "/opt/ros/jade/lib/python2.7/dist-packages/rospy/", line 720, in _invoke_callback
  File "/opt/ros/jade/lib/python2.7/dist-packages/message_filters/", line 74, in callback
  File "/opt/ros/jade/lib/python2.7/dist-packages/message_filters/", line 56, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/jade/lib/python2.7/dist-packages/message_filters/", line 190, in add
    my_queue[msg.header.stamp] = msg
AttributeError: 'AnyMsg' object has no attribute 'header'
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-03-31 15:40:56 -0600

lucasw gravatar image

updated 2016-04-01 12:40:42 -0600

I created a solution to this using a bootstrapping technique: subscribe to an AnyMsg with a regular rospy.Subscriber callback, then look in _connection_header['message_definition'] for Header header then if it has it:

Current solution

import roslib.message
topic_type = msg._connection_header['type']
topic_class = roslib.message.get_message_class(topic_type)
sub = message_filters.Subscriber("foo", topic_class)

Possible Solution

__import__ or importlib - need an example.

Previous solution

if it exists import the message type using exec(), then resubscribe with a message_filters.Subscriber using eval() and create the TimeSynchronizer, unregister the bootstrap subscriber.

Using exec and eval is undesirable for security reasons.

edit flag offensive delete link more



Can you describe why you need to use exec() or eval()? Wouldn't __import(..)__ (or __importlib__(..)) allow you to import modules by name?

gvdhoorn gravatar image gvdhoorn  ( 2016-04-01 00:56:29 -0600 )edit

I briefly tried out importlib, didn't get it right, but a search for rospy importlib brought me to roslib, which works fine.

lucasw gravatar image lucasw  ( 2016-04-01 12:13:05 -0600 )edit

Question Tools



Asked: 2016-03-31 13:32:29 -0600

Seen: 775 times

Last updated: Apr 01 '16