0
$\begingroup$

Rosanswers logo

Hello I am new to ROS. I want to send messages in the form of string from Node1->Node2 and Node2->Node1. As Node1 can publish and Node2 can subscribe to the messages. Node2 can publish the message but in Node1 callback function is not getting called.

Program for Node1(talker)

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from number_generation import give_prime, secret_no
from modulo_operations import calculate

def callback2(data):
    rospy.loginfo('I heard %s', data.data)

def talker():
    pub = rospy.Publisher('chatter1', String, queue_size=1)
    pub1 = rospy.Publisher('chatter', String, queue_size=1)
    rospy.init_node('talker', anonymous=True)
    P=str(give_prime())
    q=str(give_prime())
    secret = secret_no()
    Server = str(calculate(int(P), int(q), int(secret)))
    rospy.loginfo(P)
    rospy.loginfo(q)
    pub.publish(P)
    pub1.publish(q)
    rospy.Subscriber('c', String, callback2)
    rospy.spin()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

Program for Node2(listener)

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from number_generation import secret_no
from modulo_operations import calculate

P='asdasd'
q='ygyg'
secret = 2
client = 13

def callback(data):
       global P
       P=int(data.data)
       rospy.loginfo(rospy.get_caller_id() + 'I heard %s', P)


def callback1(data):
        global q
        global secret
        global client 
        q=int(data.data)
        rospy.loginfo(rospy.get_caller_id() + 'I heard %s', q)
        sum()   

def sum():  
        global secret
        secret = secret_no()
        client = str(calculate(int(P), int(q), int(secret)))
        print 'calculation', client
        pub2.publish(client)

if __name__ == '__main__':
        rospy.init_node('listener', anonymous=True)
        pub2 = rospy.Publisher('c',String,queue_size=1)
        rospy.Subscriber('chatter', String, callback)
        rospy.Subscriber('chatter1', String, callback1)

        rospy.spin()

Originally posted by Anuja_Rane on ROS Answers with karma: 3 on 2018-10-01

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I suspect this may be a problem of the timing of publishing and setting up the listeners.

A listener object takes a short period of time to attach to a topic before it will start receiving messages. in Node1 you are publishing on the topic chatter1 then then subscribing to the topic for the expected reply c. It is possible that the subscriber is not set up in time for it to catch the reply sent by Node2.

I recommend moving the line rospy.Subscriber('c', String, callback2) before the publish commands and adding a 1 second delay after it to make sure it's setup before you publish the test messages.

Let us know if this fixes your problem.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2018-10-01

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Anuja_Rane on 2018-10-02:
It fixed my problem :) Thank you so much

Comment by PeteBlackerThe3rd on 2018-10-02:
Great, thanks for letting us know.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.