0
$\begingroup$

Rosanswers logo

Hello,

I am trying to automate the launch of multiple nodes with the roslaunch API. So far, I am able to do what I want, and my nodes are running perfectly. (I am also able to stop them).

Now, I noticed that the roslaunch API doesn't work if no roscore is running (in a terminal)..

So I am trying to start the roscore at the beginning of my script (before starting my nodes). What I tried so far (also described here) is:

import subprocess
roscore_process = subprocess.Popen('roscore')

However, I am unable to stop the roscore process with roscore_process.kill().

It seems that the roscore program creates a parent process that spawns multiple children processes (i assume: rosmaster, parameter server, rosout...). This can also be seen with top or htop. Therefore calling roscore_process.kill() only kills one of the children.

Do you have any idea on how to start and stop a roscore ?

Thank you


Originally posted by ejalaa12 on ROS Answers with karma: 81 on 2018-04-24

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Haaa !

After digging in the documentation of the roslaunch API, I was able to find a solution.

The idea is to use a ROSLaunchParent which is used to start/shutdown .launch files. According to the documentation a ROSLaunchParent can be a roscore instance if the parameter is_core is set to True.

So here is my solution:

from roslaunch.parent import ROSLaunchParent

parent = ROSLaunchParent(run_id, [], is_core=True)     # run_id can be any string
parent.start()
# [... do other stuff ...]
parent.shutdown()

The trick is to pass an empty list of roslaunch_files, so that we just use the functionality of the class to start a roscore.

Hope this helps others :)


Originally posted by ejalaa12 with karma: 81 on 2018-04-24

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by BatmanofZNA on 2023-04-13:
This is the way. It worked for me

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.