本文主要参考下列三篇博客
ROS Nodelet的使用
ROS nodelst初探
ROS中的nodelet
1.简介
nodelet官方教程:http://wiki.ros.org/nodelet
中文教程:http://www.ncnynl.com/archives/201702/1324.html
nodelet是为了减少ros node之间的消息传输(进程间通讯), 将多个算法"结点(nodelet)"跑到一个进程中, 这样避免了数据传输, (因为进程内部内存是共享的, 传数据的话传个指针就行了), 从而实现的零拷贝(zero copy).
nodelet和node很大程度上保持一致, 对外来说, nodelet几乎可以视为node, nodelet之间仍然是消息传输(只是内部实现由原始数据的传输改成了指针传输), 将node转换为nodelet只需要少量的改动.
2.编写Nodelet
2.1创建pkg
catkin_create_pkg sample_nodelet nodelet roscpp rospy
注意要依赖nodelet包
2.2编写.h文件和cpp文件
2.2.1 在.h文件中创建子类的写法
.h文件
在.h文件中创建一个子类(注:.h文件中不创建该子类而是直接在cpp文件中编写应该也可以,具体见2.2.2), 该子类继承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文件
在cpp文件中实现onInit()函数,该函数相当于main()函数,在onInit()中编写自己的代码即可
需要注意的是,在cpp文件中需要添加如下两行,这两行也可以写在类的后面。
#include <pluginlib/class_list_macros.h>
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!!");
}
}
/*这两行写在这里也可以
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(example_pkg::SampleNodelet, nodelet::Nodelet)
*/
2.2.2 直接在cpp文件中编写的写法及ROS话题编程示例
原理同上,在cpp文件中创建一个子类, 该子类继承nodelet::Nodelet, 最好在类的外面加一个namespace, 一般以package名字做namespace. 类中有一个onInit()函数, 相当于main()函数。一般将onInit()函数放在
namespace pkg_name
{
class SampleNodelet:: public nodelet::Nodelet{
private:
std::thread initThread_;
...回调函数、变量等都放在这里
void init(ros::NodeHandle& nh){
初始化函数放在这里,如nh.getParam,nh.subscribe,nh.publish等
}
public:
void onInit(void){
ros::NodeHandle nh(getMTPrivateNodeHandle());
initThread_ = std::thread(std::bind(&SampleNodelet::init,this,nh));
}
}
}
#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(pkg_name::SampleNodelet, nodelet::Nodelet);
2.3 CMakeList
nodelet的pkg中是没有main函数的,所以不能生成可执行程序,而是要生成一个lib供ros调用
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(project_name)
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})
2.4 nodelet_plugins.xml
nodelet_plugins.xml是nodelet特有的, 文件名可以自己定, 最好用这个名字. xml文件里面指定nodelet的lib的路径, 以及nodelet类的名字.
<library path="lib/liblib_name">
<class name="pkg_name/SampleNodelet" type="pkg_name::SampleNodelet" base_class_type="nodelet::Nodelet">
<description>
This is my nodelet.
</description>
</class>
</library>
2.5 package.xml
package.xml中增加export项, 把上一个xml文件的位置配置进去.
<?xml version="1.0"?>
<package>
<name>pkg_name</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>
3.nodelet基本操作
nodelet manager //创建mananger
nodelet load pkg/Type manager_name //向manager中loader nodelet 这里的type是新建的nodelet::Nodelet的子类的名字
nodelet standalone pkg/Type //程序复用,相当启动一个普通node
nodelet unload name manager //从manager移除nodelet
4.nodelet运行
4.1 rosrun运行
roscore
rosrun nodelet nodelet manager __name:=nodelet_manager
rosrun nodelet nodelet load nodelet_tutorial_math/Plus nodelet_manager __name:=nodelet1 nodelet1/in:=foo _value:=1.1
4.2 roslaunch运行
<launch>
<node pkg="nodelet" type="nodelet" name="standalone_nodelet"
args="manager"/>
<node pkg="nodelet" type="nodelet" name="Plus"
args="load nodelet_tutorial_math/Plus standalone_nodelet">
<remap from="/Plus/out" to="Plus2/in"/>
</node>
<rosparam param="Plus2" file="$(find nodelet_tutorial_math)/plus_default.yaml"/>
<node pkg="nodelet" type="nodelet" name="Plus2"
args="load nodelet_tutorial_math/Plus standalone_nodelet">
<rosparam file="$(find nodelet_tutorial_math)/plus_default.yaml"/>
</node>
<node pkg="nodelet" type="nodelet" name="Plus3"
args="standalone nodelet_tutorial_math/Plus">
<param name="value" type="double" value="2.5"/>
<remap from="Plus3/in" to="Plus2/out"/>
</node>
</launch>
第一个node启动了nodelet manager;
第二个node向manager加载了一个nodelet,名为plus,并将输出topic重新映射为plus2/in;
第三个node向manager加载了一个nodelet,名为plus2,用文件plus_default.yaml定义了value=10,接收plus的输出作为输入;
第四个node启动一个独立的nodelet,名为plus3,定义value,接收plus2的输出作为输入;
最终,实现了plus,plus2,plus3的叠加。
5.常用API
5.1 公共方法
//动态加载时使用的默认构造函数
Nodelet()
//参数是管理器启动节点所需的参数。这将初始化nodelet基类,而后调用子类的onInit()方法。
void init (const std::string& name, const ros::M_string& remapping_args, const std::vector<std::string>& my_argv);
5.2 子类中使用的受保护成员和方法
std::string getName() //获取节点名称
ros::NodeHandle& getNodeHandle () // 获取节点句柄
ros::NodeHandle& getPrivateNodeHandle () //获取私有节点句柄(在其私有名称空间中提供此nodelets自定义重映射)
ros::NodeHandle& getMTNodeHandle () // 使用Multi Threaded获取节点句柄
ros::NodeHandle& getMTPrivateNodeHandle () //使用Multi Threaded回调队列获取私有节点句柄。 (在其私有命名空间中提供此nodelets自定义重映射)
ros::CallbackQueue& getMTCallbackQueue () // 获取回调队列(可从管理器获取线程池)。
std::vector<std::string> getMyArgv() //获取分离了ROS和nodelet特定args的nodelet命令行参数。
5.3 子类中使用的受保护成员和方法
virtual void onInit () = 0 //Virtual and must be overridden by subclass. All initialization of the ROS infrastructure must be put into this function.