Ask Your Question
0

Callback fuction is not getting called

asked 2018-10-01 04:17:53 -0500

Anuja_Rane gravatar image

updated 2018-10-01 06:53:09 -0500

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-10-01 06:59:09 -0500

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.

edit flag offensive delete link more

Comments

It fixed my problem :) Thank you so much

Anuja_Rane gravatar imageAnuja_Rane ( 2018-10-02 05:08:16 -0500 )edit

Great, thanks for letting us know.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-10-02 06:54:00 -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

Stats

Asked: 2018-10-01 04:06:05 -0500

Seen: 69 times

Last updated: Oct 01 '18