How to use launch files
-
Create a new package for the launch files. Nice to have them separated as in the case of interfaces. And also, the launch files will probably use nodes from different packages, so they don’t really fit inside any of the other ones.
ros2 pkg create my_robot_bringup
-
Remove
/include
and/src
directories inside the new package. -
Create a
/launch
directory inside the new package. -
In the
CMakeLists.txt
file, remove theDefault to C99
section. Also remove theif(BUILD_TESTING)
block. -
Add instructions on how to install the launch files (right after
find_package(...)
section):install(DIRECTORY launch DESTINATION share/${PROJECT_NAME} )
-
Create a launch file inside the
/launch
folder, e gmy_robot_app.launch.py
. -
Make it executable:
chmod +x launch/my_robot_app.launch.py
. -
Add the minimal piece of code to the launch file:
from launch import LaunchDescription def generate_launch_description(): ld = LaunchDescription() return ld
-
Build the package and source your terminal. Then run:
ros2 launch my_robot_bringup my_robot_app.launch.py
-
Add configuration on how to run your application in the launch file. See
src/my_robot_bringup/launch/number_app.launch.py
for reference. -
Add exec dependencies to the
package.xml
. Seesrc/my_robot_bringup/package.xml
for reference.
Use arguments in launch files like this:
- Show available args:
ros2 launch my_bringup robot.launch.py --show-args
- Pass args:
ros2 launch my_bringup robot.launch.py my_arg:=goodbye
- Declare in python code:
my_arg = actions.DeclareLaunchArgument("my_arg", default_value="hello")
. And:ld.add_action(my_arg)
- Pass to node:
parameters=[{"my_node_arg": LaunchConfiguration("my_arg")}]
.