ROS 2 Launch Files
Launch files are Python-based scripts that manage the startup of multiple nodes and configurations simultaneously. They allow users to define which nodes to launch, pass parameters, remap topics, and set arguments, streamlining the deployment and orchestration of complex robotics systems.
Creating Launch File Package
When creating a launch file in ROS 2 you have to use the ros2 pkg create package_name
. Once the package is created delete all the folders in the package folder and create a folder called launch
inside this folder you can put all the launch files. Finally edit the CMakeLists.txt
so that it looks like the one below.
Creating Python Launch File
Below is a example of a launch file.
Adding configs and UFDRs in Launch File Args
When you need to add a file or a config like when creating a rviz node you can take a few quick steps. First create another folder in the same directory you have the launch file. Next navigate to the CMakeList.txt
file and and add a space followed by the name of the folder you create. In this case I am creating an folder called rviz
and I will add it to the end of line 12
.
Now once it is added you will need to reference it in you launch file. Doing this is slightly different in python and xml but it is not two hard.
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python import get_package_share_directory
import os
def generate_launch_description():
rviz_config_path = os.path.join(get_package_share_directory('my_robot1_description'), 'rviz', 'urdf_config.rviz')
rviz2_node = Node(
package="rviz2",
executable="rviz2",
arguments=['-d', rviz_config_path]
)
return LaunchDescription([rviz2_node])
Using Launch File
To run a launch file use the ros2 launch
command.