1.导入必要模块

import launch
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directory
import os

import launch_ros.parameter_descriptions

2.定义功能包路径和文件路径

def generate_launch_description():
    #获取功能包的share路径
    urdf_package_path = get_package_share_directory('fishbot_description')
    default_xacro_path = os.path.join(urdf_package_path,'urdf', 'fishbot/fishbot.urdf.xacro')
    #default_rviz_config_path = os.path.join(urdf_package_path,'config', 'display_robot_model.rviz')
    default_gazebo_world_path = os.path.join(urdf_package_path,'world', 'custom_room.world')

3.声明可配置参数

 #声明一个urdf目录的参数,方便修改。
    action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(
        name='model',default_value=str(default_xacro_path),description='加载的模型文件路径'
    )

4

    #通过文件路径,获取内容,并转换成参数值对象,以供传入robot_state_publisher 
    substitutions_command_result = launch.substitutions.Command(['xacro ', launch.
    substitutions.LaunchConfiguration('model')])
    robot_description_value = launch_ros.parameter_descriptions.ParameterValue(substitutions_command_result, value_type=str)

5.启动gazebo服务器和客户端

    #启动gazebo
    action_launch_gazebo = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource(
            [get_package_share_directory('gazebo_ros'),'/launch','/gazebo.launch.py']
        ),
        launch_arguments=[('world', default_gazebo_world_path),('verbose','true')]
    )

6.发布机器人状态

    action_robot_state_publisher = launch_ros.actions.Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description':robot_description_value}]
    )

7.在gazebo中生成机器人

    #请求gazebo加载机器人
    action_spawn_entity = launch_ros.actions.Node(
        package='gazebo_ros',
        executable='spawn_entity.py',
        arguments=['-topic', '/robot_description', '-entity', 'fishbot']
    )

8. 整合所有组件

    return launch.LaunchDescription([
        action_declare_arg_mode_path,
        action_robot_state_publisher,
        action_launch_gazebo,
        action_spawn_entity,
        #action_joint_state_publisher,
        #action_rviz_node
    ])

 

 

 

 

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐