-
[ROS2] Launch 파일 작성드론/시뮬레이터 2022. 6. 6. 12:03
launch 명령어 구성은 아래와 같다.
ros2 launch <패키지 이름> <launch 파일 이름>
그리고 launch파일은 .launch.py로 되어있다.
launch파일을 작성하는 방식은 여러 방식이 있다고 한다.
ExecuteProcess 방식
launch파일을 작성하는 방식 중 하나다.
이 방식을 이용해서 launch파일을 작성해보자
먼저, 아래 명령어로 launch파일을 만들자.
$ cd ~/gcamp_ros2_ws/src/gcamp_ros2_basic/gcamp_gazebo/launch $ touch first_launch.launch.py $ gedit first_launch.launch.py
그럼 사진처럼 자동으로 파일이 생성되고 열린다.
만약, 내용이 없다면 아래 코드를 입력해 준다.
#!/usr/bin/env python3 import os from launch import LaunchDescription from launch.actions import ExecuteProcess, IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node # this is the function launch system will look for def generate_launch_description(): # create and return launch description object return LaunchDescription( [ ExecuteProcess( cmd=["ros2", "run", "rviz2", "rviz2"], output="screen" ), ] )
그리고 파일을 저장 후 닫고, 아래 명령어로 빌드를 하고 실행해보자
$ cd ~/gcamp_ros2_ws $ cbp gcamp_gazebo $ rosfoxy $ ros2 launch gcamp_gazebo first_launch.launch.py
구조 설명은 아래와 같다.
- generate_launch_description : ros2 launch 커멘드를 통해 launch file을 실행시키면, 이 이름을 가진 함수를 찾아들어갑니다. 그래서 모든 launch file에는 빠지지 않고 제일 먼저 등장하는 함수입니다.
- LaunchDescription : 이 부분에서는 말 그대로, 어떤 Node들을 실행시킬지 기술해둔 Description을 작성합니다. 다만 특정한 규칙으로 실행시킨 Node에 대한 옵션을 지정해주어야 하며, 여러 Node들의 실행이 필요할 수 있기에, 기본적으로 list의 구조를 가짐을 파악할 수 있습니다.
- ExecuteProcess : 프로세스 하나를 실행시킨다는 구분의 개념으로 생각하시면 좋습니다.
- cmd : 앞에서 살펴본 바와 같이 터미널에서 입력하는 커멘드를 그대로 실행하고자 할 시에 사용 됩니다. 입력은 list 형태로 주어지며, space를 기점으로 나누어 주면 됩니다.
- output : Error log 등의 output이 출력되는 수단을 입력합니다.
출처 : https://puzzling-cashew-c4c.notion.site/ROS-2-Launch-launch-file-55c2125808ef4b64bade278852b37d6e
ROS 2 Launch, launch file 작성
여러 Node들을 한번에 실행할 수 있는 launch와, 실습을 통해 launch 파일의 작성법을 알아보도록 하겠습니다.
puzzling-cashew-c4c.notion.site
Node 방식
먼저, 아래 명령어로 launch파일을 만들자.
$ cd ~/gcamp_ros2_ws/src/gcamp_ros2_basic/gcamp_gazebo/launch $ touch second_launch.launch.py $ gedit second_launch.launch.py
만약, 코드 없이 나오면 아래 코드를 입력하고 저장 후 파일을 닫는다.
#!/usr/bin/env python3 import os from launch import LaunchDescription from launch.actions import ExecuteProcess, IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node # this is the function launch system will look for def generate_launch_description(): turtlesim_node = Node( package='turtlesim', executable='turtlesim_node', parameters=[], arguments=[], output="screen", ) turtlesim_teleop_node = Node( package='turtlesim', executable='draw_square', parameters=[], arguments=[], output="screen", ) # create and return launch description object return LaunchDescription( [ turtlesim_node, turtlesim_teleop_node, ] )
그리고 아래 명령어로 빌드하고 실행해보자
$ cd ~/gcamp_ros2_ws $ cbp gcamp_gazebo $ rosfoxy $ ros2 launch gcamp_gazebo second_launch.launch.py
파일의 구조 설명은 다음과 같다.
- Node : Node 하나를 실행시킬 수 있는 옵션입니다.
- package : 실행시킬 Node가 포함된 package를 선택해줍니다.
- executable: c++ Node의 경우, colcon build를 하면 실행 가능한 프로그램이 생성됩니다. python의 경우도 추가 작업이 필요한데요, 우선 지금은 기존 커멘드의 마지막 인자라고 생각하시면 좋습니다.
- parameters : 실행시킬 Node의 추가 매개변수가 있다면 여기 추가됩니다.
- arguments , name, remappings 등 여러 옵션들이 있지만 필요시 그때마다 설명하도록 하겠습니다.
출처 : https://puzzling-cashew-c4c.notion.site/ROS-2-Launch-launch-file-55c2125808ef4b64bade278852b37d6e
ROS 2 Launch, launch file 작성
여러 Node들을 한번에 실행할 수 있는 launch와, 실습을 통해 launch 파일의 작성법을 알아보도록 하겠습니다.
puzzling-cashew-c4c.notion.site
'드론 > 시뮬레이터' 카테고리의 다른 글
[ROS 2] Topic (0) 2022.06.06 Gazebo Simulation 실행 및 동작 테스트 (0) 2022.05.29 ROS2 환경 구성 (0) 2022.05.19 ROS2 강의 (0) 2022.05.19 PX4 Gazebo설치 및 동작 (0) 2022.05.12