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

Remotely Launch Nodes in ROS2

asked 2020-10-26 06:30:10 -0600

smorad gravatar image

Is there a way to remotely launch nodes in ROS2? This could be done in ROS1 using the <machine> attribute. It's listed in the ROS2 design document but I can't figure out how to use it. I'm using the python launch and launch_ros libraries.

edit retag flag offensive close merge delete

Comments

Hi. Same need. I didn't find into the documentation...

resnault gravatar image resnault  ( 2021-04-01 03:34:10 -0600 )edit

4 Answers

Sort by ยป oldest newest most voted
3

answered 2022-04-26 06:23:11 -0600

afrixs gravatar image

updated 2022-04-26 11:59:52 -0600

I've just found a way to perform a remote launch from a python launch file using the ExecuteProcess launch action. There are several problems you're going to face if you go this way, so here are the workarounds I used to solve them (on Ubuntu 20.04).

First, let's state what we are trying to achieve:

  1. We have a main machine A and a machine B reachable via ssh pi@192.168.0.20 from machine A (without a password, i.e. use ssh-keygen -t rsa -b 2048 && ssh-copy-id pi@192.168.0.20 first)
  2. We have a launch file drinks_hmi_launch.py in drinks_hmi_bringup package on machine B (we can launch it from machine B via ros2 launch drinks_hmi_bringup drinks_hmi_launch.py)
  3. We have a launch file drinks_bringup_launch.py in drinks_bringup package on machine A which is supposed (among other actions) to launch drinks_hmi_launch.py on machine B

Now the following command should work from machine A, i.e. it launches the nodes, prints out output and can be terminated via SIGINT (Ctrl+C):

ssh -t pi@192.168.0.20 'bash -i -c "ros2 launch drinks_hmi_bringup drinks_hmi_launch.py"'

However, after putting it directly into ExecuteProcess action in drinks_bringup_launch.py, the process complains about not having a proper tty console and the nodes on machine B are not terminated after terminating the ros2 launch drinks_bringup drinks_bringup_launch.py process.

A workaround is to call the above command inside a detached screen (use sudo apt install screen if not present). This, however, raises two further problems:

  • The screen does not pass SIGINT from calling process to inner processes, so it does not terminate the nodes when Ctrl+C is pressed. However it can be terminated explicitly using a remote_launch_terminator node which calls screen -S hmi -X quit (see the complete solution below)
  • The screen does not pass output from inner processes to calling process, so this needs to be done explicitly by redirecting inner output to outer

The complete solution is as follows:

/home/machine_A/drink_ws/src/launch_utils/launch_utils/remote_launch_terminator.py:

import rclpy
from rclpy.node import Node
import os

def main(args=None):
    rclpy.init(args=args)

    node = Node('remote_launch_terminator')
    node.declare_parameter('screen_pid', 'remote')
    screen_pid = node.get_parameter('screen_pid').get_parameter_value().string_value

    try:
        rclpy.spin(node)
    except:
        pass

    print('Terminating ' + screen_pid)
    os.system('screen -S ' + screen_pid + ' -X quit')

if __name__ == '__main__':
    main()

/home/machine_A/drink_ws/src/drinks_bringup/launch/drinks_bringup_launch.py:

import os

from ament_index_python.packages import get_package_share_directory

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions.execute_process import ExecuteProcess

def generate_launch_description():
    return LaunchDescription([

        # other actions

        # remote launch
        ExecuteProcess(
            name='hmi',
            cmd=['{ outer_stdout=$(readlink -f /proc/self/fd/3); } 3>&1 && screen -DmS hmi bash -i -c "ssh -t pi@192.168.0.20 \'bash -i -c \\"ros2 launch drinks_hmi_bringup drinks_hmi_launch.py\\"\' > $outer_stdout"'],
            output="screen",
            shell=True,
            emulate_tty=True
        ),
        Node(
            package='launch_utils',
            executable='remote_launch_terminator',
            name='remote_launch_terminator',
            output='screen',
            parameters=[{'screen_pid': 'hmi'}]
        ),
    ])

Hope this helps, cheers! ;)

Note: ExecuteProcess action does not work inside OnShutdown event, that's why ... (more)

edit flag offensive delete link more
2

answered 2021-06-07 23:16:00 -0600

shonigmann gravatar image

updated 2021-06-07 23:17:44 -0600

As far as I can tell, this is still a work in progress, but has been on the backlog to get merged into the documentation. Lots of discussion here, and it seems the change to the documentation is awaiting approval to get merged in.

The tentative document can be found here, but it doesn't look like the proposed changes have made their way into code yet.

I'd imagine it is pretty low priority because ssh is possible on all the tier 1 platforms (ever since Windows joined the club).

edit flag offensive delete link more

Comments

1

It has been quite some time since this was posted. Could anyone comment on when support for remote-launching nodes via roslaunch can be expected to make it into ROS2?

alikureishy gravatar image alikureishy  ( 2022-02-17 14:04:33 -0600 )edit
1

answered 2022-09-19 11:50:06 -0600

gerok gravatar image

My answer to this is to use the existing linux system to do this: systemd. Basically do the following:

On the target machine, the machine you want to run remote nodes on, do the following:

  1. Make sure the nodes you want to run are on the remote machine already and it's already built on that machine.
  2. Make a bash script that executes ros2 launch and sources the right install directory. We'll call this remote_ros.sh. This is a script because you might want to do other things with this.
  3. Make a remote_ros.service file that runs the remote_ros.sh script. This can be setup to start on boot or not.

On the local machine, the machine you want to launch remote nodes on, do the following:

  1. Make a launch script that SSHes into the remote machine and executes systemctl stop remote_ros and/or systemctl start remote_ros. This can be done with the ExecuteProcess launch action.

Stop will kill the nodes and start will bring them up. If you don't want to stop the nodes, I believe start is a NOOP if they are already running.

I think this solution is more straight forward than the others presented and ideally the service just runs all the time, but you can always kill it with the systemctl stop command.

edit flag offensive delete link more
0

answered 2023-05-20 07:15:56 -0600

ketill gravatar image

I have found a solution that works for me be using rosbridge to remotely publish service messages to start or stop processes.

  1. First I made rosbridge and a ros2 node start on boot of the device by:

sudo vim /etc/systemd/system/ros2node.service

[Unit]
Description=Start ROS2 Node
After=network.target

[Service]
ExecStart=/bin/bash -c 'source /opt/ros/humble/setup.bash; source 
/home/ketill/Documents/ros2_script/ros2_ws/install/setup.bash; ros2 launch rosbridge_server 
rosbridge_websocket_launch.xml; ros2 run node_starter talker'
User=ketill
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/run/user/1000/gdm/Xauthority"
Restart=always

[Install]
WantedBy=multi-user.target

sudo systemctl enable ros2node.service sudo systemctl start ros2node.service

and similar for the node. 2. The a node for handling the processes depending on the service message for the gui

class NavigationService(Node):

def __init__(self):
    super().__init__('navigation_service')
    self.srv = self.create_service(SetBool, 'start_navigation', self.handle_service)
    self.process = None
    self.hasStartedOnce = False

def handle_service(self, request, response):
    response.success = self.run_navigation_launch_file(request.data)
    response.message = 'Navigation launch started.' if response.success else 'Failed to start navigation launch.'
    return response

def run_navigation_launch_file(self, start):
    if start and not self.hasStartedOnce:
        command = "source /opt/ros/humble/setup.bash; source /home/ggeo/auto_gpr/install/setup.bash; ros2 launch auto_gpr bringup.launch.py"
        self.process = subprocess.Popen(command, shell=True, executable="/bin/bash", preexec_fn=os.setsid)
        self.hasStartedOnce = True
        return self.process.poll() is None  # return True if process is running
    else:
        # Implement logic to stop the process here
        if self.process is not None:
            os.killpg(os.getpgid(self.process.pid), signal.SIGINT)  # Send the signal to all the process in the group
            self.process = None
            self.hasStartedOnce = False
        return False


def main(args=None):
    rclpy.init(args=args)

navigation_service = NavigationService()

rclpy.spin(navigation_service)

navigation_service.destroy_node()
rclpy.shutdown()


if __name__ == '__main__':
main()

This solution make it possible to start remotely as long as the devices are on the same network.

edit flag offensive delete link more

Question Tools

5 followers

Stats

Asked: 2020-10-26 06:27:28 -0600

Seen: 2,495 times

Last updated: Sep 19 '22