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

Calling launch file from node during runtime

asked 2014-09-22 04:57:09 -0500

bvbdort gravatar image

updated 2014-09-23 03:31:05 -0500

Hi all,

Based on some condition in node, I want to start and stop hector slam . (in parallel)

I had looked at different questions and rosspawn but couldn't figure it out.

if(start_node == true)
// start node in parallel 
// Here my case is hector SLAM
if (stop_node == true)
// Stop the node

Any suggestions ?


Edit ::

I found starting node in another package in python code from Roslaunch-API Usage. I am looking for similar way in C++ also starting nodes with params and arguments.

import roslaunch

package = 'rqt_gui'
executable = 'rqt_gui'
node = roslaunch.core.Node(package, executable)

launch = roslaunch.scriptapi.ROSLaunch()

process = launch.launch(node)
print process.is_alive()
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2014-09-22 08:00:07 -0500

paulbovbel gravatar image

updated 2014-09-22 08:57:50 -0500

AFAIK there is no right way to start and stop nodes dynamically. If you're writing the nodes yourself, it's much better to include pause/resume functionality via services.

As @Ricky stated, you can use the system call, however system will block until the command finishes, meaning you have to use the & control operator in the command to spin off a new process. Then you have the issue of trying to track down/parse the PID in order to shutdown when necessary.

I would recommend using Qt's QProcess, which will make it easier to manage the state of the process (destructing the object will kill the roslaunch process, thus shutting down the nodes).

EDIT: Poco also has nice looking Process management, if you want to avoid messing with Qt. ROS's class_loader CMakeLists is an example of how to include Poco in your package.

edit flag offensive delete link more


Hi Paul, thanks for answer, poco looks better than using direct linux commands, i will try it out. But its not the ROS way of doing.

bvbdort gravatar image bvbdort  ( 2014-09-22 14:30:31 -0500 )edit

The problem with writing everything as a service is that ROS can be a memory hog. Even small trivial nodes, doing nothing, can consume 4% of memory. Get a few dozen of those, and you're already out of memory even though your bot's just sitting there doing nothing.

Cerin gravatar image Cerin  ( 2016-12-10 17:30:04 -0500 )edit

answered 2014-09-22 07:44:49 -0500

Maybe you can try system("roslaunch pkg_name launch_file_name"); in C++.

edit flag offensive delete link more


@Ricky Thanks for reply I tried this, but it stops current node i want to run it parallel with node which is executes system call ,also i want a way to stop it on some condition in runtime.

bvbdort gravatar image bvbdort  ( 2014-09-22 07:56:08 -0500 )edit

Thanks for the explanation~I think @paulbovbel's answer is quite nice.

Po-Jen Lai gravatar image Po-Jen Lai  ( 2014-09-22 08:31:13 -0500 )edit

Question Tools

1 follower


Asked: 2014-09-22 04:57:09 -0500

Seen: 4,082 times

Last updated: Sep 23 '14