一、初见
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不能卸载,再次运行程序出现迷之错误。