The use of ROS 2 launch files seems scattered. In this repository we demonstrate how to structure ROS 2 launch files as Mixins. This offers some advantages:
- Code completion (so to understand what nodes / arguments a package offers / needs)
- Flexible composition (mix and match)
- Importable (but launch files inclusion still possible)
-
Include launch file
from launch.actions import IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import PathJoinSubstitution from launch_ros.substitutions import FindPackageShare
-
Mixins
from launch_mixins.demo_launch import TurtleSimMixin, HelloWorldMixin
-
For the general setup, do:
- In package.xml, add
<builtool_depend>ament_cmake_python</buildtool_depend>
- In CMakeLists.txt, add
find_package(ament_cmake_python REQUIRED) ... # install mixins ament_python_install_package(launch_mixins)
- Put your Mixins into launch_mixins/demo_launch folder (or your desired name). Add launch_mixins/__init__.py and launch_mixins/demo_launch/__init__.py file. Replace
demo_launch
by your package name.
Create Mixin classes for your package, see mixins.py:
- For each node, launch argument etc. create a static method
- Prefix them with
node_
for nodes andarg_
for arguments (this helps code completion)
An example use is shown in the turtlesim_hello_world.launch.py launch file. You'll do things like
ld = LaunchDescription()
# add turtlesim
ld.add_action(TurtleSimMixin.arg_node_name())
ld.add_action(TurtleSimMixin.node_turtlesim())
But you might also keep references (for event handlers)
turtlesim_node = TurtleSimMixin.node_turtlesim()
You can also create new classes
class MyNewLaunch(HelloWorldMixin, TurtleSimMixin):
pass
For code completion extend the Python path:
- In settings.json add
{ "python.envFile": "${workspaceFolder}/.env" }
- In .env add (requires
colcon build
this package)PYTHONPATH=../../install/launch_mixins/local/lib/python3.10