• Diff for "nodelet/Tutorials/Porting nodes to nodelets"
Differences between revisions 8 and 25 (spanning 17 versions)
Revision 8 as of 2011-01-11 02:25:53
Size: 2726
Editor: TullyFoote
Comment:
Revision 25 as of 2018-11-06 11:43:32
Size: 3567
Editor: Markus Bader
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
## note = [[pluginlib/Tutorials/Writing and Using a Simple Plugin]]
## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links 
## note.0= 
## note = This tutorial assumes that you have completed the previous tutorials:[[nodelet/Tutorials/Running a nodelet]]
## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links
## note.0=
Line 12: Line 12:
## multi-line description to be displayed in search 
## description = 
## multi-line description to be displayed in search
## description =
Line 19: Line 19:
## what level user is this tutorial for  ## what level user is this tutorial for
Line 23: Line 23:
Line 34: Line 33:
 * add the PLUGINLIB_DECLARE_CLASS macro  * add the PLUGINLIB_EXPORT_CLASS macro
 * add <build_depend> and <run_depend> dependencies on nodelet in the package manifest.
Line 39: Line 39:
== Minimal Nodelet ==
!MyNodeletClass.h
Line 40: Line 42:
== Minimal Nodelet (may not be correct) == Here we define the Class of our Nodelet. It's a good practice to place the header file under include/''package_name''/.
Line 42: Line 44:
!MyNodelet.h ''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''
Line 46: Line 49:
namespace nodelets namespace example_pkg
Line 49: Line 52:
    class MyNodlet : public nodelet::Nodelet     class MyNodeletClass : public nodelet::Nodelet
Line 57: Line 60:
!MyNodeletClass.cpp
Line 58: Line 62:
!MyNodelet.cpp This file should be placed under the src/ folder of your actual package
Line 63: Line 68:
PLUGINLIB_DECLARE_CLASS(nodelets, my_nodelet, nodelets::MyNodlet, nodelet::Nodelet) // Include your header
#include <example_pkg/MyNodeletClass.h>
Line 65: Line 71:
namespace nodelets // watch the capitalization carefully
PLUGINLIB_EXPORT_CLASS(example_pkg::MyNodeletClass, nodelet::Nodelet)

namespace example_pkg
Line 67: Line 76:
    MyNodelet::onInit()      void MyNodeletClass::onInit()
Line 72: Line 81:
}}}
nodelet_plugins.xml
Line 73: Line 84:
}}} This file should be placed along with the package.xml file
Line 75: Line 86:

nodelet_plugins.xml
Line 78: Line 87:
<library path="lib/libmynodelet">
  <class name="nodelets/my_nodelet" type="nodelets::MyNodlet" base_class_type="nodelet::Nodelet">
<library path="lib/libMyNodeletClass">
  <class name="example_pkg/MyNodeletClass" type="example_pkg::MyNodeletClass" base_class_type="nodelet::Nodelet">
Line 81: Line 90:
  This is an example nodelet   This is my nodelet.
Line 86: Line 95:
package.xml
Line 87: Line 97:
manifest.xml
Line 90: Line 99:
<build_depend>nodelet</build_depend>
<run_depend>nodelet</run_depend>
Line 91: Line 102:
  <nodelet plugin="${prefix}/nodlet_plugins.xml" />   <nodelet plugin="${prefix}/nodelet_plugins.xml" />
Line 95: Line 106:
mynodelet.launch
Line 96: Line 108:
mynodelet.launch Launch file should be placed under the launch/ repertory of your package, you can create it if it does not exist.
Line 101: Line 114:
  <node pkg="nodelet" type="nodelet" name="MyNodelet" args="load nodelets/my_nodelet standalone_nodelet" output="screen">
  </node>    
  <node pkg="nodelet" type="nodelet" name="MyNodeletClass" args="load example_pkg/MyNodeletClass standalone_nodelet" output="screen">
  </node>
Line 104: Line 117:
Line 106: Line 118:

## AUTOGENERATED DO NOT DELETE 
## AUTOGENERATED DO NOT DELETE

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

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>

Wiki: nodelet/Tutorials/Porting nodes to nodelets (last edited 2018-11-06 11:43:32 by Markus Bader)