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

Revision history [back]

click to hide/show revision 1
initial version

I am not entirely sure what you are trying to achieve but I'll give it a try. From your question I see two possible ways you would like to use that:

You create a message in part of your node and want to process it in a different function:

publisher = rospy.Publisher("/mytopic", ...)
rospy.Subscriber("/mytopic", eggs, ...)

def spam():
    message.a = 1
    message.b = 2
    publisher.publish(message)

def eggs(msg):
    print msg.a + msg.b

If this is he case, I wouldn't know why you would transfer the message via a tcp connection instead of just calling the function directly if both spam and eggs are in the same node.

If you are trying to modify data on a topic and then republish it on the same topic:

publisher = rospy.Publisher("/mytopic", ...)
rospy.Subscriber("/mytopic", spam, ...)

def spam(msg):
    msg.a += 1
    publisher.publish(msg)

then this is completely ambiguous and you will create an endless-loop because spam will trigger itself by publishing. It might sound like a solid idea to take data on a topic, modify it and then send it off to a different compent which is supposed to work on the modified data:

msg.a = 1 ---> spam(msg) ---> msg.a = 2 ---> foo

The problem here is that spam does not consume the message. What you would actually get is that foo receives msg.a=1 and msg.a=2 while spam receives msg.a=2 and turns it into msg.a=3 which is then received by foo and spam and so forth.

I think the issue here is more conceptual. Messages on a topic are not consumed by the receiver and, therefore, you cannot receive and publish on the same topic without creating an endless-loop. Every other behaviour would be completely counter intuitive. Also, you cannot choose which messages to receive and which to ignore.

Solution: create a different topic for one of them via remapping either the incoming or the outgoing topic.

Hope that answers your question.