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.
- 创建pkg: catkin_create_pkg sample_nodelet nodelet roscpp rospy. 注意要依赖nodelet包.
- 在include, src中创建一个类, 该类继承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. 这里我们发现, nodelet的pkg中没有main()没有可执行程序, 而是生成一个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)有一个简单的实例, 可以看一下.
网友评论