Ask Your Question
0

Can a single node act as publisher and subscriber alternativelye

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

user_123 gravatar image

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

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
    msg.age2=0
    msg.flag=1
    if msg.flag==1:
        msg.age2+=1
        print ("Hello! this is node 1!!")   
    rospy.loginfo(msg)
    pub.publish(msg)        

if __name__ == '__main__':
    try:
        talker()
    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))
    data.age1+=1
    data.flag=2


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
    rospy.spin()

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

Comments

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 imageNEngelhard ( 2016-11-25 04:29:40 -0500 )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 imageuser_123 ( 2016-11-25 06:19:03 -0500 )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 imagemgruhler ( 2016-11-25 06:26:51 -0500 )edit

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

mgruhler gravatar imagemgruhler ( 2016-11-25 06:27:23 -0500 )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 imageuser_123 ( 2016-11-28 05:01:11 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

mgruhler gravatar image

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

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

Comments

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 listener.py.

user_123 gravatar imageuser_123 ( 2016-11-25 03:12:48 -0500 )edit

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

user_123 gravatar imageuser_123 ( 2016-11-25 03:13:25 -0500 )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 imagemgruhler ( 2016-11-25 03:28:16 -0500 )edit

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

mgruhler gravatar imagemgruhler ( 2016-11-25 03:28:50 -0500 )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 imageuser_123 ( 2016-11-25 04:55:59 -0500 )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

2 followers

Stats

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

Seen: 651 times

Last updated: Nov 25 '16