Ask Your Question

Non blocking ros::init or roscore launch from c++ ?

asked 2018-01-08 14:19:26 -0500

Rhapsodus gravatar image

Greetings All,

I am new to ROS but have managed to get networked drones communication to my application. I have a particular question though regarding Initialization and the best practices of the startup sequence.

When the app launches I make a call to ros::init. This call returns immediate and ros::ok returns true. However, when there is no roscore running trying to get a ros::NodeHandle hangs the application.

ros::init(argc, argv, "MyApp");

if( ros::ok() )
    ros::NodeHandle handle; //Hangs if no ros core.

My question is thus:

  1. How can I tell in code whether a roscore was running so the application doesn't hang?
  2. Is there a way to launch roscore from c++ if the above case is true.
edit retag flag offensive close merge delete


Welcome! If you use a launch file then roscore is started for you. See

jayess gravatar imagejayess ( 2018-01-08 15:14:10 -0500 )edit

Thanks! I see that I can launch via a launch file, but can you do the same in c++ API? Most people will just want to click on a desktop application icon to launch an app rather than launching via a console and memorizing commands. I Intend to have it installed on many computers.

Rhapsodus gravatar imageRhapsodus ( 2018-01-09 15:38:44 -0500 )edit

Nothing is stopping you running roslaunch from a bash script and creating an icon for that. In fact I have quite a few of these that setup the ROS envrionment variables then startup large systems with launch files.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-01-10 05:17:21 -0500 )edit

Most people will just want to click on a desktop application icon to launch an app

That is quite a statement on a forum for a robotics framework that is heavily used in research and development, but: see #q226588.

gvdhoorn gravatar imagegvdhoorn ( 2018-01-19 02:16:29 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-01-18 16:00:26 -0500

Rhapsodus gravatar image

A colleague of mine pointed me to the call: ros::master::check() which does precisely what I was hoping for

edit flag offensive delete link more


This does solve you're first question. But not the second part, it looks like you'll still need to execute a system call from your node to start roscore if it isn't already running.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-01-19 06:11:27 -0500 )edit

In a GNOME environment, a less than elegant way to launch roscore if ros::master::check() returns false.

system("gnome-terminal -- roscore");

You likely want to wait for the rosmaster and roscore processes to start before continuing so you could do something like:

ros::init(argc, argv, "MyApp");

if (!ros::master::check()) {

    system("gnome-terminal -- roscore");    // opens roscore in a new terminal

    int waitForRoscoreMax = 60; // After 60 * 0.5 = 30 seconds stop trying
    int iterCount = 0;
    while (!ros::master::check()) {  // Check every half second
        if (iterCount < waitForRoscoreMax) {
        } else {
            ROS_ERROR_STREAM("Unable to start 'roscore'. Aborting...");
            return 0;  // Assuming we are in 'int main(...)'

if( ros::ok() )
    ros::NodeHandle handle; // Shouldn't hang anymore
scd5027 gravatar imagescd5027 ( 2019-10-03 16:38:58 -0500 )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: 2018-01-08 14:19:26 -0500

Seen: 236 times

Last updated: Jan 18 '18