本文在Ubuntu18.04 + ROS melodic环境下完成,其他ROS版本类似。

在本教程中,我们将逐步创建一个支持ROS的非常基本的gazebo插件。

一、创建一个ROS包

$ cd ~/catkin_ws/src
$ catkin_create_pkg gazebo_tutorials gazebo_ros roscpp

二、创建插件

按照此处所述创建一个非常简单的插件,并将文件另存为gazebo_tutorials/src/ simple_world_plugin.cpp:

#include <gazebo/common/Plugin.hh>
#include <ros/ros.h>

namespace gazebo
{
class WorldPluginTutorial : public WorldPlugin
{
public:
  WorldPluginTutorial() : WorldPlugin()
  {
  }

  void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
  {
    // Make sure the ROS node for Gazebo has already been initialized
    if (!ros::isInitialized())
    {
      ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
        << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
      return;
    }

    ROS_INFO("Hello World!");
  }

};
GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial)
}

三、配置CMakeLists.txt

打开gazebo_tutorials/CMakeLists.txt并将其替换为以下内容:

cmake_minimum_required(VERSION 2.8.3)
project(gazebo_tutorials)

# Check for c++11 / c++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

# Load catkin and all dependencies required for this package
find_package(catkin REQUIRED COMPONENTS
  roscpp
  gazebo_ros
)

# Depend on system install of Gazebo
find_package(gazebo REQUIRED)

link_directories(${GAZEBO_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIR} ${catkin_INCLUDE_DIRS} ${GAZEBO_INCLUDE_DIRS})

catkin_package(
  DEPENDS
    roscpp
    gazebo_ros
)

add_library(${PROJECT_NAME} src/simple_world_plugin.cpp)
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${GAZEBO_LIBRARIES})

四、配置package.xml

在<export> </ export>标记内添加以下代码:

<gazebo_ros plugin_path="${prefix}/../../lib" gazebo_media_path="${prefix}" />

五、编译插件

转到工作空间的根目录并运行catkin来构建插件:

$ cd ~/catkin_ws
$ catkin_make

六、创建一个world文件

将以下文件另存为gazebo_tutorials/worlds/hello.world:

<?xml version="1.0" ?>
<sdf version="1.4">
  <world name="default">
    <include>
      <uri>model://ground_plane</uri>
    </include>

    <include>
      <uri>model://sun</uri>
    </include>

    <!-- reference to your plugin -->
    <plugin name="gazebo_tutorials" filename="libgazebo_tutorials.so"/>
  </world>
</sdf>

七、创建launch文件

创建启动文件gazebo_tutorials/launch/hello.launch:

<launch>
  <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find gazebo_tutorials)/worlds/hello.world"/>
    <!-- more default parameters can be changed here -->
  </include>
</launch>

在继续之前,获取新的setup.*sh文件,请执行以下操作:

$ cd ~/catkin_ws
$ source devel/setup.bash

八、运行插件

$ roslaunch gazebo_tutorials hello.launch

一个空的gazebo打开,并且在终端中您应该看到它打印出下方类似的内容:

【INFO】: Hello World!