2025年7月1日 星期二

ROS2 (ros1 launch upgrade to ROS2 launch py )



您好!這是一個 ROS 1 語法的 launch 檔案。您之前遇到的錯誤是 ROS 2 環境下的,因此這個檔案的語法會導致 ROS 2 無法解析。ROS 2 的 launch 檔案是基於 Python 的。

重要的提示: 您提供的這個 XML 格式的 launch 檔案是 ROS 1 的語法。ROS 2 不支持這種 XML 格式的 launch 檔案,而是使用 Python 檔案(.launch.py)來定義啟動行為。

如果您的目標是在 ROS 2 環境中運行,您需要將這個 XML 檔案轉換成 Python 的 .launch.py 檔案。


ROS 1 XML launch 檔案的分析

即使這是 ROS 1 檔案,我們也可以看到您想要實現的邏輯:

  • 定義參數 (args)

    • model:指向機器人模型的 XACRO 檔案。

    • gui:控制是否使用 GUI 版本。

    • rvizconfig:指向 RViz 的配置檔。

  • 設置參數 (param)

    • robot_description:使用 xacro 命令處理 model 參數,將結果作為機器人描述。

    • use_gui:設置為 gui 參數的值。

  • 啟動節點 (node)

    • joint_state_publisher_gui:用於手動控制關節(如果 use_gui 為 true)。

    • robot_state_publisher:發布機器人的 TF 樹。

    • rviz:啟動 RViz,載入指定的配置檔。


將 ROS 1 launch 檔案轉換為 ROS 2 Python launch.py

現在,我們將這個邏輯轉換為 ROS 2 的 Python launch.py 語法,並將套件名稱 ROS2_car_description 替換為一個變數。假設您的 ROS 2 套件名稱是 roscar4wheel3

請將以下內容保存為 display.launch.py 在您的 roscar4wheel3 套件的 launch 資料夾中(例如:~/data/ros2_ws3/src/roscar4wheel3/launch/display.launch.py)。

============================

import os

from ament_index_python.packages import get_package_share_directory

from launch import LaunchDescription

from launch.actions import DeclareLaunchArgument

from launch.substitutions import LaunchConfiguration, Command, FindExecutable, PathJoinSubstitution

from launch_ros.actions import Node

from launch_ros.substitutions import FindPackageShare # 引入 FindPackageShare 替代 $(find)


def generate_launch_description():

    # 定義套件名稱變數

    # 這裡我們直接使用字符串,因為在 Python launch 文件中,我們通常直接知道自己的套件名稱

    # 或者如果需要從外部傳入,可以用 DeclareLaunchArgument

    pkg_name = 'roscar4wheel3' # <--- 將您的實際套件名稱填寫在這裡


    # 獲取套件的分享目錄

    pkg_share_dir = get_package_share_directory(pkg_name)


    # 宣告 Launch Arguments (對應 ROS 1 的 <arg name="..." default="..."/>)

    model_arg = DeclareLaunchArgument(

        name='model',

        default_value=PathJoinSubstitution([

            pkg_share_dir,

            'urdf',

            'ROS2_car.xacro'

        ]),

        description='Path to the robot URDF/XACRO file'

    )


    gui_arg = DeclareLaunchArgument(

        name='gui',

        default_value='true',

        description='Set to true to launch the joint state publisher gui'

    )


    rviz_config_arg = DeclareLaunchArgument(

        name='rvizconfig',

        default_value=PathJoinSubstitution([

            pkg_share_dir,

            'rviz', # 假設 rviz config 放在 rviz 資料夾

            'urdf.rviz' # 您的 RViz config 檔案名稱

        ]),

        description='Path to the RViz config file'

    )


    # 獲取 XACRO 執行檔路徑 (用於 Command substitution)

    xacro_path = PathJoinSubstitution([FindExecutable(name='xacro')])


    # 設置 robot_description 參數 (對應 ROS 1 的 <param name="robot_description" .../>)

    robot_description_content = Command([

        xacro_path, ' ', LaunchConfiguration('model')

    ])


    robot_description_param = {'robot_description': robot_description_content}


    # 啟動 joint_state_publisher_gui 節點 (對應 ROS 1 的 <node name="joint_state_publisher_gui" .../>)

    # ROS 2 中通常用 joint_state_publisher 搭配參數控制是否使用 GUI

    joint_state_publisher_node = Node(

        package='joint_state_publisher_gui',

        executable='joint_state_publisher_gui',

        name='joint_state_publisher_gui',

        parameters=[{'use_gui': LaunchConfiguration('gui')}],

        output='screen'

    )


    # 啟動 robot_state_publisher 節點

    robot_state_publisher_node = Node(

        package='robot_state_publisher',

        executable='robot_state_publisher',

        name='robot_state_publisher',

        output='screen',

        parameters=[robot_description_param] # 將 robot_description 參數傳給它

    )


    # 啟動 RViz2 節點

    rviz_node = Node(

        package='rviz2', # ROS 2 中 rviz 的 package 名稱是 rviz2

        executable='rviz2', # ROS 2 中 rviz 的執行檔名稱是 rviz2

        name='rviz2',

        arguments=['-d', LaunchConfiguration('rvizconfig')],

        output='screen',

        # required=True 可以在 ROS 2 launch 中實現,但這裡通常不這麼寫,

        # 除非您需要 launch 服務在 rviz2 退出時也結束。

    )


    return LaunchDescription([

        model_arg,

        gui_arg,

        rviz_config_arg,

        joint_state_publisher_node,

        robot_state_publisher_node,

        rviz_node

    ])


=============================

使用這個 display.launch.py 檔案:

  1. 將上面的 Python 程式碼保存為 display.launch.py,放在您的 ROS 2 套件 roscar4wheel3launch 資料夾中。

  2. 確保您的 ROS2_car.xacro 已經更新,將所有 ROS2_car_description 替換為 roscar4wheel3,就像我們之前討論的那樣。

  3. 確保您的 rviz 配置檔案 urdf.rviz 存在roscar4wheel3/rviz/ 資料夾中 (我將其路徑從 launch 改為 rviz,因為這是慣例,請根據您實際的檔案結構調整)。

  4. 在您的 ROS 2 工作空間 (例如 ~/data/ros2_ws3) 中運行 colcon build

    Bash
    cd ~/data/ros2_ws3
    colcon build --packages-select roscar4wheel3
    
  5. source 您的工作空間環境:

    Bash
    source install/setup.bash # 或 setup.zsh
    
  6. 最後,運行您的 ROS 2 launch 檔案:

    Bash
    ros2 launch roscar4wheel3 display.launch.py
    

這樣,roscar4wheel3 這個變數就透過 pkg_name 在 Python 程式碼中定義,並且所有的路徑都動態地從這個變數和 get_package_share_directory 中獲取。







沒有留言:

張貼留言