0
$\begingroup$

Rosanswers logo

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!


Originally posted by siliconlad on ROS Answers with karma: 11 on 2020-09-07

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by siliconlad with karma: 11 on 2021-11-09

This answer was ACCEPTED on the original site

Post score: 0

$\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.