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

How to handle disconnection/reconnection to the ROS master

asked 2015-02-03 11:43:20 -0500

techno74 gravatar image

I have written a ROS node, just publishing to a topic, using roscpp that runs on a separate machine to the ROS master. It is based on the simple publisher/subscriber tutorials. If the ROS master gets restarted my node no-longer appears when I query the new master with rosnode list. It needs to be able to detect that the ROS master has gone down and re-advertise it's topics when it comes back up. I haven't managed to get this to work without killing and restarting my entire process.

I can detect that the connection to the ROS master has dropped by periodically checking ros::master::check () (would love a better way). When it returns true again after the master comes back up, none of the following work:

1) Call .advertise() on the old NodeHandle and get a new publisher. I thought this would work but it doesn't seem to.

2) Create a new NodeHandle and re-advertise on that.

3) Re-run ros::init

4) Call ros::shutdown and then call ros::init core dumps.

Also: I'm assuming ros::master::check () returns false if the connection is interrupted as well as if the master is restarted, do I need to handle that case differently or will re-advertising on the same master be OK?

edit retag flag offensive close merge delete


Ever find a solution to this?

EpicZa gravatar image EpicZa  ( 2018-03-21 12:54:08 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2015-02-05 06:53:29 -0500

techno74 gravatar image

Wrapping the ros::init() and NodeHandle creation seems to work. In a bigger project where I have passed the ROS nodeHandle around and attached publishers and things to it I can't get it to work yet.


#include "ros/ros.h"

#include "std_msgs/String.h"

#include "rosConnection.H"

#include <sstream>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
    int count = 0;
    bool reconnect = false;

        rosConnectionHandler_t rc (argc, argv);

        ros::Publisher chatter_pub = rc.nodeHandle ()->advertise<std_msgs::String>("chatter", 1000);

        ros::Rate loop_rate(10);

        while (ros::ok() && ros::master::check())
            std_msgs::String msg;

            stringstream ss;
            ss << "hello world " << count;
   = ss.str();
            cout << << endl;


        cout << "ROS OK: " << (ros::ok()?"true":"false") << endl;
        cout << "MASTER UP: " << (ros::master::check()?"true":"false") << endl;

        reconnect = !ros::master::check() && ros::ok();

        if (reconnect)
            cout << "Attempting to reconnect" << endl;

    } while (reconnect);

    return 0;


#pragma once

#include <ros/ros.h>

class rosConnectionHandler_t
        rosConnectionHandler_t (int argc_, char** argv_)
            ros::init (argc_, argv_, "testnode");

            nh = new ros::NodeHandle();

        ~rosConnectionHandler_t ()
            delete nh;

        ros::NodeHandle *nodeHandle() { return nh; };

        ros::NodeHandle *nh;
edit flag offensive delete link more


This works but for some reason ROS_INFO_STREAM, ROS_WARN_STREAM and ROS_ERROR_STREAM does not seem to work after the first re-connection

VictorLamoine gravatar image VictorLamoine  ( 2018-03-30 02:33:21 -0500 )edit

answered 2018-06-26 15:37:18 -0500

murali1842 gravatar image

updated 2018-11-27 19:02:34 -0500

jayess gravatar image

An example in Python:

import rosgraph

if rosgraph.is_master_online():    
     print 'ROS MASTER is Online'
     print 'ROS MASTER is Offline'
edit flag offensive delete link more


Can you please provide an explanation to go along with your code? Code with an explanation is much more valuable than code alone.

jayess gravatar image jayess  ( 2018-11-27 19:00:47 -0500 )edit

That code is right for detecting that you lost connection, but it is not enough to solve the issues created inside the ropy module state. It will crash if you execute init_node again...if you dont call init_node.. the node communication will not work.

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2020-02-14 12:06:44 -0500 )edit

Question Tools



Asked: 2015-02-03 11:43:20 -0500

Seen: 4,616 times

Last updated: Nov 27 '18