当前位置:网站首页>ROS learning (24) plugin
ROS learning (24) plugin
2022-07-07 01:52:00 【Ice cream with code】
List of articles
Preface
ROS The plug-ins in are extension function classes that can be loaded dynamically .
ROS Medium pluginlib The function pack provides loading and unloading plugin Of C++ library , When developers use plug-ins , There is no need to consider plugin Link location of class , Just register the plug-in to pluginlib in , You can directly and dynamically load .
One 、 working principle
As shown in the figure :
Implementing a plug-in requires the following steps :
- Create base class , Define a unified interface ( If you implement a plug-in based on an existing base class , Skip this step )
- establish plugin class , Inherited base class , Realize a unified interface
- Register plug-ins ( Use pluginlib Complete the registration of macros for )
- Compile and generate the dynamic link library of plug-ins ( modify CMakefile.txt file )
- Add the plug-in to ROS in ( Create and modify corresponding xml file )
Two 、 Concrete realization
Follow these steps , The following use pluginlib Implement a plug-in . Create a pluginlib_tutorials Function pack , The order is as follows :
catkin_create_pkg pluginlib_tutorials roscpp pluginlib
Be careful , Create with pluginlib rely on .
1、 Create base class
stay include/pluginlib_tutorials/polygon_bash.h In file , establish polygon Base class , Define some interfaces , Be careful initialize() Use of interfaces . The contents of the document are as follows :
#ifndef PLUGINLIB_TUTORIALS_POLYGON_BASE_H_
#define PLUGINLIB_TUTORIALS_POLYGON_BASE_H_
namespace polygon_base
{
class RegularPolygon
{
public:
//pluginlib Constructor must not take parameters , So define initialize To complete the work that needs to be initialized
virtual void initialize(double side_length) = 0;
// Interface function for calculating area
virtual double area() = 0;
virtual ~RegularPolygon(){
}
protected:
RegularPolygon(){
}
};
};
#endif
2、 establish plugin class
stay include/pluginlib_tutorials/polygon_plugins.h In file , Definition rectangle_plugin and triangle_plugin class , Implement the interface of the base class , You can also add plugin Interface required by itself . The contents of the document are as follows :
#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() : side_length_() {
}
// Initialize side length
void initialize(double side_length)
{
side_length_ = side_length;
}
double area()
{
return 0.5 * side_length_ * getHeight();
}
// Triangle Class's own interface
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() : side_length_() {
}
// Initialize side length
void initialize(double side_length)
{
side_length_ = side_length;
}
double area()
{
return side_length_ * side_length_;
}
private:
double side_length_;
};
};
#endif
3、 Register plug-ins
stay src/pluginlib_tutorials/polygon_plugins.cpp In file , Register the created plug-in . The contents of the document are as follows :
// contain pluginlib The header file , Use pluginlib To register the plug-in
#include <pluginlib/class_list_macros.h>
#include <pluginlib_tutorials/polygon_base.h>
#include <pluginlib_tutorials/polygon_plugins.h>
// Register plug-ins , Macro parameters :plugin Implementation class of ,plugin Base class of
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon);
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon);
4、 Compile the dynamic link library of the plug-in
modify CMakefile.txt file , as follows :
add_library(pluginlib_tutorials src/polygon_plugins.cpp)
target_link_libraries(pluginlib_tutorials ${
catkin_LIBRARIES})
5、 Add plug-ins to ROS
- establish src/pluginlib_tutorials/polygon_plugins.xml file , The contents are as follows :
<library path="lib/libpluginlib_tutorials">
<class name="pluginlib_tutorials/regular_triangle" type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon">
<description>This is a triangle plugin.</description>
</class>
<class name="pluginlib_tutorials/regular_square" type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon">
<description>This is a square plugin.</description>
</class>
</library>
This XML It mainly describes plugin Dynamic library path 、 Implementation class 、 Base class 、 Function description and other information .
- modify src/pluginlib_tutorials/package.xml file , The contents are as follows :
<?xml version="1.0"?>
<package>
<name>pluginlib_tutorials</name>
<version>0.1.10</version>
<description>The pluginlib_tutorials package</description>
<maintainer email="[email protected]">Daniel Stonier</maintainer>
<license>BSD</license>
<url type="website">http://www.ros.org/wiki/pluginlib/Tutorials</url>
<url type="bugtracker">https://github.com/ros/common_tutorials/issues</url>
<url type="repository">https://github.com/ros/common_tutorials/</url>
<author>Eitan Marder-Eppstein</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>pluginlib</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>pluginlib</run_depend>
<run_depend>roscpp</run_depend>
<export>
<pluginlib_tutorials plugin="${prefix}/polygon_plugins.xml" />
</export>
</package>
You can use the following command , Check the plug-in path of the function package :
rospack plugins --attrib=plugin pluginlib_tutorials
If the configuration is correct , The effect is as follows :
6、 Call plug-ins
All the code of the plug-in has been implemented above , Now start calling the plug-in , establish src/pluginlib_tutorials/polygon_loader.cpp file , The contents are as follows :
#include <boost/shared_ptr.hpp>
#include <pluginlib/class_loader.h>
#include <pluginlib_tutorials/polygon_base.h>
int main(int argc, char** argv)
{
// Create a ClassLoader, Used to load plugin
pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("pluginlib_tutorials", "polygon_base::RegularPolygon");
try
{
// load Triangle Plug in class , Path in polygon_plugins.xml In the definition of
boost::shared_ptr<polygon_base::RegularPolygon> triangle = poly_loader.createInstance("pluginlib_tutorials/regular_triangle");
// Initialize side length
triangle->initialize(10.0);
ROS_INFO("Triangle area: %.2f", triangle->area());
}
catch(pluginlib::PluginlibException& ex)
{
ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
}
try
{
boost::shared_ptr<polygon_base::RegularPolygon> square = poly_loader.createInstance("pluginlib_tutorials/regular_square");
square->initialize(10.0);
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;
}
modify CMakefile.txt file , as follows :
add_executable(polygon_loader src/polygon_loader.cpp)
target_link_libraries(polygon_loader ${
catkin_LIBRARIES})
7、 Running effect
After successful compilation , Run the plug-in , The order is as follows :
rosrun pluginlib_tutorials polygon_loader
The effect is as follows :
边栏推荐
- 刨析《C语言》【进阶】付费知识【完结】
- ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
- 使用nodejs完成判断哪些项目打包+发版
- Get to know MySQL for the first time
- 454 Baidu Mianjing 1
- Shell script quickly counts the number of lines of project code
- ROS学习(二十)机器人SLAM功能包——rgbdslam的安装与测试
- The difference between Tansig and logsig. Why does BP like to use Tansig
- 增加 pdf 标题浮窗
- C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
猜你喜欢

ROS学习(25)rviz plugin插件

C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

新工作感悟~辞旧迎新~

ROS学习(21)机器人SLAM功能包——orbslam的安装与测试

tansig和logsig的差异,为什么BP喜欢用tansig

CISP-PTE之命令注入篇

BigDecimal 的正确使用方式

今日问题-2022/7/4 lambda体中修改String引用类型变量

ROS learning (22) TF transformation
随机推荐
Comparison of picture beds of free white whoring
字符串转成日期对象
初识MySQL
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
mysqlbackup 还原特定的表
Match VIM from zero (0) -- Introduction to vimscript
Ds-5/rvds4.0 variable initialization error
Image watermarking, scaling and conversion of an input stream
编译命令行终端 swift
[advanced C language] 8 written questions of pointer
AcWing 345. 牛站 题解(floyd的性质、倍增)
String to date object
Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
Yiwen takes you into [memory leak]
ROS learning (22) TF transformation
Let's see how to realize BP neural network in Matlab toolbox
AcWing 1142. 繁忙的都市 题解(最小生成树)
JVM 内存模型
AcWing 1140. 最短网络 (最小生成树)