Difference between revisions of "Write a plugin"

From Lofaro Lab Wiki
Jump to: navigation, search
 
Line 11: Line 11:
 
---Hello World plugin---
 
---Hello World plugin---
  
    <nowiki>$ mkdir ~/gazebo_plugin_tutorial
+
<syntaxhighlight lang="bash">$ mkdir ~/gazebo_plugin_tutorial
 
$ cd ~/gazebo_plugin_tutorial
 
$ cd ~/gazebo_plugin_tutorial
$ gedit hello_world.cc</nowiki>
+
$ gedit hello_world.cc</syntaxhighlight>
  
 
1) Copy the following into hello_world.cc
 
1) Copy the following into hello_world.cc
  
    <nowiki>#include <gazebo/gazebo.hh>
+
<syntaxhighlight lang="xml">#include <gazebo/gazebo.hh>
  
 
namespace gazebo
 
namespace gazebo
Line 33: Line 33:
 
   };
 
   };
 
   GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial)
 
   GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial)
}</nowiki>
+
}</syntaxhighlight>
  
 
2) To compile plugin, create the following
 
2) To compile plugin, create the following
  
    <nowiki>$ gedit ~/gazebo_plugin_tutorial/CMakeLists.txt</nowiki>
+
<syntaxhighlight lang="bash">$ gedit ~/gazebo_plugin_tutorial/CMakeLists.txt</syntaxhighlight>
  
 
3) Copy the following into the file created above
 
3) Copy the following into the file created above
  
    <nowiki>cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
+
<syntaxhighlight lang="bash">cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
  
 
find_package(Boost REQUIRED COMPONENTS system)
 
find_package(Boost REQUIRED COMPONENTS system)
Line 55: Line 55:
  
 
add_library(hello_world SHARED hello_world.cc)
 
add_library(hello_world SHARED hello_world.cc)
target_link_libraries(hello_world ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})</nowiki>
+
target_link_libraries(hello_world ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})</syntaxhighlight>
  
 
4) Create build directory, by following below
 
4) Create build directory, by following below
  
    <nowiki>$ mkdir ~/gazebo_plugin_tutorial/build
+
<syntaxhighlight lang="bash">$ mkdir ~/gazebo_plugin_tutorial/build
$ cd ~/gazebo_plugin_tutorial/build</nowiki>
+
$ cd ~/gazebo_plugin_tutorial/build</syntaxhighlight>
  
 
5) Compile the code, by following below
 
5) Compile the code, by following below
  
    <nowiki>$ cmake ../
+
<syntaxhighlight lang="bash">$ cmake ../
$ make</nowiki>
+
$ make</syntaxhighlight>
  
 
6) Add library path  
 
6) Add library path  
  
    <nowiki>$ export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build</nowiki>
+
<syntaxhighlight lang="bash">$ export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build</syntaxhighlight>
  
 
'''Model plugins'''
 
'''Model plugins'''
  
1) <nowiki>$ cd ~/gazebo_plugin_tutorial
+
1) <syntaxhighlight lang="bash">$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.cc</nowiki>
+
$ gedit model_push.cc</syntaxhighlight>
  
 
2) Add the following into model_push.cc
 
2) Add the following into model_push.cc
  
    <nowiki>#include <boost/bind.hpp>
+
<syntaxhighlight lang="xml">#include <boost/bind.hpp>
 
#include <gazebo/gazebo.hh>
 
#include <gazebo/gazebo.hh>
 
#include <gazebo/physics/physics.hh>
 
#include <gazebo/physics/physics.hh>
Line 115: Line 115:
 
   // Register this plugin with the simulator
 
   // Register this plugin with the simulator
 
   GZ_REGISTER_MODEL_PLUGIN(ModelPush)
 
   GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}</nowiki>
+
}</syntaxhighlight>
  
 
3) Add the following to ~/gazebo_plugin_tutorial/CMakeLists.txt
 
3) Add the following to ~/gazebo_plugin_tutorial/CMakeLists.txt
  
    <nowiki>add_library(model_push SHARED model_push.cc)
+
<syntaxhighlight lang="xml">add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})</nowiki>
+
target_link_libraries(model_push ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})</syntaxhighlight>
  
 
4) Compile  
 
4) Compile  
  
    <nowiki>$ cd ~/gazebo_plugin_tutorial/build
+
<syntaxhighlight lang="bash">$ cd ~/gazebo_plugin_tutorial/build
 
$ cmake ../
 
$ cmake ../
$ make</nowiki>
+
$ make</syntaxhighlight>
  
 
'''Running Plugin'''
 
'''Running Plugin'''
  
 
1)  
 
1)  
    <nowiki>$ cd ~/gazebo_plugin_tutorial
+
<syntaxhighlight lang="bash">$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.world</nowiki>
+
$ gedit model_push.world</syntaxhighlight>
  
 
2)  
 
2)  
    <nowiki><?xml version="1.0"?>  
+
<syntaxhighlight lang="xml"><?xml version="1.0"?>  
 
<sdf version="1.4">
 
<sdf version="1.4">
 
   <world name="default">
 
   <world name="default">
Line 171: Line 171:
 
     </model>         
 
     </model>         
 
   </world>
 
   </world>
</sdf></nowiki>
+
</sdf></syntaxhighlight>
  
 
3) Start simulation
 
3) Start simulation
  
    <nowiki>$ cd ~/gazebo_plugin_tutorial/
+
<syntaxhighlight lang="bash">$ cd ~/gazebo_plugin_tutorial/
$ gzserver -u model_push.world</nowiki>
+
$ gzserver -u model_push.world</syntaxhighlight>

Latest revision as of 23:56, 11 November 2014

Plugins 101

This tutorial is based off from this link.

Plugins 101

The plugin has direct access to all functionality of Gazebo. They are useful because they allow us to control any aspect of Gazebo.

There are 5 types of plugins: world, model, sensor, system and visual.

---Hello World plugin---

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

1) Copy the following into hello_world.cc

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

2) To compile plugin, create the following

$ gedit ~/gazebo_plugin_tutorial/CMakeLists.txt

3) Copy the following into the file created above

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})

4) Create build directory, by following below

$ mkdir ~/gazebo_plugin_tutorial/build
$ cd ~/gazebo_plugin_tutorial/build

5) Compile the code, by following below

$ cmake ../
$ make

6) Add library path

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

Model plugins

1)
$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.cc

2) Add the following into model_push.cc

#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
 
namespace gazebo
{
  class ModelPush : public ModelPlugin
  {
    public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
    {
      // Store the pointer to the model
      this->model = _parent;
 
      // Listen to the update event. This event is broadcast every
      // simulation iteration.
      this->updateConnection = event::Events::ConnectWorldUpdateBegin(
          boost::bind(&ModelPush::OnUpdate, this, _1));
    }
 
    // Called by the world update start event
    public: void OnUpdate(const common::UpdateInfo & /*_info*/)
    {
      // Apply a small linear velocity to the model.
      this->model->SetLinearVel(math::Vector3(.03, 0, 0));
    }
 
    // Pointer to the model
    private: physics::ModelPtr model;
 
    // Pointer to the update event connection
    private: event::ConnectionPtr updateConnection;
  };
 
  // Register this plugin with the simulator
  GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}

3) Add the following to ~/gazebo_plugin_tutorial/CMakeLists.txt

add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})

4) Compile

$ cd ~/gazebo_plugin_tutorial/build
$ cmake ../
$ make

Running Plugin

1)

$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.world

2)

<?xml version="1.0"?> 
<sdf version="1.4">
  <world name="default">
 
    <!-- Ground Plane -->
    <include>
      <uri>model://ground_plane</uri>
    </include>
 
    <include>
      <uri>model://sun</uri>
    </include>
 
    <model name="box">
      <pose>0 0 0.5 0 0 0</pose>
      <link name="link">
        <collision name="collision">
          <geometry>
            <box>
              <size>1 1 1</size>
            </box>
          </geometry>
        </collision>
 
        <visual name="visual">
          <geometry>
            <box>
              <size>1 1 1</size>
            </box>
          </geometry>
        </visual>
      </link>
 
      <plugin name="model_push" filename="build/libmodel_push.so"/>
    </model>        
  </world>
</sdf>

3) Start simulation

$ cd ~/gazebo_plugin_tutorial/
$ gzserver -u model_push.world