Creating a Gazebo plugin

From The Player Project

Revision as of 15:11, 28 June 2010 by Natepak (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Creating a Plugin for Gazebo

This tutorial will cover how to access and control the internals of Gazebo through a very simple plugin interface.

The original purpose of this plugin interface was to provide the ability to animate objects in a world. However, it's also possible to control almost every aspect of Gazebo.

The Code

This plugin interface allows you to directly insert (and remove) a chunk of code into a running Gazebo simulation. The following is an example plugin that listens to the World Update Callback (which is fired once every step of the simulator), and prints out a message.

#include <boost/bind.hpp>
#include <gazebo/gazeboserver.hh>

namespace gazebo
  class Elevator : public Plugin
    /// Constructor
    public: Elevator() : Plugin() 
      open_ = 0;
      raise_ = 0;
    /// Mandatory load function, neded by gazebo
    public: void Load()
      // Get the right and left doors, and the lift
      rightDoor_ = (Body*)World::Instance()->GetEntityByName("doors::right");
      leftDoor_ = (Body*)World::Instance()->GetEntityByName("doors::left");
      lift_ = (Body*)World::Instance()->GetEntityByName("lift::body");

      // Get the first contact sensor
      contact_ = (Geom*)World::Instance()->GetEntityByName("contact::body::COM_Entity::geom");
      contact_->ConnectContactCallback(boost::bind(&Elevator::ContactCB, this));

      // Get the second contact sensor
      contact2_ = (Geom*)World::Instance()->GetEntityByName("lift::body::COM_Entity::contact");
      contact2_->ConnectContactCallback(boost::bind(&Elevator::Contact2CB, this));

      // Get an update callback
      World::Instance()->ConnectWorldUpdateStartSignal(boost::bind(&Elevator::UpdateCB, this));

    /// Gazebo callback that contact occured on the plate in front of the door
    private: void ContactCB()
      open_ = 1;

    /// Gazebo callback that contact occured on the plate in the elevator
    private: void Contact2CB()
      raise_ = 1;

    /// Gazebo update callback
    public: void UpdateCB()
      if (open_ == 1)
        rightDoor_->SetLinearVel(Vector3(0, -0.5, 0));
        leftDoor_->SetLinearVel(Vector3(0, 0.5, 0));

      if (raise_ == 1)

    private: int open_, raise_;
    private: Body *rightDoor_, *leftDoor_, *lift_;
    private: Geom *contact_, *contact2_;

  /// Register this plugin with gazebo. This is mandatory
  GZ_REGISTER_PLUGIN("Elevator", Elevator)
Personal tools