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
    ])
相关推荐
_Kayo_1 小时前
node.js 学习笔记3 HTTP
笔记·学习
星星火柴9365 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
Cx330❀7 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
小幽余生不加糖8 小时前
电路方案分析(二十二)适用于音频应用的25-50W反激电源方案
人工智能·笔记·学习·音视频
..过云雨8 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
岑梓铭9 小时前
考研408《计算机组成原理》复习笔记,第五章(3)——CPU的【数据通路】
笔记·考研·408·计算机组成原理·计组
Blossom.11815 小时前
把 AI 推理塞进「 8 位 MCU 」——0.5 KB RAM 跑通关键词唤醒的魔幻之旅
人工智能·笔记·单片机·嵌入式硬件·深度学习·机器学习·搜索引擎
草莓熊Lotso16 小时前
《吃透 C++ 类和对象(中):const 成员函数与取地址运算符重载解析》
c语言·开发语言·c++·笔记·其他
玖別ԅ(¯﹃¯ԅ)17 小时前
PID学习笔记6-倒立摆的实现
笔记·stm32·单片机
想学全栈的菜鸟阿董18 小时前
Django5个人笔记
笔记