ROS之nodelet的使用

xiaoxiao2021-02-27  326

1 nodelet插件编写和使用流程介绍

ROS nodelet插件和ROS中普通插件编写流程一样,因为它们都是借助plubinlib来实现插件动态加载,都必须follow pluginlib使用规则,因此编写一个nodelet插件大致流程如下:

1.1编写nodelet插件class

由于插件基类是nodelet::NodeLet已经存在,所以不必编写啦。

MyNodeletClass.h

[cpp]  view plain  copy  print ? #include <nodelet/nodelet.h>      namespace example_pkg   {          class MyNodeletClass : public nodelet::Nodelet       {           public:               virtual void onInit();       };      }  

1.2 导出nodelet插件

MyNodeletClass.cpp

[cpp]  view plain  copy  print ? // this should really be in the implementation (.cpp file)   #include <pluginlib/class_list_macros.h>   #include <example_pkg/MyNodeletClass.h>      namespace example_pkg   {       void MyNodeletClass::onInit()        {           NODELET_DEBUG("Initializing nodelet...");       }   }      // watch the capitalization carefully   PLUGINLIB_EXPORT_CLASS(example_pkg::MyNodeletClass, nodelet::Nodelet)   

1.3 接入到ROS系统使用插件 1)编写插件描述符

MyNodeletClass.xml

[cpp]  view plain  copy  print ? <library path="lib/libMyNodeletClass">     <class name="example_pkg/MyNodeletClass" type="example_pkg::MyNodeletClass" base_class_type="nodelet::Nodelet">     <description>     This is my nodelet.     </description>     </class>   </library>   2)导出插件包到ROS系统 package.xml

[cpp]  view plain  copy  print ? ...   <build_depend>nodelet</build_depend>   <run_depend>nodelet</run_depend>   <export>     <nodelet plugin="${prefix}/nodelet_plugins.xml" />   </export>   ...   3)编译插件 主要是CMakeLists.txt一些修改工作 4)nodelet启动文件

[cpp]  view plain  copy  print ? <launch>     <!--node:nodelet manager launch-->     <node pkg="nodelet" type="nodelet" name="standalone_nodelet"  args="manager" output="screen"/>          <!--node:my nodelet launch -->     <node pkg="nodelet" type="nodelet" name="MyNodeletClass" args="load example_pkg/MyNodeletClass standalone_nodelet" output="screen">     </node>                    </launch>   2 实例分析 分析ROS已经提供的插件nodelet_tutorial_math: 2.1 编写并导出插件nodelet_tutorial_math 常规做法是分成2步,第一步编写插件,放到.h文件,第二步导出插件,放到.cpp文件;这里合并到.cpp一起完成如下。 nodelet插件主要编写的就是函数OnInit(继承自Nodelet),做一些ROS node基本初始化。

[cpp]  view plain  copy  print ? #include <pluginlib/class_list_macros.h>   #include <nodelet/nodelet.h>   #include <ros/ros.h>   #include <std_msgs/Float64.h>   #include <stdio.h>         #include <math.h> //fabs      namespace test_nodelet   {      class Plus : public nodelet::Nodelet   {   public:     Plus()     : value_(0)     {}      private:     virtual void onInit()     {       ros::NodeHandle& private_nh = getPrivateNodeHandle();       private_nh.getParam("value", value_);       pub = private_nh.advertise<std_msgs::Float64>("out", 10);       sub = private_nh.subscribe("in", 10, &Plus::callback, this);     }        void callback(const std_msgs::Float64::ConstPtr& input)     {       std_msgs::Float64Ptr output(new std_msgs::Float64());       output->data = input->data + value_;       NODELET_DEBUG("Adding %f to get %f", value_, output->data);       pub.publish(output);     }        ros::Publisher pub;     ros::Subscriber sub;     double value_;   };      PLUGINLIB_DECLARE_CLASS(test_nodelet, Plus, test_nodelet::Plus, nodelet::Nodelet);   }  

2.2 插件接入到ROS系统使用 1)编写插件描述符 nodelet_math.xml

2)导出插件到ROS系统 package.xml

3)编写插件启动文件 plus.launch 启动后将会看到如下节点:

4)编译插件 CMakeLists.txt

转载请注明原文地址: https://www.6miu.com/read-2799.html

最新回复(0)