Knowhow/ROS2

[ROS2 Foxy Tutorial 한글 번역] 9. Launching nodes

침닦는수건 2023. 2. 3. 12:10
반응형

Link

https://docs.ros.org/en/foxy/Tutorials/Beginner-CLI-Tools/Launching-Multiple-Nodes/Launching-Multiple-Nodes.html

 

Launching nodes — ROS 2 Documentation: Foxy documentation

Goal: Use a command line tool to launch multiple nodes at once. In most of the introductory tutorials, you have been opening new terminals for every new node you run. As you create more complex systems with more and more nodes running simultaneously, openi

docs.ros.org

 

Background

앞선 튜토리얼들에서 노드를 실행할 때마다 새로운 터미널을 열어왔다. 그런데 시스템이 점점 더 복잡해지고 노드 수가 늘어나는 상황이 되면 이러한 방식 대신 동시에 여러 노드를 실행하는 방식이 필요하다. 복잡한 시스템에서도 하나 하나 노드마다 터미널을 사용하는 것은 너무나 귀찮은 일이다.

 

Lauch file은 ROS2 노드들은 동시 실행함과 동시에 설정값도 동시에 정할 수 있도록 도와준다. 

 

하나의 launch file을 "ros2 launch" 커맨드를 통해 실행하면 모든 노드와 모든 configuration을 동시에 생성하고 적용한다.

 

Prerequisites

본 튜토리얼은 시작하기 전 ROS2 installation 페이지의 내용을 따라 설치했는지 확인해야 한다.

 

아래 사용될 커맨드들은 binary pacakage installation을 통해 설치했을 경우를 가정한다. Linux일 경우, Debian package를 이용해 설치했어야 한다. 만약 source로 부터 직접 build했을 경우 방법이 없는 것은 아닌데 setup file 까지의 경로를 좀 다르게 설정해야 한다. 또한 "sudo apt install ros-<distro>-<package>" 커맨드도 안 통할 것이라 패키지 설치가 조금 더 까다로워 진다. 

 

Tasks

Runiing a Launch File

터미널을 열고 다음 커맨드를 실행한다.

ros2 launch turtlesim multisim.launch.py

위 커맨드는 다음과 같은 lanuch file을 실행시킨다. (python 외는 기록하지 않겠다.)

# turtlesim/launch/multisim.launch.py

from launch import LaunchDescription
import launch_ros.actions

def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            namespace= "turtlesim1", package='turtlesim', executable='turtlesim_node', output='screen'),
        launch_ros.actions.Node(
            namespace= "turtlesim2", package='turtlesim', executable='turtlesim_node', output='screen'),
    ])

내용대로 두 개의 turtlesim 노드를 동시 실행시킨다.

launch file의 작성법은 여기서 다루지 않는다. 만약 관심이 있다면 튜토리얼 페이지에서 ROS2 launch tutorials를 참고하면 되겠다.

 

(Optional) Control the Turtlesim Nodes

지금 2개의 노드가 실행되고 있는데 각각 "ros2 run"으로 실행시킨 것처럼 컨트롤할 수 있다. 예를 들어, 왼쪽 거북이와 오른쪽 거북이를 각각 다른 터미널을 열어 커맨드를 입력함으로써 서로 다르게 움직이게 할 수 있다. 

 

각각 다른 새로운 터미널에 다음 커맨드를 입력해보자.

ros2 topic pub  /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
ros2 topic pub  /turtlesim2/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"

다음과 같은 결과를 얻을 수 있을 것이다.

 

Summary

이 튜토리얼에서 배운 것은 여러 개의 turtlesim 노드를 단 하나의 커맨드로 실행하는 방법이다. launch file을 작성하는 방법을 배우면 동시 실행 뿐만 아니라 동시 configuration도 가능할 것이다. 

 

launch file에 대해서 배우고 싶다면 메인 페이지에서 launch tutorial을 따로 찾아보라.

반응형