ROS与C++入门教程-nodelet-Node封装为Nodelet
ROS与C++入门教程-nodelet-Node封装为Nodelet
说明:
- 介绍如何将节点封装为Nodelet
处理过程
- 查阅nodelet_tutorial_math例子
- 添加必要的#includes
- 去掉int main()
- 继承子类 nodelet::Nodelet
- 将代码从构造函数移动到onInit()
- 添加PLUGINLIB_EXPORT_CLASS宏或PLUGINLIB_DECLARE_CLASS宏
- 在包manifest.xml或package.xml中对nodelet添加<build_depend> 和 <run_depend>依赖项。
- 请在包manifest.xml或package.xml
部分中添加 项 - 创建.xml文件以将nodelet定义为插件
- 对CMakeLists.txt进行必要的更改(注释掉一个rosbuild_add_executable,添加一个rosbuild_add_library)
代码库:
- https://github.com/ncnynl/example_pkg
最小的Nodelet
- 创建包example_pkg
cd ~/catkin_ws/src
catkin_create_pkg example_pkg
- 创建MyNodeletClass.h文件
cd ~/catkin_ws/src/example_pkg/
mkdir -p include/example_pkg
touch include/example_pkg/MyNodeletClass.h
vim include/example_pkg/MyNodeletClass.h
- 代码:
#include <nodelet/nodelet.h>
namespace example_pkg
{
class MyNodeletClass : public nodelet::Nodelet
{
public:
virtual void onInit();
};
}
- 创建MyNodeletClass.cpp文件
cd ~/catkin_ws/src/example_pkg/
mkdir src
touch src/MyNodeletClass.cpp
vim src/MyNodeletClass.cpp
- 代码:
// this should really be in the implementation (.cpp file)
#include <ros/ros.h>
#include <pluginlib/class_list_macros.h>
#include <example_pkg/MyNodeletClass.h>
namespace example_pkg
{
void MyNodeletClass::onInit()
{
NODELET_DEBUG("Initializing nodelet...");
ROS_INFO("Nodelet is Ok for test!!");
}
}
// watch the capitalization carefully
PLUGINLIB_DECLARE_CLASS(example_pkg, MyNodeletClass, example_pkg::MyNodeletClass, nodelet::Nodelet)
成功则输出"Nodelet is Ok for test!!"
创建nodelet_plugins.xml文件
cd ~/catkin_ws/src/example_pkg/
mkdir plugins
touch plugins/nodelet_plugins.xml
vim plugins/nodelet_plugins.xml
- 代码:
<library path="lib/libexample_pkg">
<class name="example_pkg/MyNodeletClass" type="example_pkg::MyNodeletClass" base_class_type="nodelet::Nodelet">
<description>
This is my nodelet.
</description>
</class>
</library>
- 修改package.xml文件,增加:
cd ~/catkin_ws/src/example_pkg/
vim package.xml
- 代码:
<buildtool_depend>catkin</buildtool_depend>
<build_depend>nodelet</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>nodelet</run_depend>
<run_depend>roscpp</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
<nodelet plugin="${prefix}/plugins/nodelet_plugins.xml" />
</export>
- 修改CMakeLists.txt
cd ~/catkin_ws/src/example_pkg
vim CMakeLists.txt
- 内容:
cmake_minimum_required(VERSION 2.8.3)
project(example_pkg)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS nodelet roscpp)
find_package(Boost REQUIRED)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS nodelet roscpp
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
## Declare a C++ library
add_library(${PROJECT_NAME} src/MyNodeletClass.cpp)
add_dependencies(${PROJECT_NAME}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
- 创建mynodelet.launch文件
cd ~/catkin_ws/src/example_pkg/
mkdir launch
touch launch/mynodelet.launch
vim launch/mynodelet.launch
- 代码:
<launch>
<node pkg="nodelet" type="nodelet" name="standalone_nodelet" args="manager" output="screen"/>
<node pkg="nodelet" type="nodelet" name="MyNodeletClass" args="load example_pkg/MyNodeletClass standalone_nodelet" output="screen">
</node>
</launch>
- 编译:
cd ~/catkin_ws/
catkin_make
rospack profile
测试:
- 新终端,运行roscore
$ roscore
- 新终端,运行launch
$ rosluanch examples_pkg mynodelet.launch
- 效果:
SUMMARY
========
PARAMETERS
* /rosdistro: indigo
* /rosversion: 1.11.16
NODES
/
MyNodeletClass (nodelet/nodelet)
standalone_nodelet (nodelet/nodelet)
ROS_MASTER_URI=http://192.168.0.88:11311
core service [/rosout] found
process[standalone_nodelet-1]: started with pid [23420]
process[MyNodeletClass-2]: started with pid [23421]
[ INFO] [1488697612.246845992]: Loading nodelet /MyNodeletClass of type example_pkg/MyNodeletClass to manager standalone_nodelet with the following remappings:
[ INFO] [1488697612.289895137]: waitForService: Service [/standalone_nodelet/load_nodelet] has not been advertised, waiting...
[ INFO] [1488697612.521288394]: Initializing nodelet with 1 worker threads.
[ INFO] [1488697612.553078141]: waitForService: Service [/standalone_nodelet/load_nodelet] is now available.
[ INFO] [1488697612.831934104]: Nodelet is Ok for test!!
[ INFO] [1488697626.013265094]: Bond broken, exiting
[MyNodeletClass-2] process has finished cleanly
log file: /home/ubu/.ros/log/e1cb7ac4-0156-11e7-9599-1002b594fceb/MyNodeletClass-2*.log
- 输出[ INFO] [1488697612.831934104]: Nodelet is Ok for test!!
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号