将ros节点改为nodelet格式的详细步骤

目录

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文件


1、参考链接:

 

  • 1、http://wiki.ros.org/nodelet
  • 2、https://blog.csdn.net/zxqhbd/article/details/71169522
  • 3、https://www.ncnynl.com/archives/201702/1326.html

 

2、Nodelet简介

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.

  • 1、nodelet包被设计为在同一个机器,同一个进程中可以零拷贝的运行多个算法。
  • 2、nodelet允许动态加载不同的算法类到同一个节点中,并且每一个类都有独立的命名空间。
  • 3、ROS nodelet插件和ROS中普通插件编写流程一样,因为它们都是借助plubinlib来实现插件动态加载,都必须follow pluginlib使用规则.

3、修改nodelet节点

 

1. 添加必要的头文件

在自己算法的主类(主要算法实现的功能类例如:clase MobileBase)头文件中添加nodelet必要的头文件:

#include 

#include 

 

2. 添加nodelet的继承,使自己的主类继承于Nodelet

#include 

namespace example_ns
{
    class MobileBase : public nodelet::Nodelet
    {
        public:
            virtual void onInit();
    };
}

3. 主类MobileBase 中添加virtual void onInit ()函数

将构造函数中初始化的内容移动到 onInit ()

nedelet方式动态加载的节点会自动调用onInit()。nodelet父类会调用子类的onInit函数初始化子类。

 

4. 去掉 main ()函数

如果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)));

 

5. 添加PLUGINLIB_EXPORT_CLASS宏或PLUGINLIB_DECLARE_CLASS宏:导出nodelet插件

PLUGINLIB_EXPORT_CLASS(example_ns::MobileBase, nodelet::Nodelet)
或者
PLUGINLIB_DECLARE_CLASS(example, MobileBase, example_ns::MobileBase, nodelet::Nodelet)

6. 在包manifest.xml或package.xml中对nodelet添加依赖项。

nodelet

nodelet

 

7. 编写插件描述符:创建 nodelet_plugin.xml 文件以将 nodelet 定义为插件

 

mobile_base :为CmakeLists.txtproject定义的名字:




mobile_base nede ,communicate with the robot.


 

8. 请在包清单package.xml的 部分中添加 项,导出插件包到ROS系统

输入的参数为上面创建的插件描述符。



 

9. 对 CMakeLists.txt 进行必要的更改(注释掉一个 add_executable ,添加一个add_library )

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)

10、编写launch文件





    
    

    
    

 

你可能感兴趣的:(技术资料,ROS,nodelet,ros,nodelet)