目录
1、参考链接:
2、Nodelet简介
3、修改nodelet节点
1. 添加必要的头文件
2. 添加nodelet的继承,使自己的主类继承于Nodelet
3. 主类MobileBase 中添加virtual void onInit ()函数
4. 去掉 main ()函数
5. 添加PLUGINLIB_EXPORT_CLASS宏或PLUGINLIB_DECLARE_CLASS宏:导出nodelet插件
6. 在包manifest.xml或package.xml中对nodelet添加 和 依赖项。
7. 编写插件描述符:创建 nodelet_plugin.xml 文件以将 nodelet 定义为插件
8. 请在包清单package.xml的 部分中添加 项,导出插件包到ROS系统
9. 对 CMakeLists.txt 进行必要的更改(注释掉一个 add_executable ,添加一个add_library )
10、编写launch文件
Nodelets are designed to provide a way to run multiple algorithms on a single machine, in a single process, without incurring copy costs when passing messages intraprocess. roscpp has optimizations to do zero copy pointer passing between publish and subscribe calls within the same node. To do this nodelets allow dynamic loading of classes into the same node, however they provide simple separate namespaces such that the nodelet acts like a seperate node, despite being in the same process. This has been extended further in that it is dynamically loadable at runtime using pluginlib.
在自己算法的主类(主要算法实现的功能类例如:clase MobileBase)头文件中添加nodelet必要的头文件:
#include
#include
#include
namespace example_ns
{
class MobileBase : public nodelet::Nodelet
{
public:
virtual void onInit();
};
}
将构造函数中初始化的内容移动到 onInit ()。
nedelet方式动态加载的节点会自动调用onInit()。nodelet父类会调用子类的onInit函数初始化子类。
如果main函数中存在spin类型的需要循环处理的函数,在新建一个进程处理。
新建spin函数,用于处理循环处理的数据:
void MobileBase::spin()
{
ros::Rate r(100.0);
while(ros::ok())
{
//循环处理的数据
}
}
在MbileBase类中定义线程指针:
boost::shared_ptr spinThread_;
在onInit中新建进程:
spinThread_ = boost::shared_ptr< boost::thread >
(new boost::thread(boost::bind(&MobileBase::spin, this)));
PLUGINLIB_EXPORT_CLASS(example_ns::MobileBase, nodelet::Nodelet)
或者
PLUGINLIB_DECLARE_CLASS(example, MobileBase, example_ns::MobileBase, nodelet::Nodelet)
nodelet
nodelet
mobile_base :为CmakeLists.txtproject定义的名字:
mobile_base nede ,communicate with the robot.
输入的参数为上面创建的插件描述符。
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
***
nodelet
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS roscpp rospy std_msgs nodelet
)
## Declare a C++ executable
#add_executable( mobile_base src/main.cpp src/MobileBase.cpp src/embedded_protocol.cpp src/qserial.cpp)
## Declare a C++ library
add_library(${PROJECT_NAME}
src/MobileBase.cpp src/embedded_protocol.cpp src/qserial.cpp)
## Add cmake target dependencies of the executable
## same as for the library above
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS} )
## Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} state_handle)