当前位置:网站首页>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 :
边栏推荐
- Match VIM from zero (0) -- Introduction to vimscript
- CISP-PTE之命令注入篇
- 字符串的相关编程题
- 一起看看matlab工具箱内部是如何实现BP神经网络的
- Blue Bridge Cup 2022 13th provincial competition real topic - block painting
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- uva 1401 dp+Trie
- Shortcut keys commonly used in idea
- AcWing 904. 虫洞 题解(spfa求负环)
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
猜你喜欢
刨析《C语言》【进阶】付费知识【一】
Comparison of picture beds of free white whoring
Shell script quickly counts the number of lines of project code
Scenario practice: quickly build wordpress blog system based on function calculation
LeetCode:1175. Prime permutation
tansig和logsig的差异,为什么BP喜欢用tansig
Recognition of C language array
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
我如何编码8个小时而不会感到疲倦。
Modify the system time of Px4 flight control
随机推荐
ROS学习(22)TF变换
The use of video in the wiper component causes full screen dislocation
对C语言数组的再认识
Use nodejs to determine which projects are packaged + released
Share a general compilation method of so dynamic library
Analyze "C language" [advanced] paid knowledge [End]
C language instance_ four
蓝桥杯2022年第十三届省赛真题-积木画
JVM memory model
When grep looks for a process, it ignores the grep process itself
Related programming problems of string
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
HDU 4661 message passing (wood DP & amp; Combinatorics)
WCF Foundation
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
The difference between Tansig and logsig. Why does BP like to use Tansig
Google released a security update to fix 0 days that have been used in chrome
IDEA常用的快捷键
grep查找进程时,忽略grep进程本身
JS Es5 can also create constants?