Launch files
So far we have been running each node manual 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
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
Then, we have to return a LaunchDescription
object which takes a list of ROS
nodes we want to run.
NOTE: how this is basically the same as ros2 run my_package param_test
Solution
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:
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.
we then can pass the LaunchConfiguration
object into the Node object and put the DeclarationLaunchArgument
object into the return array.
now we can simply change the parameter in python_params_launch.py
by running
Exercise!! ( hlep me )
- try to make a launch file for the publisher and subscriber