Welcome to Raspberry Pi 4B repository
5.ROS2launch file starts
5、ROS2launch file starts
In ROS2, launch is used for multi-node startup and configuring program running parameters, and ROS2's launch file formats include XML, Yaml and Python formats. This lesson takes the launch file in Python format as an example, compared to the other two formats, the Python format is more flexible:
- Python has a large number of libraries that can be used in startup files;
- ROS2 generic boot features and specific boot features are written in Python, so XML and YAML boot features that may not be exposed can be accessed;
Using python language to write ROS2 launch files, 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,
def generate_launch_description(): return LaunchDescription([ action_1, action_2, ... action_n ])1、Create a folder to store the launch file
Under the path of the function package created earlier, we create a new folder to store the launch file and terminal input.
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkgmkdir launch

Launch files are often named in LaunchName_launch.py, where LaunchName is custom, _launch.py is often considered fixed. You need to modify the setup.py file under the function pack, modify the content to add the file under the launch path, and compile to generate the executed .py file.
xxxxxxxxxx#1、Import the relevant header filesimport osfrom glob import glob#2、In the list of data_files, add the launch path and the launch.py files under the path(os.path.join('share',package_name,'launch'),glob(os.path.join('launch','*launch.py')))
2、Write a launch for a single node node
terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit single_node_launch.pyCopy the following into the file,
xfrom launch import LaunchDescriptionfrom launch_ros.actions import Node
def generate_launch_description(): turtle_node = Node( package='turtlesim', executable='turtlesim_node', ) launch_description = LaunchDescription([turtle_node]) return launch_description2.1、Compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
2.2、Run the program
terminal input,
xxxxxxxxxxros2 launch topic_pkg single_node_launch.py

After the program runs, it will run the baby turtle node.
2.3、Source code analysis
1) 、Import related libraries
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Node
2)、Defines a function generate_launch_description and returns a launch_description
xxxxxxxxxxdef generate_launch_description(): turtle_node = Node( package='turtlesim', executable='turtlesim_node', ) launch_description = LaunchDescription([turtle_node]) return launch_descriptionDefine a variable turtle_node return value for starting as a node, call the Node function, and launch two important parameters, package and executable.
- package: Indicates the function package, representing the name of the feature package.
- executable: indicates the executed program, the name of the executable program.
Then define a variable launch_description as the return value after calling the LaunchDescription function, and add it after multiple nodes are started.
xxxxxxxxxxlaunch_description = LaunchDescription([turtle_node])
Finally, return launch_description.
3、Write launch for multiple node nodes
terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit multi_node_launch.pyCopy the following into the file,
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Node
def generate_launch_description(): pub_node = Node( package='topic_pkg', executable='publisher_demo', output='screen' ) sub_node = Node( package='topic_pkg', executable='subscriber_demo', output='screen' ) launch_description = LaunchDescription([pub_node,sub_node]) return launch_description3.1、compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
3.2、Run the program
terminal input,
xxxxxxxxxxros2 launch topic_pkg multi_node_launch.py
The terminal does not print content, we can see which nodes are started to verify that there is a successful startup, the terminal inputs,
xxxxxxxxxxros2 node list

As can be seen from the above figure, two nodes are started, which correspond to the two programs in the launch file.
3.3、Source code parsing
It's about the same as simple_node_launch.py, but with one more node, and an extra node in launch_description = LaunchDescription([pub_node,sub_node]).
4、Topic name mapping in launch files
terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit remap_name_launch.py
Copy the following into the file,
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch_ros.actions import Node
def generate_launch_description(): turtle_node = Node( package='turtlesim', executable='turtlesim_node', remappings=[("/turtle1/cmd_vel", "/cmd_vel")] ) launch_description = LaunchDescription([turtle_node]) return launch_description4.1、compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
4.2、Run the program
Let's first see what is the name of the little turtle's speed topic before remapping the topic, terminal input,
xxxxxxxxxxros2 launch topic_pkg single_node_launch.pyros2 topic list

The topic here is /turtle1/cmd_vel.
Then run the program after remapping the topic, see what is the name of the speed topic that the little turtle subscribes to, and enter it in the terminal,
xxxxxxxxxxros2 launch topic_pkg remap_name_launch.pyros2 topic list
As can be seen from the figure above, the speed topic name is remapped, and the mapped baby turtle speed topic name is /cmd_vel.
4.3、Source code analysis
Modifications have been made on the basis of the original single_node_launch.py, mainly adding the following parts,
xxxxxxxxxxremappings=[("/turtle1/cmd_vel", "/cmd_vel")]
This is to remap the original /turtle1/cmd_vel to /cmd_vel. The original topic name in front is the topic name we want to change to.
5、 Edit the launch file
Terminal input
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit include_launch.py
Copy the following into the file
xxxxxxxxxximport osfrom ament_index_python.packages import get_package_share_directoryfrom launch import LaunchDescriptionfrom launch.actions import IncludeLaunchDescriptionfrom launch.launch_description_sources import PythonLaunchDescriptionSourcedef generate_launch_description(): node1 = IncludeLaunchDescription( PythonLaunchDescriptionSource([os.path.join( get_package_share_directory('topic_pkg'), 'launch'), '/multi_node_launch.py']) ) node2 = IncludeLaunchDescription( PythonLaunchDescriptionSource([os.path.join( get_package_share_directory('topic_pkg'), 'launch'), '/single_node_launch.py']) ) return LaunchDescription([node1,node2])5.1、compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
5.2、Run the program
terminal input,
xxxxxxxxxxros2 launch topic_pkg include_launch.py
This launch file will contain two launch files, simple_node_launch.py and multi_node_launch.py. You can check whether the node of these launch files is started by the following command, terminal input,
xxxxxxxxxxros2 node list

It is true that three nodes are started, so the startup is successful.
5.3、Source code analysis
xxxxxxxxxx#Import the necessary library filesfrom ament_index_python.packages import get_package_share_directoryfrom launch import LaunchDescriptionfrom launch.actions import IncludeLaunchDescriptionfrom launch.launch_description_sources import PythonLaunchDescriptionSourcenode1 = IncludeLaunchDescription( PythonLaunchDescriptionSource([os.path.join( get_package_share_directory('topic_pkg'), 'launch'), '/multi_node_launch.py']) )- os.path.join(get_package_share_directory('topic_pkg'): Gets the location of the feature pack, where 'topic_pkg' is the name of the feature pack;
- launch'): indicates the folder that stores the launch file under the function pack;
- /multi_node_launch.py': indicates the name of the launch file under the launch file in the package folder, which is /multi_node_launch.py in the example.
6、Launch file parameter configuration rosparam
terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit param_launch.py
Copy the following into the file,
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch.actions import DeclareLaunchArgumentfrom launch.substitutions import LaunchConfiguration, TextSubstitutionfrom launch_ros.actions import Node
def generate_launch_description(): background_r_launch_arg = DeclareLaunchArgument( 'background_r', default_value=TextSubstitution(text='0')) background_g_launch_arg = DeclareLaunchArgument( 'background_g', default_value=TextSubstitution(text='225')) background_b_launch_arg = DeclareLaunchArgument( 'background_b', default_value=TextSubstitution(text='0')) return LaunchDescription([ background_r_launch_arg, background_g_launch_arg, background_b_launch_arg, Node( package='turtlesim', executable='turtlesim_node', name='sim', parameters=[{ 'background_r':LaunchConfiguration('background_r'), 'background_g':LaunchConfiguration('background_g'), 'background_b':LaunchConfiguration('background_b'), }] ) ]) 6.1、compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
6.2、Run the program
terminal input,
xxxxxxxxxxros2 launch topic_pkg param_launch.py

After the program runs, it will load the set parameters, modify the default RGB parameters, and change the color of the background panel.
6.3、Source code parsing
xxxxxxxxxxfrom launch.actions import DeclareLaunchArgument #Declare the Argument class used in the launch file background_r_launch_arg = DeclareLaunchArgument( 'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_r 'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_g 'background_r', default_value=TextSubstitution(text='0')) #Create a Launch file with parameter background_b background_r_launch_arg, #Call the parameter created above background_g_launch_arg, background_b_launch_arg, parameters=[{ #ROS parameter list 'background_r': LaunchConfiguration('background_r'), #Create parameters background_r 'background_g': LaunchConfiguration('background_g'), #Create parameters background_g 'background_b': LaunchConfiguration('background_b'), #Create a parameter background_bArgument and param both mean parameters, but argument is passed in the launch file, and param is passed in the node program.
7、launch file loads the parameter configuration table
First create a new parameter table, terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkgmkdir configcd configgedit turtle_config.yaml
Copy the following contents into the turtle_config.yaml file,
xxxxxxxxxxsim ros__parameters background_r0 background_g0 background_b7Exit after saving, then modify the setup.py file, the path to the loading parameter file, terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkggedit setup.py

In the image above, add the following,
xxxxxxxxxx(os.path.join('share', package_name, 'config'), glob(os.path.join('config', '*.yaml'))),Exit after saving, finally write the launch file, terminal input,
xxxxxxxxxxcd ~/ros2_ws/src/topic_pkg/launchgedit param_config_launch.py
Copy the following into the file,
xxxxxxxxxximport osfrom launch import LaunchDescriptionfrom launch_ros.actions import Nodefrom ament_index_python.packages import get_package_share_directorydef generate_launch_description(): config = os.path.join( get_package_share_directory('topic_pkg'), 'config', 'turtle_config.yaml' ) return LaunchDescription([ Node( package='turtlesim', executable='turtlesim_node', name='sim', parameters=[config] ) ]) 7.1、compile the workspace
terminal input,
xxxxxxxxxxcd ~/ros2_wscolcon build
After the compilation is completed, refresh the environment variables of the workspace.
xxxxxxxxxxsource ~/ros2_ws/install/setup.bash
7.2、Run the program
terminal input,
xxxxxxxxxxros2 launch topic_pkg param_config_launch.py

Running the program can get baby turtles and the background plate color is set to black by parameters.
7.3, Source code parsing
xxxxxxxxxx#Locate the parameter file locationconfig = os.path.join( get_package_share_directory('topic_pkg'), 'config', 'turtle_config.yaml' )#Load the parameters fileparameters=[config]Let's look at the parameter file turtle_config.yaml,
xxxxxxxxxxsim ros__parameters background_r0 background_g0 background_b7The parameter file location is: ~/ros2_ws/src/topic_pkg/config
- sim: indicates the node name
- ros__parameters: Indicates the ROS parameter, which is fixed here
- background_r the parameter name, followed by the value set by the parameter