当前位置:网站首页>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 :
边栏推荐
- 公钥\私人 ssh避password登陆
- 刨析《C语言》【进阶】付费知识【完结】
- Shortcut keys commonly used in idea
- 鼠标右键 自定义
- 2022/0524/bookstrap
- Yunna - work order management system and process, work order management specification
- 设置Wordpress伪静态连接(无宝塔)
- Let's see how to realize BP neural network in Matlab toolbox
- Share a general compilation method of so dynamic library
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
猜你喜欢

鼠标右键 自定义

454-百度面经1

爬虫实战(六):爬笔趣阁小说

微服务架构介绍

ROS学习(24)plugin插件

AcWing 345. Cattle station solution (nature and multiplication of Floyd)

ROS learning (23) action communication mechanism

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

Yunna | work order management measures, how to carry out work order management

ROS learning (22) TF transformation
随机推荐
图片打水印 缩放 和一个输入流的转换
2022/0524/bookstrap
Related programming problems of string
盒子拉伸拉扯(左右模式)
Shortcut keys commonly used in idea
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
ROS学习(二十)机器人SLAM功能包——rgbdslam的安装与测试
WCF Foundation
Google released a security update to fix 0 days that have been used in chrome
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
Reptile practice (VI): novel of climbing pen interesting Pavilion
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
MySQL's most basic select statement
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Get to know MySQL for the first time
AcWing 361. 观光奶牛 题解(spfa求正环)
Let's see how to realize BP neural network in Matlab toolbox
How did partydao turn a tweet into a $200million product Dao in one year
制作带照明的DIY焊接排烟器
Set WordPress pseudo static connection (no pagoda)