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
    ])
相关推荐
摇滚侠17 小时前
Vue 项目实战《尚医通》,预约挂号就诊人组件搭建上,笔记40
前端·javascript·vue.js·笔记
心无旁骛~1 天前
Masquerade 总结笔记:解锁野外人类视频的机器人政策学习潜力
笔记·机器人
谅望者1 天前
数据分析笔记14:Python文件操作
大数据·数据库·笔记·python·数据挖掘·数据分析
未若君雅裁1 天前
LeetCode 51 - N皇后问题 详解笔记
java·数据结构·笔记·算法·leetcode·剪枝
循环过三天1 天前
7.7、Python-常用内置函数
笔记·python·学习
AA陈超1 天前
ASC学习笔记0007:用于与GameplayAbilities系统交互的核心ActorComponent
c++·笔记·学习·ue5·虚幻引擎
智者知已应修善业1 天前
【51单片机:两边向中间流水:即两边先点亮然后熄灭,次边的点亮再熄灭,直到最中间的两个点亮再熄灭,然后重复动作。】2023-3-4
c语言·c++·经验分享·笔记·嵌入式硬件·算法·51单片机
2301_821727171 天前
nfs服务
网络·笔记
报错小能手1 天前
C++笔记 bind函数模板
开发语言·c++·笔记
大筒木老辈子1 天前
Git笔记---其他常用操作
笔记·git