Ask Your Question
1

Launching and killing nodes within a Python node in ROS 2 Foxy

asked 2020-09-07 19:47:53 -0500

I'm in the process of migrating a ROS Melodic python node into a ROS 2 Foxy python node. This particular node acts as the state machine for our software stack. It receives a message from another part of the system and, depending on the message, will launch a set of nodes described in a launch file.

Previously in ROS melodic we handled this with:

    uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
    roslaunch.configure_logging(uuid)
    LAUNCHED_NODES = roslaunch.parent.ROSLaunchParent(uuid, [full_path])
    LAUNCHED_NODES.start()

where full_path is the absolute path to a launch file.

Then, when certain conditions are met it will kill these nodes with

LAUNCHED_NODES.shutdown()

Is there a way to do this in ROS 2 Foxy within a Python node? The nodes that get launched can't block the original node from running. I've played around with launch_service, but I can't get it to stop blocking the original node from continuing to execute.

Any tips would be greatly appreciated!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-11-09 07:36:44 -0500

So the way I ended up solving this problem is by running the ros2 launch command in a Python subprocess:

launch_process = subprocess.Popen(["ros2", "launch", relative_path_to_launch_file], text=True)

Then to kill the nodes, I can simply run

launch_process.send_signal(SIGINT)
launch_process.wait(timeout=30)

When I posted the question, this wasn't working in Foxy. I think it might've been related to these issues here and here. But it works now. It also works with Galactic.

edit flag offensive delete link more
0

answered 2021-11-14 20:17:36 -0500

guru_florida gravatar image

updated 2021-11-14 20:19:12 -0500

You can also use the Launch API to launch "launch.py" files and monitor them. You can see how I do this in launch_a_launch_file(...) function on line 323 this gist for Model Training.

I used this py script to learn system variables through episodic training.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-09-07 15:40:35 -0500

Seen: 773 times

Last updated: Nov 14 '21