ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Crash detection

asked 2015-03-27 09:44:05 -0600

Matth gravatar image


I try to code a crash detection system with a publisher and a subscriber. Now, when i shutdown (^C) the publisher, the subscriber is in a state of "wait". It waits until it sees another msg on the topic and won't execute my code after the subscribe function.

I've tried to detect a decrease of publisher on the topic but it doesn't work.

Can you help me with this ?

Here are my code :

My Primary (Publisher) :

int main (int argc, char **argv){

int i=0;
ros::init(argc, argv, "primary");
ros::NodeHandle primary;
ros::Publisher pub_wd;
pub_wd = primary.advertise<primary::Test>("primwd", 1000);

    ::primary::Test test_msg;
    test_msg.count = i;
    if(i == 16001){
            i = 1;
    cout << "Le message publié est " << i << endl;
return 0;

My watchdog (Subscriber) :

watchdog::watchdog() {

    i = 0;
    inc = 1;
    etat_prec = 0;


int watchdog::check_watchdog(){

            cout << "J'entre dans la boucle inc = etat prec" << endl;
            return 0;
        else if(inc!=etat_prec){
            cout << "J'entre dans la boucle inc != etat prec" << endl;
            i = 0;
            etat_prec = inc;
            return 0;
    else if(i==2){
        cout << "le primaire a crashé" << endl;
        return 1;

void watchdog::callback (const primary::Test::ConstPtr& test_msg){

    inc = test_msg->count;
    pub = sub.getNumPublishers();
    cout << "Le compteur vaut : " << inc << endl;
    cout << "Nombre de publisher : " << pub << endl;


void watchdog::subscri(){

    sub = n.subscribe("primwd", 1000, &watchdog::callback,this);

void watchdog::mode_fonct(){


And the main of my WD :

int main (int argc, char **argv) {
    ros::init(argc, argv, "watchdog"); // Initialisation de ROS
    int WD;
    watchdog wd; // Création d'un watchdog



    cout << "Fin d'execution" << endl;

    return 0;

My WD and my Primary are in different packages and i have a wd.h as well but there is no problems with it :

class watchdog {
    int etat_prec,inc,i,pub;
    ros::NodeHandle n; // Création du noeud Watchdog
    ros::Subscriber sub; 
    int check_watchdog();
    void callback (const primary::Test::ConstPtr&);
    void mode_fonct();
    void subscri();
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-03-30 11:38:29 -0600

ahendrix gravatar image

You're making the subscriber much more complicated than it needs to be.

  • ROS will handle subscribing to a new publisher automatically - no need to call subscribe on every iteration
  • ros::spin() doesn't return until your program is done - probably not what you want

The pseudocode for a watchdog should look more like:

class Watchdog {
    // timer or last update time member variables
    void callback(your_message & msg); // update/reset timer
    bool check(); // check timer for timeout

int main() {
   // ros node and nodehandle setup
   Watchdog watchdog;
   ros::Subscriber sub = nh.subscribe("topic", 10, &watchdog, &Watchdog::callback);

   ros::Rate rate(100);

   while(ros::ok()) {
      if(!watchdog.check()) {
        // scream loudly and/or throw things
      // check subscriber for correct number of publishers

   return 0;

Most of the implementation details are left to the reader.

Note that there are two different checks being done here:

  • Check that a message was received recently (the publisher is still publishing)
  • Check that there are still publishers connected

Note also that you could move the ros::Subscriber object to be a member of the Watchdog class, and do the subscriber setup in the Watchdog constructor.

edit flag offensive delete link more


Thank you a lot for your answer, i understand the concept of publishing and subscribing !

Matth gravatar image Matth  ( 2015-04-01 07:52:22 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-03-27 09:44:05 -0600

Seen: 1,566 times

Last updated: Mar 30 '15