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:
xxxxxxxxxx
def generate_launch_description():
return LaunchDescription([
action_1,
action_2,
...
action_n
])
We create a new folder under the path of the function package created before to store the launch file, terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
mkdir launch
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.
xxxxxxxxxx
#1、导入相关的头文件
#1, import the relevant header file
import os
from glob import glob
#2、在data_files的列表中,加上launch路径以及路径下的launch.py文件
#2. In the data_files list, add the launch path and the launch.py file under the path
(os.path.join('share',package_name,'launch'),glob(os.path.join('launch','*launch.py')))
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit single_node_launch.py
Copy the following into the file,
xxxxxxxxxx
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
turtle_node = Node(
package='turtlesim',
executable='turtlesim_node',
)
launch_description = LaunchDescription([turtle_node])
return launch_description
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Terminal input,
xxxxxxxxxx
ros2 launch topic_pkg single_node_launch.py
After the program runs, it will run on the node of the turtle.
xxxxxxxxxx
from launch import LaunchDescription
from launch_ros.actions import Node
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
xxxxxxxxxx
def generate_launch_description():
turtle_node = Node(
package='turtlesim',
executable='turtlesim_node',
)
launch_description = LaunchDescription([turtle_node])
return 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.
xxxxxxxxxx
launch_description = LaunchDescription([turtle_node])
Finally, return launch_description.
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit multi_node_launch.py
Copy the following into the file,
xxxxxxxxxx
from launch import LaunchDescription
from 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_description
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Terminal input,
xxxxxxxxxx
ros2 launch topic_pkg multi_node_launch.py
The terminal does not print content, we can check which nodes start to verify whether there is a successful start, terminal input,
xxxxxxxxxx
ros2 node list
As can be seen from the figure above, two nodes are started, corresponding to the two programs in the launch file.
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]).
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit remap_name_launch.py
Copy the following into the file,
xxxxxxxxxx
from launch import LaunchDescription
from 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_description
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Let's see what the speed of the baby turtle is before we remap the topic what the topic name is, terminal input,
xxxxxxxxxx
ros2 launch topic_pkg single_node_launch.py
ros2 topic list
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,
xxxxxxxxxx
ros2 launch topic_pkg remap_name_launch.py
ros2 topic list
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.
The original single_node_launch.py has been modified, mainly adding the following parts:
xxxxxxxxxx
remappings=[("/turtle1/cmd_vel", "/cmd_vel")]
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.
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit include_launch.py
Copy the following into the file,
xxxxxxxxxx
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
def 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])
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Terminal input,
xxxxxxxxxx
ros2 launch topic_pkg include_launch.py
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,
xxxxxxxxxx
ros2 node list
Three nodes were indeed started, so it was successful.
xxxxxxxxxx
#导入必要的库文件
# Import the necessary library files
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
node1 = IncludeLaunchDescription(
PythonLaunchDescriptionSource([os.path.join(
get_package_share_directory('topic_pkg'), 'launch'),
'/multi_node_launch.py'])
)
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit param_launch.py
Copy the following into the file,
xxxxxxxxxx
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration, TextSubstitution
from 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'),
}]
)
])
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Terminal input,
xxxxxxxxxx
ros2 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 board.
xxxxxxxxxx
from launch.actions import DeclareLaunchArgument # 声明launch文件内使用的Argument类
# Declares the Argument class used in the launch file
background_r_launch_arg = DeclareLaunchArgument(
'background_r', default_value=TextSubstitution(text='0')) # 创建一个Launch文件内参数background_r
# Creates a Launch file with the background_r parameter
'background_r', default_value=TextSubstitution(text='0')) # 创建一个Launch文件内参数background_g
# Creates a Launch file with the background_g parameter
'background_r', default_value=TextSubstitution(text='0')) # 创建一个Launch文件内参数background_b
# Creates a Launch file with the background_b argument
background_r_launch_arg, # 调用以上创建的参数
# calls the parameters created above
background_g_launch_arg,
background_b_launch_arg,
parameters=[{ # ROS参数列表# ROS parameter list
'background_r': LaunchConfiguration('background_r'), # 创建参数background_r
# Create parameter background_r
'background_g': LaunchConfiguration('background_g'), # 创建参数background_g
# Create parameter background_g
'background_b': LaunchConfiguration('background_b'), # 创建参数background_b
# Create parameter background_b
argument and param are both parameters, but argument is passed in the launch file, and param is passed in the node program.
First create a parameter table, terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
mkdir config
cd config
gedit turtle_config.yaml
Copy the contents into the turtle_config.yaml file,
xxxxxxxxxx
sim
ros__parameters
background_r0
background_g0
background_b7
Save and exit, then modify the setup.py file, the path to load the parameter file, terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg
gedit setup.py
In the position shown in the above picture, add the following,
xxxxxxxxxx
(os.path.join('share', package_name, 'config'), glob(os.path.join('config', '*.yaml'))),
Save and exit, finally write the launch file, terminal input,
xxxxxxxxxx
cd ~/ros2_ws/src/topic_pkg/launch
gedit param_config_launch.py
Copy the following into the file,
xxxxxxxxxx
import os
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
def 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]
)
])
Terminal input,
xxxxxxxxxx
cd ~/ros2_ws
colcon build
After compiling, refresh the environment variables in the workspace,
xxxxxxxxxx
source ~/ros2_ws/install/setup.bash
Terminal input,
xxxxxxxxxx
ros2 launch topic_pkg param_config_launch.py
Run the program to get the turtle and the background color is set to black by parameter.
xxxxxxxxxx
#找到参数文件位置
# Find the parameter file location
config = os.path.join(
get_package_share_directory('topic_pkg'),
'config',
'turtle_config.yaml'
)
#加载参数文件
# Load parameter file
parameters=[config]
Let's look at the parameter file turtle_config.yaml,
xxxxxxxxxx
sim
ros__parameters
background_r0
background_g0
background_b7
The location of the parameter file is ~/ros2_ws/src/topic_pkg/config