当前位置: 首页 > backend >正文

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

http://www.xdnf.cn/news/16472.html

相关文章:

  • 2025年7月一区SCI-基尔霍夫定律优化算法Kirchhoff’s law algorithm-附Matlab免费代码
  • HDFS Block与Spark的partition对比
  • 基于AFLFast的fuzz自动化漏洞挖掘(2)
  • 中型企业如何用 RUM 技术破解地理分布式用户体验难题?从指标监测到优化实操
  • 嵌入式开发学习———Linux环境下数据结构学习(四)
  • Cacti RCE漏洞复现
  • 【AlphaFold3】网络架构篇(2)|Input Embedding 对输入进行特征嵌入
  • halcon-blob
  • docker 入门,运行上传自己的首个镜像
  • 学习人工智能所需知识体系及路径详解
  • CTF-Web学习笔记:文件包含篇
  • java中一些数据结构的转换
  • ts学习3
  • CES Asia 2025:以创新为笔,书写亚洲科技新纪元
  • python-内存管理
  • 安宝特方案丨智能革新安全管控:AR技术赋能物流仓储行业安全升级
  • 【STM32编码器接口测速】实现测速功能
  • strcmp 与 strcpy 的深入解析
  • Day 24:元组与os模块
  • 基于Hadoop3.3.4+Flink1.17.0+FlinkCDC3.0.0+Iceberg1.5.0整合,实现数仓实时同步mysql数据
  • 基于springboot的在线购票系统/在线售票系统
  • C++ 中实现 `Task::WhenAll` 和 `Task::WhenAny` 的两种方案
  • Gradle#Plugin
  • Triton编译
  • JavaScript - 实现套索工具的demo
  • MySQL 8.0.42创建MGR集群
  • 深度学习(鱼书)day04--手写数字识别项目实战
  • OpenCL study - code03 rgb2gray
  • 通过硬编码函数地址并转换为函数指针来调用函数
  • 6.Pinia快速入门