Note: This tutorial assumes that you have completed the previous tutorials:nodelet/Tutorials/Running a nodelet. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Porting nodes to nodelets
Description:Tutorial Level: BEGINNER
Contents
Work in progress...(see nodelet_tutorial_math for an example)
- add the necessary #includes
- get rid of int main()
- subclass nodelet::Nodelet
- move code from constructor to onInit()
- add the PLUGINLIB_EXPORT_CLASS macro
add <build_depend> and <run_depend> dependencies on nodelet in the package manifest.
add the <nodelet> item in the <export> part of the package manifest
- create the .xml file to define the nodelet as a plugin
- make the necessary changes to CMakeLists.txt (comment out a rosbuild_add_executable, add a rosbuild_add_library)
Minimal Nodelet
MyNodeletClass.h
Here we define the Class of our Nodelet. It's a good practice to place the header file under include/package_name/.
NB: The usage of the namespace is a good practice but not mandatory, in the following example we will proceed with the namespace example_pkg
#include <nodelet/nodelet.h> namespace example_pkg { class MyNodeletClass : public nodelet::Nodelet { public: virtual void onInit(); }; }
MyNodeletClass.cpp
This file should be placed under the src/ folder of your actual package
// this should really be in the implementation (.cpp file) #include <pluginlib/class_list_macros.h> // Include your header #include <example_pkg/MyNodeletClass.h> // watch the capitalization carefully PLUGINLIB_EXPORT_CLASS(example_pkg::MyNodeletClass, nodelet::Nodelet) namespace example_pkg { void MyNodeletClass::onInit() { NODELET_DEBUG("Initializing nodelet..."); } }
nodelet_plugins.xml
This file should be placed along with the package.xml file
<library path="lib/libMyNodeletClass"> <class name="example_pkg/MyNodeletClass" type="example_pkg::MyNodeletClass" base_class_type="nodelet::Nodelet"> <description> This is my nodelet. </description> </class> </library>
package.xml
... <build_depend>nodelet</build_depend> <run_depend>nodelet</run_depend> <export> <nodelet plugin="${prefix}/nodelet_plugins.xml" /> </export> ...
mynodelet.launch
Launch file should be placed under the launch/ repertory of your package, you can create it if it does not exist.
<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>