Plugins

BACK: Table of contents

Gazebo plugins are C++ libraries that are loaded when gazebo is launched. It accesses Gazebo’s API and can move objects, add/remove, and access sensor data.

I”m using Gazebo v7 for the projects I’m working on, and I didn’t need to but, install

$ sudo apt install libgazebo7-dev

Writing a Plugin

There are six different types of plugins.

  1. World: GZ_REGISTER_WORLD_PLUGIN
  2. Model: GZ_REGISTER_MODEL_PLUGIN
  3. Sensor: GZ_REGISTER_SENSOR_PLUGIN
  4. System: GZ_REGISTER_SYSTEM_PLUGIN
  5. Visual: GZ_REGISTER_VISUAL_PLUGIN
  6. GUI: GZ_REGISTER_GUI_PLUGIN

Use the Model plugins to control joints/states of a model, etc. Create a directory for your plugin $ mkdir gazebo_plugin_tutorial

#include <gazebo/gazebo.hh> // these are the core set of gazebo functions. does NOT include gazebo/physics.hh, /sensors.hh, etc.

//all plugins need to be in the gazebo namespace
namespace gazebo  
{
  //you need to inherit from the plugin type. In this case, the WorldPlugin (see above).
  class WorldPluginTutorial : public WorldPlugin
  {
    public: WorldPluginTutorial() : WorldPlugin()
            {
              printf("Hello World!\n");
            }

    //this is the other mandatory function, which recieves SDF element that contains the elements and attributes specified in loaded SDF file.
    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
            {
            }
  };
  //You need to register the plugin to the simulator. see above on which one to use
  GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial)
}

Compiling the Plugin

Use CMake, your favorite tool (not my favorite tool)

Create a CMakeLists.txt $ gedit ~/gazebo_plugin_tutorial/CMakeLists.txt and paste the following in

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

find_package(gazebo REQUIRED)
include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
list(APPEND CMAKE_CXX_FLAGS "${GAZEBO_CXX_FLAGS}")

add_library(hello_world SHARED hello_world.cc)
target_link_libraries(hello_world ${GAZEBO_LIBRARIES})

In terminal:

$ mkdir build
$ cd build
$ cmake ..
$ make

Now you need to tell Gazebo to look for this plugin (similar to ROS’s devel/setup.bash). In the terminal: $ export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build

Note that you would have to type this out everytime unless you put it in your ~/.bashrc.

Running the Plugin

Now that you have created the plugin, you want to attach it to either a sdf model or a world file. Let’s create a .world file.

Note: This is confusing because our plugin is called hello_world, but Gazebo has .world files. Pretend that hello_world is hello_everyone or something else. Or that hello.world is husky_playpen.world

Create a world file: $ gedit ~/gazebo_plugin_tutorial/hello.world and add:

<?xml version="1.0"?>
<sdf version="1.4">
  <world name="default">
    <plugin name="hello_world" filename="libhello_world.so"/>
  </world>
</sdf>

You can run your plugin: $ gzserver ~/gazebo_plugin_tutorial/hello.world --verbose

There are more types of plugins! The list above are links.