ROS2编写一个简单的插件
1.create a base class package
ros2 pkg create --build-type ament_cmake --license Apache-2.0 --dependencies pluginlib --node-name area_node polygon_base
编辑ros2_ws/src/polygon_base/include/polygon_base/regular_polygon.hpp
#ifndef POLYGON_BASE_REGULAR_POLYGON_HPP
#define POLYGON_BASE_REGULAR_POLYGON_HPPnamespace polygon_base
{class RegularPolygon{public:virtual void initialize(double side_length) = 0;virtual double area() = 0;virtual ~RegularPolygon(){}protected:RegularPolygon(){}};
} // namespace polygon_base#endif // POLYGON_BASE_REGULAR_POLYGON_HPP
编辑CMakeLists.txt
install(DIRECTORY include/DESTINATION include
)
ament_export_include_directories(include
)
2. create the plugin package
ros2 pkg create --build-type ament_cmake --license Apache-2.0 --dependencies polygon_base pluginlib --library-name polygon_plugins polygon_plugins
编辑ros2_ws/src/polygon_plugins/src/polygon_plugins.cpp
#include <polygon_base/regular_polygon.hpp>
#include <cmath>namespace polygon_plugins
{class Square : public polygon_base::RegularPolygon{public:void initialize(double side_length) override{side_length_ = side_length;}double area() override{return side_length_ * side_length_;}protected:double side_length_;};class Triangle : public polygon_base::RegularPolygon{public:void initialize(double side_length) override{side_length_ = side_length;}double area() override{return 0.5 * side_length_ * getHeight();}double getHeight(){return sqrt((side_length_ * side_length_) - ((side_length_ / 2) * (side_length_ / 2)));}protected:double side_length_;};
}#include <pluginlib/class_list_macros.hpp>
// 导出插件类
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)
这两行代码使用 ROS 的pluginlib
库将两个类注册为插件。
Create ros2_ws/src/polygon_plugins/plugins.xml
with the following code:
<library path="polygon_plugins"><class type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon"><description>This is a square plugin.</description></class><class type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon"><description>This is a triangle plugin.</description></class>
</library>
编辑ros2_ws/src/polygon_plugins/CMakeLists.txt,after the line reading find_package(pluginlib REQUIRED)
:
pluginlib_export_plugin_description_file(polygon_base plugins.xml)
3.使用插件
Edit ros2_ws/src/polygon_base/src/area_node.cpp
#include <pluginlib/class_loader.hpp>
#include <polygon_base/regular_polygon.hpp>int main(int argc, char** argv)
{// To avoid unused parameter warnings(void) argc;(void) argv;pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("polygon_base", "polygon_base::RegularPolygon");try{std::shared_ptr<polygon_base::RegularPolygon> triangle = poly_loader.createSharedInstance("polygon_plugins::Triangle");triangle->initialize(10.0);std::shared_ptr<polygon_base::RegularPolygon> square = poly_loader.createSharedInstance("polygon_plugins::Square");square->initialize(10.0);printf("Triangle area: %.2f\n", triangle->area());printf("Square area: %.2f\n", square->area());}catch(pluginlib::PluginlibException& ex){printf("The plugin failed to load for some reason. Error: %s\n", ex.what());}return 0;
}
编译
colcon build --packages-select polygon_base polygon_plugins