ROS2 Launch文件
在ROS1中launch文件是一种格式以.launch结尾的xml文档;而在ROS2中推荐使用Python方式编写launch文件,此时的launch文件是一种格式以.launch.py结尾的Python脚本。
LaunchDescription
对于一个基础的启动节点的launch文件,需要引用以下库,然后创建一个名为做generate_launch_description的函数:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
创建LaunchDescription的对象ld(名字任意)
# Create the launch description and populate
ld = LaunchDescription()
# Declare the launch options
ld.add_action(declare_urdf_model_path_cmd)
package
获取package路径
# Set the path to this package.
pkg_share = FindPackageShare(package='roarm').find('roarm')
path
路径
# Set the path to the URDF file
default_urdf_model_path = os.path.join(pkg_share, 'urdf/roarm.urdf')
configuration
# Launch configuration variables specific to simulation
gui = LaunchConfiguration('gui')
parameter_file = LaunchConfiguration('params_file')
是用于获取参数配置的方式之一。ROS 2使用Launch系统来管理节点和参数的启动。
在这行代码中,LaunchConfiguration是一个类,它接受一个字符串参数作为参数名称。该参数名称用于表示要从启动系统中获取的参数值。通常情况下,这些参数值存储在.launch.py文件中或通过其他方法进行配置,并在启动时传递给ROS2节点。
通过将参数名称 ‘params_file’ 传递给 LaunchConfiguration 构造函数,创建了一个LaunchConfiguration对象,它可以在后续代码中被引用。LaunchConfiguration对象可以用于检索对应参数配置的值,并将其赋给变量 parameter_file。这样,可以方便地在ROS 2的节点启动过程中访问和使用参数配置的值。
arguments
# Declare the launch arguments
declare_urdf_model_path_cmd = DeclareLaunchArgument(
name='urdf_model',
default_value=default_urdf_model_path,
description='Absolute path to robot urdf file')
arguments:arguments仅在launch文件内部有意义,相当与局部变量;
parameters:parameters是ROS系统使用的数值,存在parameter server上,nodes可通过ros::param::get函数编程得到,用户可通过rosparam获取;
DeclareLaunchArgument(,):这个函数的作用是声明一个变量并设定它的默认值、描述等,这样后续如果include了当前的Launch文件就可以调用该变量,或者也可以在命令行中用ros2 launch .来设定变量的值。
params_declare = DeclareLaunchArgument(
name = 'params_file',
default_value = os.path.join(share_dir, 'params', 'XXXX.yaml'),
description = 'FPath to the ROS2 parameters file to use.')
是在ROS 2中使用Launch系统声明一个启动参数的方式。DeclareLaunchArgument是一个函数或类,它接受多个参数来定义一个启动参数。下面是参数的解释:
name = ‘params_file’:参数名称,用于标识要声明的启动参数。
default_value=os.path.join(share_dir, ‘params’, ‘XXXX.yaml’):默认值参数,指定了该参数的默认值。在本例中,它使用os.path.join函数拼接了一个路径,表示默认的参数文件路径。share_dir是用于存储共享数据的目录,'params’是文件夹名称,'XXXX.yaml’是具体的参数文件名。
description=‘FPath to the ROS2 parameters file to use.’:描述参数的字符串,用于对该参数进行说明和文档化。
通过使用DeclareLaunchArgument,我们可以在ROS2 Launch系统中声明一个名为’params_file’的启动参数,并指定其默认值和描述。之后,可以在Launch文件中引用该参数,并将其传递给节点或其他组件,以便在运行时使用特定的参数配置。
node
# Publish the joint state values for the non-fixed joints in the URDF file.
start_joint_state_publisher_cmd = Node(
condition=UnlessCondition(gui),
package='joint_state_publisher',
executable='joint_state_publisher',
name='joint_state_publisher')
node 说明
example-node = Node(
package='package-name', #节点所在的功能包
namespace='package-namespace', #命名空间。如果存在同名节点,这一选项会有用
executable='execute-name/script-name.py', #表示要运行的可执行文件名或脚本名字.py
parameters=[{'parameter-name': parameter-value}], #参数
arguments=['-xxx', xxx, '-xxx', xxx ], #启动参数
output='screen', #用于将话题信息打印到屏幕
name='node-name' #表示启动后的节点名,可以没有
remappings=[ #重映射
('/xxx/xxx-new', '/xxx/xxx-old'),
]
)