ROS2开发笔记——Launch系统(六)
在前面启动某个节点时,往往都是使用ros2 run命令,这样美启动一个节点都需要打开一个新的终端,显然不科学。那么,我们是如何管理我们的节点呢?答案就是用Launch系统。使用Launch系统可以同时启动多个节点,还可以配置节点的参数(parameter),注册事件处理函数等。
一、Launch系统的使用
Launch系统使用launch文件描述如何启动系统。在包目录下新建一个launch目录并新建turtlesim_mimic_launch.py文件,
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='84')
)
background_b_launch_arg = DeclareLaunchArgument(
'background_b', default_value=TextSubstitution(text='122')
)
return LaunchDescription([
background_r_launch_arg,
background_g_launch_arg,
background_b_launch_arg,
Node(
package='turtlesim',
namespace='turtlesim1',
executable='turtlesim_node',
name='sim'
),
Node(
package='turtlesim',
namespace='turtlesim2',
executable='turtlesim_node',
name='sim',
parameters=[{
'background_r': LaunchConfiguration('background_r'),
'background_g': LaunchConfiguration('background_g'),
'background_b': LaunchConfiguration('background_b'),
}]
),
Node(
package='turtlesim',
executable='mimic',
name='mimic',
remappings=[
('/input/pose', '/turtlesim1/turtle1/pose'),
('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
]
)
])
首先导入相应的python模块,
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():
return LaunchDescription([
])
加载第一个节点,从turtlesim包执行turtlesim_node程序,并指定命名空间和节点名称。命名空间的存在,使得我们可以在不同命名空间下运行同名的节点。
Node(
package='turtlesim',
namespace='turtlesim1',
executable='turtlesim_node',
name='sim'
),
加载第二个节点,不过我们重新指定了窗口的背景颜色,并作为参数传递给节点,
background_r_launch_arg = DeclareLaunchArgument(
'background_r', default_value=TextSubstitution(text='0')
)
background_g_launch_arg = DeclareLaunchArgument(
'background_g', default_value=TextSubstitution(text='84')
)
background_b_launch_arg = DeclareLaunchArgument(
'background_b', default_value=TextSubstitution(text='122')
)
...
Node(
package='turtlesim',
namespace='turtlesim2',
executable='turtlesim_node',
name='sim',
parameters=[{
'background_r': LaunchConfiguration('background_r'),
'background_g': LaunchConfiguration('background_g'),
'background_b': LaunchConfiguration('background_b'),
}]
),
第三个节点是想让第二个节点启动的小海龟模仿第一个节点启动的小海龟的行为,
Node(
package='turtlesim',
executable='mimic',
name='mimic',
remappings=[
('/input/pose', '/turtlesim1/turtle1/pose'),
('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
]
)
通过launch文件启动系统,
$ ros2 launch <package_name> <launch_file_name>
编译之前需要在package.xml中加入以下依赖
<exec_depend>ros2launch</exec_depend>
执行之后,应该就会出现两个窗口,
通过以下命令操纵第一个小海龟,第二个也跟着动,
ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"
查看节点,
$ rqt_graph
Launch系统还有很多高阶用法,请参考官方教程!