Creating a Gazebo plugin

From The Player Project

Jump to: navigation, search
(New page: == 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...)
(The Code)
Line 5: Line 5:
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 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 ===
+
=== An Example ===
-
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.
+
This plugin interface allows you to directly insert (and remove) a chunk of code into a running Gazebo simulation.  
 +
The following is a stub for a plugin
 +
 +
<pre>
 +
#include <gazebo/gazeboserver.hh>
 +
 +
namespace gazebo
 +
{
 +
  class ExamplePlugin : public Plugin
 +
  {
 +
    /// Constructor
 +
    public: ExamplePlugin() : Plugin()
 +
    {
 +
    }
 +
    /// Mandatory load function, needed by gazebo
 +
    public: void Load()
 +
    {
 +
    }
 +
  };
 +
 +
  /// Register this plugin with gazebo. This is mandatory
 +
  GZ_REGISTER_PLUGIN("ExamplePlugin", ExamplePlugin)
 +
}
 +
</pre>
 +
 +
The <tt>gazebo/gazeboserver.hh</tt> include will bring in everything needed to compile the plugin. Each plugin must inherit from the <tt>Plugin</tt> base class, define a <tt>Load</tt> function, and register itself using the <tt>GZ_REGISTER_PLUGIN</tt> macro.
 +
 +
A package config file is included in the Gazebo install called gazeboserver.pc that should make compilation easy:
 +
<pre>
 +
pkg-config --libs --cflags gazeboserver
 +
</pre>
 +
There is also an example cmake file located in the Gazebo sources in the <tt>plugins</tt> directory.
 +
 +
 +
The following is an example plugin that works in conjunction with the elevator.world file that is located in the <tt>worlds</tt> directory in the sources.
<pre>
<pre>
#include <boost/bind.hpp>
#include <boost/bind.hpp>
Line 82: Line 116:
   GZ_REGISTER_PLUGIN("Elevator", Elevator)
   GZ_REGISTER_PLUGIN("Elevator", Elevator)
}
}
 +
</pre>
 +
 +
This example plugin connects to the World's Update signal, which is called once every iteration of the simulation. Two geometries that act as contact sensors exist in the world. Within a running simulation, these two geoms are colored red. The elevator plugin connects to each of the geom's contact signals. When the first geom is hit, then the doors to the elevator are opened. When the second contact is hit, then the elevator is raised.
 +
 +
Examples of how to access bodies, geoms, and callbacks are provided in this example plugin. In order to use this plugin, use the follow steps:
 +
 +
1. Compile the elevator plugin (you should be in the directory containing the gazebo sources):
 +
<pre>
 +
mkdir plugins/build
 +
</pre>
 +
<pre>
 +
cd plugins/build
 +
</pre>
 +
<pre>
 +
cmake ../
 +
</pre>
 +
<pre>
 +
make
 +
</pre>
 +
 +
2. In another terminal, launch gazebo with the elevator.world:
 +
<pre>
 +
gazebo worlds/elevator.world
 +
</pre>
 +
 +
3. In the terminal in which you compiled the plugins, load the elevator plugin:
 +
<pre>
 +
gazeboplugin add ./libelevator.so elevator
 +
</pre>
 +
The <tt>gazeboplugin</tt> command line tool allows you to insert, list, and remove plugins. The first parameter specifies the action (add or list or remove). When adding, the second argument is the location of the plugin file, and the third argument is a name for the plugin.
 +
It is also possible to list the currently running plugins:
 +
<pre>
 +
gazeboplugin list
 +
</pre>
 +
And to remove a plugin you only have to specify the name given to the plugin:
 +
<pre>
 +
gazeboplugin remove elevator
</pre>
</pre>

Revision as of 16:05, 28 June 2010

Personal tools