当前位置:网站首页>ROS learning (24) plugin

ROS learning (24) plugin

2022-07-07 01:52:00 Ice cream with code


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 :
 Insert picture description here
Implementing a plug-in requires the following steps :

  1. Create base class , Define a unified interface ( If you implement a plug-in based on an existing base class , Skip this step )
  2. establish plugin class , Inherited base class , Realize a unified interface
  3. Register plug-ins ( Use pluginlib Complete the registration of macros for )
  4. Compile and generate the dynamic link library of plug-ins ( modify CMakefile.txt file )
  5. 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

  1. 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 .

  1. 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 :
 Insert picture description here

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 :
 Insert picture description here

原网站

版权声明
本文为[Ice cream with code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061812138944.html