将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 <pluginlib/class_list_macros.h>

#include <nodelet/nodelet.h>

 

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

#include <nodelet/nodelet.h>

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<boost::thread> 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添加<build_depend> 和 <run_depend>依赖项。

<run_depend>nodelet</run_depend>

<build_depend>nodelet</build_depend>

 

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

 

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

<library path="lib/libmobile_base">
<class name="example_ns/MobileBase" type="example_ns::MobileBase" base_class_type="nodelet::Nodelet">
<description>
mobile_base nede ,communicate with the robot.
</description>
</class>
</library>

 

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

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

<export>
<nodelet plugin="${prefix}/nodelet_plugin .xml"/>
</export>

 

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

<?xml version="1.0"?>
<!-- launch base node and publish all tf -->

<launch>
    <!--node:nodelet manager launch-->
    <node pkg="nodelet" type="nodelet" name="standalone_nodelet" args="manager" output="screen"/>

    <node name="mobile_base" pkg="nodelet" type="nodelet" output="screen" args="load example_ns/MobileBase standalone_nodelet" >
    </node>
</launch>

 

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值