5. ROS2launch file is launched

In ROS2, launch is used to start multiple nodes and configure program running parameters. The launch file format of ROS2 is xml, yaml, and python. This lesson uses the launch file in python format as an example, which is more flexible than the other two formats:

Using python language to write ROS2 launch file, the most important thing is to abstract each node, file, script, etc. into an action, with a unified interface to start, the main structure is:

1. Create a folder to store the launch file

We create a new folder under the path of the function package created before to store the launch file, terminal input,

image-20230504151311185

The launch file is usually named LaunchName_launch.py, where LaunchName is custom and _launch.py is usually considered fixed. You need to modify the setup.py file in the function package to add the file in the launch path, so that the.py file can be generated by compiling.

image-20230504151546895

2. Write a single Node launch

Terminal input,

Copy the following into the file,

2.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

2.2. Run the program

Terminal input,

image-20230504153403183

After the program runs, it will run on the node of the turtle.

2.3 Source code analysis

1).Import relevant libraries

After the program runs, it will run on the node of the turtle.

2). Define a function generate_launch_description and return a launch_description

Defines a variable turtle_node as the return value of a Node start, calls the node function, launches two important parameters, package and executable.

The variable launch_description is then defined as the return value of the LaunchDescription function, which can be added later if multiple nodes are started.

Finally, return launch_description.

3. Write multiple Node launches

Terminal input,

Copy the following into the file,

3.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

3.2. Run the program

Terminal input,

The terminal does not print content, we can check which nodes start to verify whether there is a successful start, terminal input,

image-20230504175912359

As can be seen from the figure above, two nodes are started, corresponding to the two programs in the launch file.

3.3 Source code analysis

It's roughly the same as simple_node_launch.py, but with an extra node and an extra node in launch_description = LaunchDescription([pub_node,sub_node]).

4. Topic name mapping in launch file

Terminal input,

Copy the following into the file,

4.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

4.2. Run the program

Let's see what the speed of the baby turtle is before we remap the topic what the topic name is, terminal input,

image-20230504181824461

The topic here is /turtle1/cmd_vel.

Run the program after remapping the topic, see what the speed topic name of the turtle subscription is, terminal input,

image-20230504182212983

As can be seen from the figure above, the speed topic name is remapped, and the mapped speed topic name of the baby turtle is /cmd_vel.

4.3 Source code analysis

The original single_node_launch.py has been modified, mainly adding the following parts:

This is where the original /turtle1/cmd_vel is remapped to /cmd_vel. The original topic name in the front, and the topic name we want to change into the back.

5. launch file launch the launch file

Terminal input,

Copy the following into the file,

5.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

5.2. Run the program

Terminal input,

The launch file will contain two launch files, simple_node_launch.py and multi_node_launch.py. You can check whether the nodes of these launch files are started by the following command, terminal input,

image-20230504191621429

Three nodes were indeed started, so it was successful.

5.3 Source code analysis

6. Configure rosparam with launch file parameters

Terminal input,

Copy the following into the file,

6.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

6.2. Run the program

Terminal input,

image-20230505095640547

After the program runs, it will load the set parameters, modify the default RGB parameters, and change the color of the background board.

6.3 Source code analysis

argument and param are both parameters, but argument is passed in the launch file, and param is passed in the node program.

7. launch file load parameter configuration table

First create a parameter table, terminal input,

Copy the contents into the turtle_config.yaml file,

Save and exit, then modify the setup.py file, the path to load the parameter file, terminal input,

image-20230518122218693

In the position shown in the above picture, add the following,

Save and exit, finally write the launch file, terminal input,

Copy the following into the file,

 

7.1. Compile the workspace

Terminal input,

After compiling, refresh the environment variables in the workspace,

7.2. Run the program

Terminal input,

image-20230505122336162

Run the program to get the turtle and the background color is set to black by parameter.

7.3 Source code analysis

Let's look at the parameter file turtle_config.yaml,

The location of the parameter file is ~/ros2_ws/src/topic_pkg/config