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
    ])
相关推荐
Suckerbin1 天前
DarkHole: 2靶场渗透
笔记·安全·web安全·网络安全
一枝小雨1 天前
【C++】Vector完全指南:动态数组高效使用
开发语言·c++·笔记·vector·学习笔记·std库
大白的编程日记.1 天前
【Linux学习笔记】信号的深入理解之软件条件产生信号
linux·笔记·学习
Wilber的技术分享1 天前
【大模型实战笔记 1】Prompt-Tuning方法
人工智能·笔记·机器学习·大模型·llm·prompt
来生硬件工程师1 天前
【硬件笔记】负载是如何烧MOS的?
笔记·嵌入式硬件·硬件架构·硬件工程·硬件设计
lingggggaaaa1 天前
小迪安全v2023学习笔记(七十八讲)—— 数据库安全&Redis&CouchDB&H2database&未授权&CVE
redis·笔记·学习·算法·安全·网络安全·couchdb
Source.Liu1 天前
【Python基础】 13 Rust 与 Python 注释对比笔记
开发语言·笔记·python·rust
诗句藏于尽头1 天前
DJANGO后端服务启动报错及解决
数据库·笔记·django
汇能感知1 天前
光谱相机的探测器类型
经验分享·笔记·科技
g_i_a_o_giao1 天前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析