Ask Your Question

Can a single node act as publisher and subscriber alternativelye

asked 2016-11-25 00:30:30 -0600

user_123 gravatar image

updated 2016-11-25 06:24:31 -0600

mgruhler gravatar image

I want to write two nodes such that each node sends a message alternatively in recurrent cycle and the message contains one string message and two age variables. For example, Node 1 sends a message to Node 2 (Node 1 Publisher, Node 2 Subscriber, node 1 age ++), then after receiving the message Node 2 sends the message back to Node 1 (Node 2 Publisher, Node 1 Subscriber, node 2 age ++) and this cycle continues. I am new to ROS, please help me to design this code in python or c++.

#!/usr/bin/env python
import rospy
from beginner_tutorials.msg import Num

def talker():
    pub = rospy.Publisher('custom_Chat', Num,queue_size=1
    rospy.init_node('custom_Node1', anonymous=True)
    msg = Num()
    msg.message = "Message sent to Node2"
    msg.age1 =0
    if msg.flag==1:
        print ("Hello! this is node 1!!")   

if __name__ == '__main__':
    except rospy.ROSInterruptException: pass


#!/usr/bin/env python
import rospy
from beginner_tutorials.msg import Num

def callback(data):
    rospy.loginfo("Hello!this is node2")
    rospy.loginfo("I heard the message %s. %d is my age." % (data.message, data.age2))

def listener():
    rospy.init_node('custom_Node2', anonymous=True)
    rospy.Subscriber("custom_Chat", Num, callback)

    # spin() simply keeps python from exiting until this node is stopped

if __name__ == '__main__':
edit retag flag offensive close merge delete


What do you achieve in the end? This looks like a heartbeat implementation to see if both nodes are alive, but there are easier ways to do that.

NEngelhard gravatar image NEngelhard  ( 2016-11-25 04:29:40 -0600 )edit

I was simply trying node1 to publish a messgae on "custom_chat" topic, and node 2 to receive it, but here, node 1 is publishing it but node2 isn't receiving

user_123 gravatar image user_123  ( 2016-11-25 06:19:03 -0600 )edit

Well, your custom_Node1 shuts down after having done one run. You have no subscriber in there, no callback.

Basically, take the listener node, add a publisher and publish the data from the callback. This should be all. Also, I'm not quite sure if this will work with the same topic. I don't know..

mgruhler gravatar image mgruhler  ( 2016-11-25 06:26:51 -0600 )edit

.. if a node can "self-subscribe", so you are probably better of publishing/subscribing to different topics...

mgruhler gravatar image mgruhler  ( 2016-11-25 06:27:23 -0600 )edit

If possible, can you please modify the program and rewrite it for me using different topics and attach it as a link, I am getting puzzled after going midway...please help...

user_123 gravatar image user_123  ( 2016-11-28 05:01:11 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-11-25 01:25:49 -0600

mgruhler gravatar image

updated 2016-11-25 01:26:11 -0600

This is simple (assuming there is no other publisher to this topic and you do take care of timing).

Subscribed topics get handled in callbacks, so if you publishe the first message in node 1 (e.g. from the main loop, make sure to only send it once), node 2 receives it and it is handled in the callback. In this callback, adapt what you want and publish it again, so that node 1 can receive it. There, do the same thing in the callback.

How to write publishers and subscribers is documented in detail on the wiki for C++) and Python).

edit flag offensive delete link more


I am following the basic Publisher-Subscriber tutorial as given in the link.According to you, I should have line no 7-15 from talker.pyin the callback definition,in

user_123 gravatar image user_123  ( 2016-11-25 03:12:48 -0600 )edit

Similarly, in I should have two functions called callback and listener.

user_123 gravatar image user_123  ( 2016-11-25 03:13:25 -0600 )edit

no, just the message composition (9-11) and the publishing (15). You only can have one init_node per node, also you don't need the loop, but just publish once in the callback.

Yes, you need in both nodes a subscriber with a callback function and a publisher, but that does not require a ...

mgruhler gravatar image mgruhler  ( 2016-11-25 03:28:16 -0600 )edit

... special function. So, I would also recommend to rename the nodes, as both are doing "talking" and "listening".

mgruhler gravatar image mgruhler  ( 2016-11-25 03:28:50 -0600 )edit

Hey I have tried to write two nodes for one side communication to start with, when I am running, node 1 is publishing, but node 2 is not giving any output. Please find the code and edit if possible. I have modified the question with the two codes.

user_123 gravatar image user_123  ( 2016-11-25 04:55:59 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-11-25 00:30:30 -0600

Seen: 1,204 times

Last updated: Nov 25 '16