Node to NOde synchronous communication using Publisher Subscriber Architecture

asked 2016-12-07 04:59:16 -0600

user_123 gravatar image

updated 2016-12-08 01:31:18 -0600

mgruhler gravatar image

I want to design three nodes such that node 1 is publishing a message to node 2 and node 3, node 2 and 3 is processing the message and sending back to node 1, and once node 1 receives both the messages from node 2 and node 3, it sends the message again and cycle continues. The entire thing must be carried out synchronously. Please suggest me the ways to do it. Thank you,

Node 1(code)---------------------------------------------------------------------------

#!/usr/bin/env python

import rospy
from beginner_tutorials.msg import Num

msg=Num()
msg.message="Message sent to listener node"
msg.age1=0
msg.age2=0
msg.flag=1

def callback(data):
    rospy.loginfo("Hello! I'm node 1. I received the message %s.My age is: %d" % (data.message, data.age1))
    pub=rospy.publisher('node_chat',Num,queue_size=1)
    msg=Num()
    msg.message="Message sent to listener node"
    msg.age1=data.age1
    msg.age2=data.age2+1
    msg.flag=1
    rospy.loginfo(msg)
    pub.publish(msg)
    rospy.spin()


if __name__ == '__main__':
    try:

        pub = rospy.Publisher('node_chat', Num, queue_size=1)
        rospy.init_node('node1', anonymous=True)
        while not rospy.is_shutdown():
                print ("Hello! I'm node 1")
                msg.age2+=1
                rospy.loginfo(msg)
                pub.publish(msg)
                rospy.sleep(10)
                rospy.Subscriber('node_chat',Num, callback)

    except rospy.ROSInterruptException:
        pass

node2(code)-----------------------------------------------------------------------------------------------------------------------------------

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

def callback(data):
    rospy.loginfo("Hello! I'm node 2. I received the message %s.My age is: %d" % (data.message, data.age2))
    pub=rospy.publisher('node_chat',Num,queue_size=1)
    msg=Num()
    msg.message="Message sent back to talker node"
    msg.age1=data.age1+1
    msg.age2=data.age2
    msg.flag=2
    rospy.loginfo(msg)
    pub.publish(msg)
   # rospy.spin()

def listener():
    rospy.init_node('node2', anonymous=True)
    rospy.Subscriber('node_chat',Num, callback)

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

if __name__ == '__main__':
    listener()

I am getting this error also, which shows----------------

[ERROR] [WallTime: 1481180187.955999] bad callback: <function callback at 0x7f22                                                                                                                                        22ddca28>
Traceback (most recent call last):
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720,                                                                                                                                         in _invoke_callback
    cb(msg)
  File "/home/collab/catkin_ws/src/beginner_tutorials/scripts/customlistener.py"                                                                                                                                        , line 7, in callback
    pub=rospy.publisher('node_chat',Num,queue_size=1)
AttributeError: 'module' object has no attribute 'publisher'
edit retag flag offensive close merge delete

Comments

Where exactly(!) is your problem? (And what do you want to achieve? Do you only want to know if node 2 and 3 are still alive?)

NEngelhard gravatar imageNEngelhard ( 2016-12-07 05:26:58 -0600 )edit

Actually, I am new to ROS, I don't know how to write the publisher and subscriber commands together in a node and I want to ensure that the communication is synchronous, that is node 1 publishes only if after it receives back the messages from node 2 and node 3.

user_123 gravatar imageuser_123 ( 2016-12-07 06:26:03 -0600 )edit

Moreover, the timing maintained by rospy.Rate as written in the tutorial, I don't know how to modify it to acheive the concerned purpose.

user_123 gravatar imageuser_123 ( 2016-12-07 06:27:43 -0600 )edit

But where is your problem? I assume you already tried to solve your problem yourself before asking here. So what did you do? Could you show the code?

NEngelhard gravatar imageNEngelhard ( 2016-12-07 06:46:13 -0600 )edit

I have written the code for two nodes, I am getting proper communication from node 1 to node 2, but not the other way around. Could you please modify the code so that node 1 also receives the message published by node 2.

user_123 gravatar imageuser_123 ( 2016-12-08 01:07:34 -0600 )edit

@rimital, please use the preformatted text button (the one with ones and zeros) when copying in code...

mgruhler gravatar imagemgruhler ( 2016-12-08 01:30:45 -0600 )edit
1

also, this seems to be pretty much a followup of http://answers.ros.org/question/24878... As @NEngelhard wrote: What is your problam exactly?

The error is simply because you Publisher should start with a captial letter...

mgruhler gravatar imagemgruhler ( 2016-12-08 01:33:55 -0600 )edit
2

Also: don't instantiate Publishers, Subscribers or NodeHandles inside callbacks, ever. It's not going to work reliably. Instantiate them somewhere else, then use them in callbacks.

gvdhoorn gravatar imagegvdhoorn ( 2016-12-08 01:52:40 -0600 )edit