1. 簡介
nodelet是為了減少ros node之間的消息傳輸(進程間通訊), 將多個算法"結點(nodelet)"跑到一個進程中, 這樣避免了數據傳輸, (因為進程內部內存是共享的, 傳數據的話傳個指針就行了), 從而實現的零拷貝(zero copy).
nodelet和node很大程度上保持一致, 對外來說, nodelet幾乎可以視為node, nodelet之間仍然是消息傳輸(只是內部實現由原始數據的傳輸改成了指針傳輸), 將node轉換為nodelet只需要少量的改動.
2. 編寫nodelet
nodelet的pkg組織架構和node有點相似, 包括: include, src, package.xml, CMakeLists.txt, 和 nodelet_plugins.xml.
- 創(chuàng)建pkg: catkin_create_pkg sample_nodelet nodelet roscpp rospy. 注意要依賴nodelet包.
- 在include, src中創(chuàng)建一個類, 該類繼承nodelet::Nodelet, 最好在類的外面加一個namespace, 可以以package名字做namespace. 類中有一個onInit()函數, 相當于main()函數.
//sample_nodelet.h
#ifndef __SAMPLE_NODELET_H__
#define __SAMPLE_NODELET_H__
#include <nodelet/nodelet.h>
namespace example_pkg
{
class SampleNodelet: public nodelet::Nodelet
{
public:
virtual void onInit();
};
}
#endif //__SAMPLE_NODELET_H__
- 在cpp中實現onInit()函數, 編寫自己的代碼. 有個特別的地方是 cpp中還需要加一行
- PLUGINLIB_EXPORT_CLASS(example_pkg::SampleNodelet, nodelet::Nodelet)
//sample_nodelet.cpp
#include <pluginlib/class_list_macros.h>
#include <ros/ros.h>
#include <sample_nodelet.h>
PLUGINLIB_EXPORT_CLASS(example_pkg::SampleNodelet, nodelet::Nodelet)
namespace example_pkg
{
void SampleNodelet::onInit()
{
NODELET_DEBUG("Initializing nodelet...");
ROS_INFO("Nodelet is Ok for test!!");
}
}
- CMakelist中添加library. 這里我們發(fā)現, nodelet的pkg中沒有main()沒有可執(zhí)行程序, 而是生成一個lib, 讓ros調用.
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(sample_nodelet)
find_package(catkin REQUIRED COMPONENTS
nodelet
roscpp
rospy
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS nodelet roscpp rospy
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_library(${PROJECT_NAME} src/sample_nodelet.cpp)
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
- nodelet_plugins.xml是nodelet特有的, 文件名可以自己定, 最好用這個名字. xml文件里面指定nodelet的lib的路徑, 以及nodelet類的名字.
<library path="lib/libsample_nodelet">
<class name="example_pkg/SampleNodelet" type="example_pkg::SampleNodelet" base_class_type="nodelet::Nodelet">
<description>
This is my nodelet.
</description>
</class>
</library>
- package.xml中增加export項, 把上一個xml文件的位置配置進去.
<?xml version="1.0"?>
<package>
<name>sample_nodelet</name>
<version>0.0.0</version>
<description>The sample_nodelet package</description>
<maintainer email="symao@todo.todo">symao</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>nodelet</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<run_depend>nodelet</run_depend>
<run_depend>roscpp</run_depend>
<run_depend>rospy</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<nodelet plugin="${prefix}/nodelet_plugins.xml" />
</export>
</package>
至此, nodelet pkg的編寫就基本完成. 可以用catkin_make直接編譯
3. 運行nodelet
3.1 以launch文件運行
編寫launch文件, 如下. 該launch打開兩個nodelet, 更改第二個nodelet的name和args.
<launch>
<node pkg="nodelet" type="nodelet" name="standalone_nodelet" args="manager" output="screen"/>
<node pkg="nodelet" type="nodelet" name="SampleNodelet" args="load example_pkg/SampleNodelet standalone_nodelet" output="screen">
</node>
</launch>
3.2 命令行運行
#啟動nodelet管理器
roscore
rosrun nodelet nodelet manager __name:=nodelet_manager
#啟動自己的nodelet
rosrun nodelet nodelet load nodelet_tutorial_math/Plus nodelet_manager __name:=nodelet1 nodelet1/in:=foo _value:=1.1
這里(http://www.ncnynl.com/archives/201702/1325.html)有一個簡單的實例, 可以看一下.