Launch files
So far we have been running each node manually which may get tiring.
This is where ROS
launch files come in.
ROS
launch files are files where multiple nodes can be launched from all stored in one place.
First, create a folder called launch
in the root folder of the package and inside launch create a file called python_params_launch.py
inside we first import the ROS
libraries
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
Then we create a function called generate_launch_description()
in this function, we first define which nodes we want to run. In our case we want to replicate the command ros2 run my_package param_test
and setting the parameter of that node to earth
def generate_launch_description():
node = Node(
package='my_package',
executable='param_test',
parameters=[
{'my_parameter': 'earth'}
]
)
Then, we have to return a LaunchDescription
object which takes a list of ROS
nodes we want to run.
def generate_launch_description():
return LaunchDescription([
node
])
NOTE: how this is basically the same as ros2 run my_package param_test
Solution
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
def generate_launch_description():
node = Node(
package='my_package',
executable='param_test',
parameters=[
{'my_parameter': 'earth'}
]
)
return LaunchDescription(
[node]
)
Registering the launch file to the workspace
To register the launch file we have to go into setup.py
and add in 3 different lines shown below:
then build the workspace:
colcon build --symlink-install
and run the launch file with ros2 launch <package name> <launch file name>
Launch arguments
We can also specify arguments to go into launch files for convenience
For example, we don’t want to reopen the launch file to change what param_test
prints out every time.
First, at the top of generate_launch_description()
we would declare a LaunchConfiguration
and DeclareLaunchArgument
object.
LaunchConfiguration
takes the parameter’s name and DeclareLaunchArgument
takes the name of the same parameter and its default value.
def generate_launch_description():
some_arg = LaunchConfiguration('some_arg')
launch_arg = DeclareLaunchArgument( 'some_arg', default_value='test')
we then can pass the LaunchConfiguration
object into the Node object and put the DeclarationLaunchArgument
object into the return array.
def generate_launch_description():
some_arg = LaunchConfiguration('some_arg')
launch_arg = DeclareLaunchArgument( 'some_arg', default_value='test')
node = Node(
package='my_package',
executable='param_test',
parameters=[
# {'my_parameter': 'earth'}
{'my_parameter': some_arg}
]
)
return LaunchDescription(
[launch_arg, node]
)
now we can simply change the parameter in python_params_launch.py
by running
ros2 launch my_package python_params_launch.py some_arg:=hi
Exercise!!
- try to make a launch file for the publisher and subscriber