Skip to content

Creating a new plugin

David-rn edited this page Jan 23, 2020 · 3 revisions

The robotnik_pad_node loads plugins that specify the desired behaviour. In order to do that, the plugins must follow the generic_pad_plugin.h interface.

Let's say we want to create a Movement plugin, so we would create a new file inside robotnik_pad_plugins package called example_movement:

robotnik_pad_plugins
│   CMakeLists.txt
│   package.xml
│   robotnik_pad_pluginlib.xml 
│
└───include/robotnik_pad_plugins
│      example_movement.h
│      
└───src
    │   example_movement.cpp
    │   generic_pad_plugins.cpp

Then, in the header file example_movement.h that we've just created we have to inherit from the interface:

namespace pad_plugins
{
class PadPluginExampleMovement : public GenericPadPlugin
{
public:
  virtual void initialize(const ros::NodeHandle& nh, const std::string& plugin_ns);
  virtual void execute(std::vector<Button>& buttons, std::vector<float>& axes);
};
} // namespace

The next step is to add our desired behaviour in the example_movement.cpp file.

We have almost finished, but first we have to add our new cpp file to the CMakeLists in order to be compiled. Moreover, we need to register our class as a plugin, so in order to do that we should open generic_pad_plugin.cpp file and add our class:

#include <pluginlib/class_list_macros.h>

#include <robotnik_pad/generic_pad_plugin.h>
#include <robotnik_pad_plugins/example_movement.h>

PLUGINLIB_EXPORT_CLASS(pad_plugins::PadPluginExampleMovement, pad_plugins::GenericPadPlugin);

Finally, we have to make our plugin available for ROS. So, let's add our plugin in the robotnik_pad_pluginlib.xml file:

<library path="lib/librobotnik_pad_pluginlib">
    <class name="robotnik_pad_plugins/ExampleMovement" type="pad_plugins::PadPluginExampleMovement" base_class_type="pad_plugins::GenericPadPlugin">
        <description>This is the example movement plugin.</description>
    </class>
</library>

After all of these steps your class should be available as a plugin and ready to be loaded.

Clone this wiki locally