[Documentation] [TitleIndex] [WordIndex

(!) 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.

Implementing a simple filter

Description: This will describe how to implement a simple filter. This filter will simply add 1 to any incoming value. This filter exists in the filter package. The version for this tutorial has been isolated from other example files in the filters package.

Keywords: filters

Tutorial Level: BEGINNER

Next Tutorial: How to use filters with respect to laser data laser_filters/Tutorials/Laser filtering using the filter nodes laser_filters/Tutorials/Laser filtering in C++

Process

Create a Class Definition

An example prototype can be seen in include/filters/increment.h

  11 class IncrementFilter: public FilterBase <int>
  12 {
  13 public:
  14   IncrementFilter();
  15   ~IncrementFilter();
  16   virtual bool configure();
  17   virtual bool update( const T & data_in, T& data_out);
  18 };

Implement the Class

   4 IncrementFilter::IncrementFilter()
   5 {}
   6 
   7 bool IncrementFilter::configure()
   8 {  return true;}
   9 
  10 template <typename T>
  11 IncrementFilter::~IncrementFilter()
  12 {}
  13 
  14 bool IncrementFilter::update(const int & data_in, int& data_out)
  15 {
  16   data_out = data_in + 1;
  17   return true;
  18 };

  20 PLUGINLIB_REGISTER_CLASS(IncrementFilterInt, filters::IncrementFilter<int>, filters::FilterBase<int>)

Add a plugin description file

  <library path="lib/libincrement">
    <class name="IncrementFilterInt" type="filters::IncrementFilter<int>"
            base_class_type="filters::FilterBase<int>">
      <description>
        This is a increment filter which works on a stream of ints.
      </description>
    </class>
  </library>

Add the library to CMakeLists.txt

rosbuild_add_library(increment src/increment.cpp)

Export the description file

  15   <filters plugin="${prefix}/plugin_description_file.xml" />

Files

include/filters/increment.h

   1 #ifndef FILTERS_INCREMENT_H
   2 #define FILTERS_INCREMENT_H
   3 #include <stdint.h>
   4 #include <cstring>
   5 #include <stdio.h>
   6 #include <boost/scoped_ptr.hpp>
   7 #include "filters/filter_base.h"
   8 
   9 namespace filters {
  10 
  11 class IncrementFilter: public FilterBase <int>
  12 {
  13 public:
  14   IncrementFilter();
  15   ~IncrementFilter();
  16   virtual bool configure();
  17   virtual bool update( const T & data_in, T& data_out);
  18 };
  19 
  20 }
  21 
  22 #endif
  23 

src/increment.cpp

   1 #include "filters/increment.h"
   2 #include "pluginlib/class_list_macros.h"
   3 using namespace filters;
   4 IncrementFilter::IncrementFilter()
   5 {}
   6 
   7 bool IncrementFilter::configure()
   8 {  return true;}
   9 
  10 template <typename T>
  11 IncrementFilter::~IncrementFilter()
  12 {}
  13 
  14 bool IncrementFilter::update(const int & data_in, int& data_out)
  15 {
  16   data_out = data_in + 1;
  17   return true;
  18 };
  19 
  20 PLUGINLIB_REGISTER_CLASS(IncrementFilterInt, filters::IncrementFilter<int>, filters::FilterBase<int>)

plugin_description_file.xml

<class_libraries>
  <library path="lib/libincrement">
    <class name="IncrementFilterInt" type="filters::IncrementFilter<int>"
            base_class_type="filters::FilterBase<int>">
      <description>
        This is a increment filter which works on a stream of ints.
      </description>
    </class>
  </library>
</class_libraries>

CMakeLists.txt

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()

rosbuild_add_boost_directories()

#Plugins                                                                                                                                                     
rosbuild_add_library(increment src/increment.cpp)

manifest.xml

   1 <package>
   2 <description brief='A standardized filter interface'>
   3 
   4 This is an example filter providing package
   5 
   6 </description>
   7 <author>Tully Foote/tfoote@willowgarage.com</author>
   8 <license>BSD</license>
   9 <review status="unreviewed" notes=""/>
  10 <url>http://pr.willowgarage.com</url>
  11 <depend package="rosconsole" />
  12 <depend package="roscpp" />
  13 <depend package="pluginlib" />
  14 <export>
  15   <filters plugin="${prefix}/plugin_description_file.xml" />
  16 </export>
  17 </package>


2020-01-25 12:38