ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
You could use a bond:
#! /usr/bin/python
import rospy
from bondpy import bondpy
def formed():
print "We got a bond"
def broken():
print "SOMEONE DIED"
rospy.init_node("A", anonymous=True)
b = bondpy.Bond("heartbeat_topic_name","bond_name", on_broken=broken, on_formed=formed)
b.start()
if not b.wait_until_formed():
raise Exception('Bond could not be formed')
rospy.spin()
Just start this node twice, wait for the bond to be established and kill one of the nodes. After a short timeout (4 seconds) the other node will notice the missing partner. With this method, you don't need to poll rosnode list or even know the name of the other node, you just have to use the same topic and bond_name to establish the connection and have a callback triggered as soon as one node does not send its heartbeat anymore.