Hello World plugin

Plugins are designed to be simple. Here, we will discuss a bare bones World plugin which contains a class with a few member functions.

First, we will make a directory and a .cc file for the new plugin:

$ mkdir ~/gazebo_plugin_tutorial
$ cd ~/gazebo_plugin_tutorial
$ gedit hello_world.cc

You can also use the existing package from the chapter3_tutorials/ gazebo_plugin_tutorial file on GitHub.

Next, we will add the following code to hello_world.cc:

#include <gazebo/gazebo.hh> 
namespace gazebo 
{ 
  class WorldPluginTutorial : public WorldPlugin 
  { 
    public: WorldPluginTutorial() : WorldPlugin() 
            { 
              printf("Gazebo Says: Hello World!n"); 
            } 
    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf) 
            { 
            } 
  }; 
  GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) 
} 
 

In the preceding code, the gazebo/gazebo.hh file includes a core set of basic Gazebo functions. Since this is a case by case basis and type of plugin, it should include gazebo/physics/physics.hh, gazebo/rendering/rendering.hh, or gazebo/sensors/sensors.hh. Moreover, all of the plugins must be in the gazebo namespace and must inherit from a plugin type, which in this case is the WorldPlugin class. Here, one of the required functions is Load(), which receives an SDF element that contains the elements and attributes that are specified in the loaded SDF file. We will discuss more about the SDF file in the upcoming chapter, Chapter 4ROS Visualization and Debugging Tools while we simulate the design of the robot model and its world.

Finally, the plugin must be registered with the simulator using the GZ_REGISTER_WORLD_PLUGIN macro. The only parameter to this macro is the name of the plugin class. There are matching register macros for each plugin type, which are as follows: GZ_REGISTER_MODEL_PLUGIN, GZ_REGISTER_SENSOR_PLUGIN, GZ_REGISTER_GUI_PLUGIN, GZ_REGISTER_SYSTEM_PLUGIN, and GZ_REGISTER_VISUAL_PLUGIN. They should be used on a case by case basis.

..................Content has been hidden....................

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