Launch
ros2 中,启动脚本是基于python文件维护的。
1.简单的launch文件
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
#必须以这个函数名开始
def generate_launch_description():
pkg_share = FindPackageShare("svc_demo").find('svc_demo')
urdf_file = os.path.join(pkg_share,'launch','single_robot.urdf')
with open(urdf_file,'r') as infp:
robot_desc = infp.read()
rsp_params = {'robot_description': obot_desc}
argument_node_para_declare = DeclareLaunchArgument('node_count',default_value='1')
svc_demo = Node(package='svc_demo', #功能包名称
executable='svc_demo',#可执行文件名
name='new_node_name',# 重命名进程内所有的节点名 ,这个一般不设置
namespace='new_namespace',# 给进程内所有内容添加命名空间名
exec_name='new_run_name',# 给运行文件修改名称
output='screen',
arguments=[rsp_params,LaunchConfiguration('node_count')]# 传入参数
)
ld = LaunchDescription()
ld.addaction(svc_demo)
ld.addaction(argument_node_para_declare)
return ld
2.如何构建启动系统
启动脚本有两种策略:在某个功能包内启动改功能包功能,建立专门用于启动的功能包用于启动一个或者多个功能包
步骤:
(1) 在功能包中建立launch文件夹
(2) 添加指定的依赖项,确保构建顺序在其调用功能包之后
(3) 在launch文件夹中添加启动脚本的python文件,一般命名格式xxxx.launch.py
(4) 在脚本中写入启动脚本内容
(5) 在CMakeLists.txt 或者 setup.py 中添加install指令,指定安装”launch“目录
(6) 使用colcon 构建与部署
2.1 添加到CMake
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME})
2.2 添加到setup.py
setup(
data_files=[
#....
(os.path.join('share',package_name), glob('launch/*launch.py'))
])