插件Plugin
首先需要注册。The pluginlib package provides tools for writing and dynamically loading plugins using the ROS build infrastructure. To work, these tools require plugin providers to register their plugins in the package.xml of their package.

动态加载。pluginlib is a C++ library for loading and unloading plugins from within a ROS package. Plugins are dynamically loadable classes that are loaded from a runtime library (i.e. shared object, dynamically linked library). With pluginlib, one does not have to explicitly link their application against the library containing the classes — instead pluginlib can open a library containing exported classes at any point without the application having any prior awareness of the library or the header file containing the class definition. Plugins are useful for extending/modifying application behavior without needing the application source code.

例如。To understand how pluginlib works, let’s consider a small example. First, suppose that a ROS package exists containing a polygon base class (“polygon_interface_package”). Let’s also say that there are two different kinds of polygons supported in the system: a rectangle which lives in the “rectangle_plugin” package and a triangle that lives in the “triangle_plugin” package. The implementers of both the rectangle_plugin and triangle_plugin packages would include special export lines in their package.xml file telling the rosbuild system that they intend to provide plugins for the polygon class in the polygon_interface_package package.
These export lines, in effect, register the classes with the ROS build/packaging system. This means that someone wishing to see all polygon classes available in the system can run a simple rospack query which will return a list of available classes, in this case, rectangle and triangle.

1. Providing a Plugin
1.1 Registering/Exporting a Plugin
In order to allow a class to be dynamically loaded, it must be marked as an exported class. This is done through the special macro PLUGINLIB_EXPORT_CLASS. This macro can be put into any source (.cpp) file that composes the plugin library, but is usually put at the end of the .cpp file for the exported class. For the example above, we might create a class_list.cpp file in package ‘example_pkg’ that looks as follows and compile it into the librectangle library:
#include #include #include
//Declare the Rectangle as a Polygon class
PLUGINLIB_EXPORT_CLASS(rectangle_namespace::Rectangle, polygon_namespace::Polygon)
1.2 The Plugin Description File
The plugin description file is an XML file that serves to store all the important information about a plugin in a machine readable format. It contains information about the library the plugin is in, the name of the plugin, the type of the plugin, etc. If we consider the rectangle_plugin package discussed above, the plugin description file (e.g. rectangle_plugin.xml), would look something like this:

This is a rectangle plugin

We need this file in addition to the code macro to allow the ROS system to automatically discover, load, and reason about plugins. The plugin description file also holds important information, like a description of the plugin, that doesn’t fit well in the macro.
1.3 Registering Plugin with ROS Package System
In order for pluginlib to query all available plugins on a system across all ROS packages, each package must explicitly specify the plugins it exports and which package libraries contain those plugins. A plugin provider must point to its plugin description file in its package.xml inside the export tag block. Note, if you have other exports they all must go in the same export field. Considering the rectangle_plugin package again, the relevant lines would look as follows:

Important Note: In order for the above export command to work properly, the providing package must depend directly on the package containing the plugin interface. For example, the rectangle_plugin must have the line below in its catkin/package.xml:
polygon_interface_package
polygon_interface_package
1.4 Querying ROS Package System For Available Plugins
One can query the ROS package system via rospack to see which plugins are available by any given package. For example:
rospack plugins –attrib=plugin nav_core
This will return all plugins exported from the nav_core package.

2. Using a Plugin
pluginlib provides a ClassLoader class available in the class_loader.h header that makes it quick and easy to use provided classes.Below, we’ll show a simple example of using the ClassLoader to create an instance of a rectangle in some code that uses a polygon:
#include #include //… some code …
pluginlib::ClassLoader poly_loader(“polygon_interface_package”, “polygon_namespace::Polygon”);

try
{
boost::shared_ptr poly = poly_loader.createInstance(“rectangle_namespace::Rectangle”);

//… use the polygon, boost::shared_ptr will automatically delete memory when it goes out of scope
}
catch(pluginlib::PluginlibException& ex)
{
//handle the class failing to load
ROS_ERROR(“The plugin failed to load for some reason. Error: %s”, ex.what());
}
Important Note: The ClassLoader must not go out scope while you are using the plugin. So, if you are loading a plugin object inside a class, make sure that the class loader is a member variable of that class.