ROS之nodelet

一、初见

1.对nodelet的理解:

ros节点基于TCP/IP通信,数据传输时会有打包和解包的过程,当数据量大或对实时性要求比较高的时候,这个时间就必须考虑。nodelet提供了一种方法,可以在同一个进程中运行多个节点,实现零复制通信。

2.使用及注意事项:

  • 1.头文件:
#ifndef H
#define H
#include <nodelet/nodelet.h>
namespace nodel
{
    class nodel : public nodelet::Nodelet
    {
        public:
            virtual void onInit();
            ros::NodeHandle n_;
    };
}
#endif
  • 2.源文件:
#include <ros/ros.h>
#include <pluginlib/class_list_macros.h>
#include <nodel.h>


namespace nodel
{
    void nodel::onInit()
    {
        NODELET_DEBUG("Initializing nodelet...");
        n_ = this->getNodeHandle();
        ROS_INFO("Nodelet is Ok for test!!");
    }
}

// watch the capitalization carefully
PLUGINLIB_EXPORT_CLASS(nodel, nodel, nodel::nodel, nodelet::Nodelet)

需要 #include <pluginlib/class_list_macros.h>,在最后注册插件类,PLUGINLIB_EXPORT_CLASS(命名空间::插件类, nodelet::Nodelet)。

  • 3.plugin.xml
<library path="lib/libexample">
  <class name="DIY" type="namespace::plugin_class" base_class_type="nodelet::Nodelet">
  <!-- plugin_name is named by self -->
  <description>
  This is my nodelet.
  </description>
  </class>
</library>
  • 4.package.xml
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <exec_depend>roscpp</exec_depend>
    
  <build_depend>nodelet</build_depend>
  <build_export_depend>nodelet</build_export_depend>
  <exec_depend>nodelet</exec_depend>

  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->
    <nodelet plugin="${prefix}/plugins/nodel_plugins.xml" />
    <nodelet plugin="${prefix}/plugins/sub_nodelet.xml" />
  </export>
  • 5.launch
<launch>
  <node pkg="nodelet" type="nodelet" name="ZHUYI"  args="manager" output="screen"/>
  <node pkg="nodelet" type="nodelet" name="random" args="load (same as class name in plugin.xml) ZHUYI" output="screen">
  </node>                 
</launch>

3.参考

雷一鸣的博客

二、问题

  • 1.使用ros::spin()会导致bond无法发布,使用rqt会看到节点都是独立的,没有交互。
  • 2.注意发布变量和接收变量的生命周期,不要在onInit()中声明,最好作为成员变量或全局变量。
  • 3.onInit里不要写没有明确结束的循环,如while(ros::ok()),应该重开个线程,使用detach方法。否则会导致onInit不能返回,对应nodelet不能卸载,再次运行程序出现迷之错误。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值