来源 http://wiki.ros.org/pluginlib/Tutorials/Writing%20and%20Using%20a%20Simple%20Plugin
代码解释参考https://www.ncnynl.com/archives/201702/1323.html
具体步骤如下
准备
进入工作空间
cd ~/pluginlib_ws/src/
创建pluginlib_tutorials_示例包
catkin_create_pkg pluginlib_tutorials_ roscpp pluginlib
1 创建插件库
1.1 创建基类
vim pluginlib_tutorials_/include/pluginlib_tutorials_/polygon_base.h
基类polygon_base.h代码:
#ifndef PLUGINLIB_TUTORIALS__POLYGON_BASE_H_
#define PLUGINLIB_TUTORIALS__POLYGON_BASE_H_
namespace polygon_base
{
class RegularPolygon
{
public:
virtual void initialize(double side_length) = 0;
virtual double area() = 0;
virtual ~RegularPolygon(){}
protected:
RegularPolygon(){}
};
};
#endif
1.2 创建插件
vim pluginlib_tutorials_/include/pluginlib_tutorials_/polygon_plugins.h
代码如下:
#ifndef PLUGINLIB_TUTORIALS__POLYGON_PLUGINS_H_
#define PLUGINLIB_TUTORIALS__POLYGON_PLUGINS_H_
#include <pluginlib_tutorials_/polygon_base.h>
#include <cmath>
namespace polygon_plugins
{
class Triangle : public polygon_base::RegularPolygon
{
public:
Triangle(){}
void initialize(double side_length)
{
side_length_ = side_length;
}
double area()
{
return 0.5 * side_length_ * getHeight();
}
double getHeight()
{
return sqrt((side_length_ * side_length_) - ((side_length_ / 2) * (side_length_ / 2)));
}
private:
double side_length_;
};
class Square : public polygon_base::RegularPolygon
{
public:
Square(){}
void initialize(double side_length)
{
side_length_ = side_length;
}
double area()
{
return side_length_ * side_length_;
}
private:
double side_length_;
};
};
#endif
1.3 注册插件
vim pluginlib_tutorials_/src/polygon_plugins.cpp
代码如下:
#include <pluginlib/class_list_macros.h>
#include <pluginlib_tutorials_/polygon_base.h>
#include <pluginlib_tutorials_/polygon_plugins.h>
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon)
构建插件库
在CMakeLists.txt文件中添加
include_directories(include)
add_library(polygon_plugins src/polygon_plugins.cpp)
编译
2 使插件可用于ROS工具链
创建一个XML文件,还需要再包清单中的建立特殊导出行,就可以为ROS工具链提供有关插件的所有必要信息
2.1 创建XML文件
vim pluginlib_tutorials_/polygon_plugins.xml
polygon_plugins.xml位于包的顶层,与CMakeLists.txt和package.xml在一起
代码如下:
<library path="lib/libpolygon_plugins">
<class type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon">
<description>This is a triangle plugin.</description>
</class>
<class type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon">
<description>This is a square plugin.</description>
</class>
</library>
2.2 导出插件
在package.xml中添加
<export>
<pluginlib_tutorials_ plugin="${prefix}/polygon_plugins.xml" />
</export>
验证是否生效:
rospack plugins --attrib=plugin pluginlib_tutorials_
如果生效,可以看见polygon_plugins.xml文件的完整路径
2.3 使用插件
vim pluginlib_tutorials_/src/polygon_loader.cpp
代码如下
#include <pluginlib/class_loader.h>
#include <pluginlib_tutorials_/polygon_base.h>
int main(int argc, char** argv)
{
pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("pluginlib_tutorials_", "polygon_base::RegularPolygon");
try
{
boost::shared_ptr<polygon_base::RegularPolygon> triangle = poly_loader.createInstance("polygon_plugins::Triangle");
triangle->initialize(10.0);
boost::shared_ptr<polygon_base::RegularPolygon> square = poly_loader.createInstance("polygon_plugins::Square");
square->initialize(10.0);
ROS_INFO("Triangle area: %.2f", triangle->area());
ROS_INFO("Square area: %.2f", square->area());
}
catch(pluginlib::PluginlibException& ex)
{
ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
}
return 0;
}
3 运行插件
在CMakeLists.txt文件中添加以下行
add_executable(polygon_loader src/polygon_loader.cpp)
target_link_libraries(polygon_loader ${catkin_LIBRARIES})
编译
运行
rosrun pluginlib_tutorials_ polygon_loader
[ INFO] [1534856640.660509344]: Triangle area: 43.30
[ INFO] [1534856640.660569938]: Square area: 100.00
完整工程见 https://github.com/YangFighting/ros_tutorial/tree/master/pluginlib_tutorials_
本文如有问题,请与我联系 yangzhang_y@163.com