当前位置:网站首页>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 :
边栏推荐
猜你喜欢
Set WordPress pseudo static connection (no pagoda)
Comparison of picture beds of free white whoring
我如何编码8个小时而不会感到疲倦。
Scenario practice: quickly build wordpress blog system based on function calculation
mongodb查看表是否导入成功
一起看看matlab工具箱内部是如何实现BP神经网络的
Right mouse button customization
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
tansig和logsig的差异,为什么BP喜欢用tansig
永久的摇篮
随机推荐
爬虫实战(六):爬笔趣阁小说
Mysqlbackup restores specific tables
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
C language [23] classic interview questions [Part 2]
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
ROS学习(25)rviz plugin插件
Today's question -2022/7/4 modify string reference type variables in lambda body
Basic introduction and use of dvajs
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
Use nodejs to determine which projects are packaged + released
ROS学习(22)TF变换
Gin introduction practice
js如何快速创建一个长度为 n 的数组
Yiwen takes you into [memory leak]
Drag to change order
Comparison of picture beds of free white whoring
POJ 3177 redundant paths POJ 3352 road construction (dual connection)
Set WordPress pseudo static connection (no pagoda)