Creating a basic world plugin

We will look at a basic Gazebo world plugin and try to build and load it in Gazebo.

Create a folder called gazebo_basic_world_plugin in the user home folder and create a CPP file called hello_world.cc:

    $ mkdir ~/gazebo_basic_world_plugin
    $ cd ~/gazebo_basic_world_plugin
    $ nano hello_world.cc

The definition of hello_world.cc is as follows:

//Gazebo header for getting core gazebo functions 
#include <gazebo/gazebo.hh> 
 
//All gazebo plugins should have gazebo namespace 
 
namespace gazebo 
{ 
 
  //The custom WorldpluginTutorials is inheriting from standard worldPlugin. Each world plugin has to inheriting from standard plugin type.  
 
  class WorldPluginTutorial : public WorldPlugin 
  { 
 
    public: WorldPluginTutorial() : WorldPlugin() 
            { 
              printf("Hello World!
"); 
            } 
 
 
 //The Load function can receive the SDF elements  
    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf) 
            { 
            } 
  }; 
 
//Registering World Plugin with Simulator  
  GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) 
} 

The header file used in this code is <gazebo/gazebo.hh>; the header contains core functionalities of Gazebo. Other headers are as follows:

  • gazebo/physics/physics.hh: This is the Gazebo header for accessing the physics engine parameters
  • gazebo/rendering/rendering.hh: This is the Gazebo header for handling rendering parameters
  • gazebo/sensors/sensors.hh: This is the header for handling sensors

At the end of the code, we have to export the plugin using the statements
mentioned below.

The GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) macro will register and export the plugin as a world plugin. The following macros are used to register for sensors, models, and so on:

  • GZ_REGISTER_MODEL_PLUGIN: This is the export macro for Gazebo robot model
  • GZ_REGISTER_SENSOR_PLUGIN: This is the export macro for Gazebo sensor model
  • GZ_REGISTER_SYSTEM_PLUGIN: This is the export macro for Gazebo system
  • GZ_REGISTER_VISUAL_PLUGIN: This is the export macro for Gazebo visuals

After setting the code, we can make the CMakeLists.txt for compiling the source. The following is the source of CMakeLists.txt:

    $ nano ~/ gazebo_basic_world_plugin/CMakeLists.txt
    
    cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
    
    find_package(Boost REQUIRED COMPONENTS system)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    
    include (FindPkgConfig)
    if (PKG_CONFIG_FOUND)
      pkg_check_modules(GAZEBO gazebo)
    endif()
    include_directories(${GAZEBO_INCLUDE_DIRS})
    link_directories(${GAZEBO_LIBRARY_DIRS})
    
    add_library(hello_world SHARED hello_world.cc)
    target_link_libraries(hello_world ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})

Create a build folder for storing the shared object:

    $ mkdir ~/gazebo_basic_world_plugin/build
    $ cd ~/gazebo_basic_world_plugin/build

After switching to the build folder, execute the following command to compile and build the source code:

    $ cmake ../
    $ make
  

After building the code, we will get a shared object called libhello_world.so and we have to export the path of this shared object in GAZEBO_PLUGIN_PATH and add to the .bashrc file:

    export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_basic_world_plugin/build
  

After setting the Gazebo plugin path, we can use it inside the URDF file or the
SDF file. The following is a sample world file called hello.world, which includes this plugin:

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

Run the Gazebo server and load this world file:

    $ cd ~ /gazebo_basic_world_plugin
    $ gzserver hello.world --verbose
  
Figure 11: The Gazebo world plugin printing "Hello World"

We will source the code for various Gazebo plugins from the Gazebo repository.

We can check https://bitbucket.org/osrf/gazebo

Browse for the source code. Take the examples folder and then the plugins, as shown
in the following figure:

Figure 12: The list of Gazebo plugins in the repository
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.175