ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

I can see 2 solution based on your comment :

1)

Check this solution : https://answers.ros.org/question/244311/how-to-detect-subscriber-drops-a-message/?answer=270198#post-id-270198 (the one at the bottom)

You can get more info here : http://wiki.ros.org/Topics#Topic_statistics

It give you a nice approach to detect if message are not received (by using TopicStatistics.traffic or TopicStatistics.delivered_msgs, by checking for how long the data remain the same).

I just discovered TopicStatistics, so I cannot give you any insight on how to use it (and how it affect performance, if it do)

2)

You can use some Python basic library to do the job (But you lose the awesome feature of topic statistic) :

import rospy
import threading # Needed for Timer
from std_msgs.msg import String

rospy.init_node("test")

def timeout():
    print("No message received for 5 seconds")
    # Do something

def callback(msg):
    global timer
    print("Message received")
    timer.cancel()
    timer = threading.Timer(5,timeout)
    timer.start()
    # Do Other thing

rospy.Subscriber("/test",String, callback) # When receiving a message, call callback()
timer = threading.Timer(5,timeout) # If 5 seconds elapse, call timeout()
timer.start()

while not rospy.is_shutdown():
    #Do something else
    rospy.sleep(1)

This code give you the idea, by using a timer you can check when no message are received. If a message is received, you reset the timer (By cancelling and starting a new timer). If no message are received, then you can do what you want in the timeout() function.

I can see 2 solution based on your comment :

1)

Check this solution : https://answers.ros.org/question/244311/how-to-detect-subscriber-drops-a-message/?answer=270198#post-id-270198 (the one at the bottom)

bottom) You can get more info here : http://wiki.ros.org/Topics#Topic_statistics

http://wiki.ros.org/Topics#Topic_statistics It give you a nice approach to detect if message are not received (by using TopicStatistics.traffic or TopicStatistics.delivered_msgs, by checking for how long the data remain the same).

same). I just discovered TopicStatistics, so I cannot give you any insight on how to use it (and how it affect performance, if it do)do)

EDIT: Topics statistics messages are send by the subscriber, when the subscriber received a message, hence it will not be usable in your use case (when the subscriber doesn't received a message).

2)

You can use some Python basic library to do the job (But you lose the awesome feature of topic statistic) :

import rospy
import threading # Needed for Timer
from std_msgs.msg import String

rospy.init_node("test")

def timeout():
    print("No message received for 5 seconds")
    # Do something

def callback(msg):
    global timer
    print("Message received")
    timer.cancel()
    timer = threading.Timer(5,timeout)
    timer.start()
    # Do Other thing

rospy.Subscriber("/test",String, callback) # When receiving a message, call callback()
timer = threading.Timer(5,timeout) # If 5 seconds elapse, call timeout()
timer.start()

while not rospy.is_shutdown():
    #Do something else
    rospy.sleep(1)

This code give you the idea, by using a timer you can check when no message are received. If a message is received, you reset the timer (By cancelling and starting a new timer). If no message are received, then you can do what you want in the timeout() function.