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

Revision history [back]

Here is a small example from @wkentaro that receives a messages, sleeps a certain amount and also adds that amount to the header stamp, then republishes it on a new topic https://gist.github.com/wkentaro/2cd56593107c158e2e02

from rospy.msg import AnyMsg
...
        self._sub = rospy.Subscriber('~input', AnyMsg, self._cb)

    def _cb(self, anymsg):
        topic_types = self._master.getTopicTypes()
        msg_name = [ty for tp, ty in topic_types if tp == self._sub.name][0]
        msg_class = get_message_class(msg_name)
        msg = msg_class().deserialize(anymsg._buff)

I think this could also work:

msg_name = topic._connection_header['type']

But what if there is no header? This could guard against that:

if 'Header header' in anymsg._connection_header['message_definition']

I used it in a subscriber bootstrapping technique where an AnyMsg is subscribed to, then there is a check to see if the message has a header, then that first subscriber is unsubscribe and a second subscriber that is of the proper type is created (but I didn't give much example code) #q230676

In other cases I've used it to subscribe to topics where a warning is generated if the topic hasn't published recently, the AnyMsg prevents deserialization overhead and I can subscribe to any number of different types of topics using the same code. That is similar to the bandwidth calculation.

(One thing to note is that rospy doesn't like two concurrent subscribers in the same node on the same topic with different types- if one is AnyMsg and the other is the full topic type- the message is delivered to both callbacks as an AnyMsg, https://github.com/ros/ros_comm/issues/1351)

http://docs.ros.org/kinetic/api/rospy/html/rospy.msg.AnyMsg-class.html

Here is a small example from @wkentaro that receives a messages, sleeps a certain amount and also adds that amount to the header stamp, then republishes it on a new topic https://gist.github.com/wkentaro/2cd56593107c158e2e02

from roslib.message import get_message_class
from rospy.msg import AnyMsg
...
        self._sub = rospy.Subscriber('~input', AnyMsg, self._cb)

    def _cb(self, anymsg):
        topic_types = self._master.getTopicTypes()
        msg_name = [ty for tp, ty in topic_types if tp == self._sub.name][0]
        msg_class = get_message_class(msg_name)
        msg = msg_class().deserialize(anymsg._buff)

I think this could also work:

msg_name = topic._connection_header['type']

But what if there is no header? This could guard against that:

if 'Header header' in anymsg._connection_header['message_definition']

I used it in a subscriber bootstrapping technique where an AnyMsg is subscribed to, then there is a check to see if the message has a header, then that first subscriber is unsubscribe and a second subscriber that is of the proper type is created (but I didn't give much example code) #q230676

In other cases I've used it to subscribe to topics where a warning is generated if the topic hasn't published recently, the AnyMsg prevents deserialization overhead and I can subscribe to any number of different types of topics using the same code. That is similar to the bandwidth calculation.

(One thing to note is that rospy doesn't like two concurrent subscribers in the same node on the same topic with different types- if one is AnyMsg and the other is the full topic type- the message is delivered to both callbacks as an AnyMsg, https://github.com/ros/ros_comm/issues/1351)

http://docs.ros.org/kinetic/api/rospy/html/rospy.msg.AnyMsg-class.html

Here is a small example from @wkentaro that receives a messages, sleeps a certain amount and also adds that amount to the header stamp, then republishes it on a new topic https://gist.github.com/wkentaro/2cd56593107c158e2e02

from roslib.message import get_message_class
from rospy.msg import AnyMsg
...
        self._sub = rospy.Subscriber('~input', AnyMsg, self._cb)

    def _cb(self, anymsg):
        topic_types = self._master.getTopicTypes()
        msg_name = [ty for tp, ty in topic_types if tp == self._sub.name][0]
        msg_class = get_message_class(msg_name)
        msg = msg_class().deserialize(anymsg._buff)

I think this could also work:

msg_name = topic._connection_header['type']

But what if there is no header? This could guard against that:

if 'Header header' in anymsg._connection_header['message_definition']

I used it in a subscriber bootstrapping technique where an AnyMsg is subscribed to, then there is a check to see if the message has a header, then that first subscriber is unsubscribe unsubscribed and a second subscriber that is of the proper type is created (but I didn't give much example code) #q230676

In other cases I've used it to subscribe to topics where a warning is generated if the topic hasn't published recently, the AnyMsg prevents deserialization overhead and I can subscribe to any number of different types of topics using the same code. That is similar to the bandwidth calculation.

(One thing to note is that rospy doesn't like two concurrent subscribers in the same node on the same topic with different types- if one is AnyMsg and the other is the full topic type- the message is delivered to both callbacks as an AnyMsg, https://github.com/ros/ros_comm/issues/1351)

http://docs.ros.org/kinetic/api/rospy/html/rospy.msg.AnyMsg-class.html