Two nodes subscribed to each other that should count do not start unless I force a publisher
Here is the code for the first node called /first which is subscribed to /second_channel and publishes to /first_channel
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node('second')
bar = rospy.Publisher('first_channel', Int32, queue_size=10)
count = 0
def callback(msg):
count = msg.data
print count
bar.publish(count+1)
sub = rospy.Subscriber('second_channel', Int32, callback)
bar.publish(count)
rospy.spin()
Here is the code for the second node called /second which is subscribed to /first_channel and publishes to /second_channel
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node('first')
foo = rospy.Publisher('second_channel', Int32, queue_size=10)
count = 0
def callback(msg):
count = msg.data
print count
foo.publish(count+1)
sub = rospy.Subscriber('first_channel', Int32, callback)
rospy.spin()
When I start both of these in terminal, they are running without anything published and the rqt_graph command shows that they are subscribed to each other. But when I run: 'rostopic pub -1 /first_channel std_msgs/Int32 2' both nodes start passing the counter back and forth correctly. How can I make the passing back and forth start?