Launch文件笔记: 二、加载GAZEBO文件

1、创建文件.launch.py

加载URDF文件,发布robot_description

https://blog.csdn.net/laocui1/article/details/148080739

python 复制代码
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument
import os # 导包
from ament_index_python.packages import get_package_share_directory # 获取包的共享目录
from launch.substitutions import Command, LaunchConfiguration
from launch_ros.parameter_descriptions import ParameterValue
 
 
def generate_launch_description():
    arduinobot_description_dir = get_package_share_directory("arduinobot_description")
 
    # 定义一个启动参数 model
    # arduinobot_description_dir 是包的共享目录
    model_arg = DeclareLaunchArgument(name="model", # 参数名称
        default_value=os.path.join(arduinobot_description_dir, "urdf", "arduinobot.urdf.xacro"),# 参数默认值
        description="Absolute path to robot urdf file" # 参数描述:这是URDF文件的绝对路径
        )
    
    # 通过xacro命令将xacro文件转换为urdf文件
    # 这里使用了ParameterValue来将xacro文件的内容传递给robot_description参数
    robot_description = ParameterValue(Command(["xacro ", LaunchConfiguration("model")]),
                                       value_type=str)
 
    robot_state_publisher = Node(
        package="robot_state_publisher",
        executable="robot_state_publisher",
        parameters=[{"robot_description": robot_description}]
    )
 
 
    return LaunchDescription([
        model_arg,
        robot_state_publisher_node,
 
    ])

2. 设置环境变量: GZ_SIM_RESOURCE_PATH

python 复制代码
from launch.actions import SetEnvironmentVariable
from ament_index_python.packages import get_package_prefix

   env_var = SetEnvironmentVariable("GAZEBO_MODLEL_PATH",os.path.join(get_package_prefix("arduinobot_description"), "share"))
    

3. 从一个launch中导入另一launch

分别嵌套创立两个gazebo launch服务

python 复制代码
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource

    # 嵌套2个launch
    start_gazebo_server = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(os.path.join(get_package_share_directory("gazebo_ros"), "launch", "gzserver.launch.py")),
        )

    start_gazebo_client = IncludeLaunchDescription(
 PythonLaunchDescriptionSource(os.path.join(get_package_share_directory("gazebo_ros"), "launch", "gzclient.launch.py")),
        )

4. 在Gazebo中生成机器人

python 复制代码
    spawn_robot = Node(
        package="gazebo_ros",
        executable="spawn_entity.py",
        arguments=[".entity", "arduinobot", "-topic", "robot_description"]
    )

5. 完整代码

python 复制代码
import os
from pathlib import Path
from ament_index_python.packages import get_package_share_directory, get_package_prefix

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription, SetEnvironmentVariable
from launch.substitutions import Command, LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource

from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue


def generate_launch_description():
    arduinobot_description_dir = get_package_share_directory("arduinobot_description")
 
    # 定义一个启动参数 model
    # arduinobot_description_dir 是包的共享目录
    model_arg = DeclareLaunchArgument(name="model", # 参数名称
        default_value=os.path.join(arduinobot_description_dir, "urdf", "arduinobot.urdf.xacro"),# 参数默认值
        description="Absolute path to robot urdf file" # 参数描述:这是URDF文件的绝对路径
        )
    
    robot_description = ParameterValue(Command(["xacro ", LaunchConfiguration("model")]),
                                       value_type=str)
 
    robot_state_publisher_node = Node(
        package="robot_state_publisher",
        executable="robot_state_publisher",
        parameters=[{"robot_description": robot_description}]
    )
    env_var = SetEnvironmentVariable("GAZEBO_MODLEL_PATH",os.path.join(get_package_prefix("arduinobot_description"), "share"))
  
    # 嵌套2个launch
    start_gazebo_server = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(os.path.join(get_package_share_directory("gazebo_ros"), "launch", "gzserver.launch.py")),
        )

    start_gazebo_client = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(os.path.join(get_package_share_directory("gazebo_ros"), "launch", "gzclient.launch.py")),
        )
    spawn_robot = Node(
        package="gazebo_ros",
        executable="spawn_entity.py",
        arguments=[".entity", "arduinobot", "-topic", "robot_description"]
    )

    return LaunchDescription([
        model_arg,
        env_var,
        robot_state_publisher_node,
        start_gazebo_server,
        start_gazebo_client,
        spawn_robot
    ])
相关推荐
li星野1 小时前
打工人日报#20251005
笔记·程序人生·fpga开发·学习方法
JJJJ_iii2 小时前
【深度学习01】快速上手 PyTorch:环境 + IDE+Dataset
pytorch·笔记·python·深度学习·学习·jupyter
悠哉悠哉愿意3 小时前
【ROS2学习笔记】RViz 三维可视化
笔记·学习·机器人·ros2
序属秋秋秋5 小时前
《C++进阶之C++11》【智能指针】(下)
c++·笔记·学习·面试·c++11·智能指针·新特性
丰锋ff6 小时前
2024 年真题配套词汇单词笔记(考研真相)
笔记·考研
序属秋秋秋6 小时前
《C++进阶之C++11》【智能指针】(上)
c++·笔记·学习·面试·c++11·智能指针·新特性
十安_数学好题速析6 小时前
根式方程:结构联想巧用三角代换
笔记·学习·高考
弘毅 失败的 mian7 小时前
利用 VsCode + EIDE 进行嵌入式开发(保姆级教程)
经验分享·笔记·vscode·stm32·单片机·嵌入式硬件·eide
li星野7 小时前
打工人日报#20251002
笔记·程序人生·学习方法
能不能别报错8 小时前
K8s学习笔记(十三) StatefulSet
笔记·学习·kubernetes