Write a plugin
From Lofaro Lab Wiki
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