Robotics StackExchange | Archived questions

How can i make communication between two nodes(Python)

Hi... I have a problem with rospy. I want to create two nodes that communicate with each other. The problem is that the communication starts only when node1 and node2 send information on the channel where they are subscribed.

ROSPY NODE1

import rospy
from std_msgs.msg import String
def callback(msg):
    print '%s' % msg.data
def nodo():
    pub = rospy.Publisher('chatter1', String, queue_size=10)
    rospy.init_node('nodo1', anonymous=True)
    rospy.Subscriber('chatter2', String, callback)
    rate = rospy.Rate(1) # 10hz
    x = "Nodo1"
    while not rospy.is_shutdown():
    for i in range(1,51):       
            num = "%s" % (x)
            rospy.loginfo(num)
            pub.publish(num)
            rate.sleep()
        rospy.spin()
if __name__ == '__main__':
    try:
        nodo()
    except rospy.ROSInterruptException:
        pass

ROSPY NODE2

    import rospy
from std_msgs.msg import String
def callback(msg):
    print '%s' % msg.data
def nodo():
    pub = rospy.Publisher('chatter2', String, queue_size=10)
    rospy.init_node('nodo2', anonymous=True)
    rospy.Subscriber('chatter1', String, callback)
    rate = rospy.Rate(1) # 10hz
    x2 = "Nodo2"
    while not rospy.is_shutdown():
    for i in range(1,51):
            num = "%s" % (x2)
            rospy.loginfo(num)
            pub.publish(num)
            rate.sleep()
            rospy.spin()
if __name__ == '__main__':
    try:
        nodo()
    except rospy.ROSInterruptException:
        pass

How can i start the communication only when the nodes are enabled???

Asked by crisannik on 2016-05-21 04:58:44 UTC

Comments

Is this related to Communication between nodes (rospy)?

Asked by gvdhoorn on 2016-05-21 09:15:24 UTC

Yes, my problem is related

Asked by crisannik on 2016-05-21 09:25:04 UTC

Answers