You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ROS1 Noetic的XML格式启动文件转换为ROS2兼容格式?

ROS1到ROS2启动文件转换方案

ROS2的启动机制和ROS1存在不少差异,比如rviz的包名改为rviz2,参数引用方式也有变化,下面给你两种可行的转换方案:

方案一:Python启动文件(推荐)

ROS2官方更推崇用Python编写启动文件,灵活性更高。在你的mobile_robot包下创建launch/display.launch.py文件,内容如下:

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command, PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare

def generate_launch_description():
    # 保留原有的model参数(原文件未用到,仅做兼容)
    declare_model_arg = DeclareLaunchArgument(
        name='model',
        default_value='',
        description='URDF模型路径参数'
    )

    # 获取mobile_robot包的安装路径
    mobile_robot_share = FindPackageShare(package='mobile_robot')

    # 拼接URDF文件路径
    urdf_path = PathJoinSubstitution(
        [mobile_robot_share, 'urdf', 'mobile_robot.urdf']
    )

    # 拼接rviz配置文件路径
    rviz_config_path = PathJoinSubstitution(
        [mobile_robot_share, 'urdf.rviz']
    )

    # 读取URDF文件内容,设置robot_description参数
    robot_description = ParameterValue(
        Command(['cat ', urdf_path]),  # 纯URDF用cat命令读取,xacro文件替换为xacro命令
        value_type=str
    )

    # 启动joint_state_publisher_gui节点
    joint_state_gui_node = Node(
        package='joint_state_publisher_gui',
        executable='joint_state_publisher_gui',
        name='joint_state_publisher_gui'
    )

    # 启动robot_state_publisher节点
    robot_state_pub_node = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        name='robot_state_publisher',
        parameters=[{'robot_description': robot_description}]
    )

    # 启动rviz2节点(ROS2中rviz包名改为rviz2)
    rviz_node = Node(
        package='rviz2',
        executable='rviz2',
        name='rviz',
        arguments=['-d', rviz_config_path]
    )

    return LaunchDescription([
        declare_model_arg,
        joint_state_gui_node,
        robot_state_pub_node,
        rviz_node
    ])

配套设置与启动

  • 确保包的setup.py中配置了launch文件的安装路径,在data_files里添加:
    data_files=[
        ('share/ament_index/resource_index/packages',
         ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
        (os.path.join('share', package_name, 'urdf'), glob(os.path.join('urdf', '*'))),
        (os.path.join('share', package_name), ['urdf.rviz']),
    ],
    
  • 编译包后,用命令启动:ros2 launch mobile_robot display.launch.py

方案二:ROS2 XML启动文件

如果更习惯XML格式,ROS2也支持,但语法与ROS1不同,创建launch/display.launch.xml文件:

<launch>
  <arg name="model" default="" description="URDF模型路径参数"/>

  <!-- 定义包路径变量,替代ROS1的$(find) -->
  <let name="mobile_robot_share" value="$(find-pkg-share mobile_robot)"/>
  <let name="urdf_path" value="$(join-path $(var mobile_robot_share) 'urdf/mobile_robot.urdf')"/>
  <let name="rviz_config_path" value="$(join-path $(var mobile_robot_share) 'urdf.rviz')"/>

  <!-- 设置robot_description参数,读取URDF文件内容 -->
  <param name="robot_description" value="$(command 'cat $(var urdf_path)')" />

  <!-- 启动joint_state_publisher_gui节点 -->
  <node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" exec="joint_state_publisher_gui" />

  <!-- 启动robot_state_publisher节点 -->
  <node name="robot_state_publisher" pkg="robot_state_publisher" exec="robot_state_publisher">
    <param name="robot_description" value="$(command 'cat $(var urdf_path)')" />
  </node>

  <!-- 启动rviz2节点 -->
  <node name="rviz" pkg="rviz2" exec="rviz2" args="-d $(var rviz_config_path)" />
</launch>

启动命令

编译后执行:ros2 launch mobile_robot display.launch.xml

内容的提问来源于stack exchange,提问作者Mubashir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 07:20:28