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
    ])
相关推荐
future14121 小时前
每日问题总结
经验分享·笔记
超喜欢下雨天1 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
循环过三天3 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
之歆3 小时前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
DKPT5 小时前
Java组合模式实现方式与测试方法
java·笔记·学习·设计模式·组合模式
受之以蒙5 小时前
Rust & WASM 之 wasm-bindgen 基础:让 Rust 与 JavaScript 无缝对话
前端·笔记·rust
茫忙然6 小时前
【WEB】Polar靶场 6-10题 详细笔记
笔记
eric*16886 小时前
尚硅谷张天禹老师课程配套笔记
前端·vue.js·笔记·vue·尚硅谷·张天禹·尚硅谷张天禹
Allen_LVyingbo7 小时前
数智读书笔记系列035《未来医疗:医疗4.0引领第四次医疗产业变革》
人工智能·经验分享·笔记·健康医疗
岑梓铭7 小时前
考研408《计算机组成原理》复习笔记,第三章(3)——多模块存储器
笔记·考研·408·计算机组成原理