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

Run roscore from python

asked 2018-04-24 04:46:15 -0500

ejalaa12 gravatar image

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2018-04-24 05:14:47 -0500

ejalaa12 gravatar image

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 :)

edit flag offensive delete link more

Comments

This is the way. It worked for me

BatmanofZNA gravatar image BatmanofZNA  ( 2023-04-13 20:49:52 -0500 )edit
0

answered 2018-05-28 02:45:12 -0500

thinwybk gravatar image

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.

Just for completion in case you want to use the subprocess approach: You need to cleanly kill all processes like described in this example.

edit flag offensive delete link more

Comments

That's a great example. Thanks! I'll have a look and try it :)

ejalaa12 gravatar image ejalaa12  ( 2018-06-27 04:26:41 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-04-24 04:46:15 -0500

Seen: 1,754 times

Last updated: May 28 '18