I can see 2 solution based on your comment :
1)
Check this solution : https://answers.ros.org/question/2443... (the one at the bottom)
You can get more info here : http://wiki.ros.org/Topics#Topic_stat...
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)
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 :
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.
wait_for_message
will get the first message that is published. If you want continuous subscription use normal subscriber.Is the question how to detect stale/hung-up topics?
To complete the comment above, you can use a normal subscriber in rospy (rospy.subscribe(...) at the beginning of your code), since the spinning is threaded in rospy, you will always execute the callback function (without calling rospy.spin())
Actually, when the topic is not publishing msg, I need do some action. So its important for me to detect whether the topic is publishing...
take a look here, with this you can get list of all topics that are advertising, you can look for your topic in there and if it's not then do your action.
Thank you for the answer but I have just tried the
get_published_topics
and I think it will list all topics which is registered in master, without checking whether it is publishing msg or not...I have used its counterpart in c++, I wanted to know when a topic is being advertised and that worked for me, and documentation says that it should report all topics that are being published so it should work. But you can verify its working easily.
Yes, it's work for advertised topics (I use it too in c++), but pumpkin want to know when a message is published (Well, actually 'not' published), and get_published_topics will report a topics as a publisher when calling rospy.Publisher(..), even if the Publisher never .publish(..)