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

Writing a local path planner as plugin in ROS

Description: A tutorial to writing a custom local planner to work with the ROS1.This tutorial will be structured in a similar manner to ROS Global Path Planner

Keywords: local planner, move_base

Tutorial Level: BEGINNER

Next Tutorial: Writing a Global Planner Plugin

Note: The ROS Wiki is generally for ROS 1 only! If you have installed ROS 2 please use the ROS 2 documentation website.

Writing the Path Planner Class

ROS Installation

Before starting these tutorials please complete installation as described in the ROS installation instructions.

Class Implementation Header

The first step is to write a new class for the path planner that adheres to the ROS Base Local Planner nav_core::BaseLocalPlanner class. For this, you need to create a header file, that we will call in our case, local_planner.h. I will present just the minimal code for adding a plugin, which are the necessary and common steps to add any local planner. Be sure to create a ros package and place the code within the package. The minimal header file is defined as follows:

   1 #ifndef LOCAL_PLANNER_H_
   2 #define LOCAL_PLANNER_H_
   3 
   4 // abstract class from which our plugin inherits
   5 #include <nav_core/base_local_planner.h>
   6 
   7 #include <ros/ros.h>
   8 
   9 using namespace std;
  10 
  11 namespace simple_local_planner{
  12 
  13   class LocalPlanner : public nav_core::BaseLocalPlanner{
  14 
  15     public:
  16       LocalPlanner();
  17 
  18       LocalPlanner(std::string name, tf::TransformListener* tf,
  19           costmap_2d::Costmap2DROS* costmap_ros);
  20 
  21       ~LocalPlanner();
  22 
  23       void initialize(std::string name, tf::TransformListener* tf,
  24           costmap_2d::Costmap2DROS* costmap_ros);
  25 
  26 
  27       bool setPlan(const std::vector<geometry_msgs::PoseStamped>& orig_global_plan);
  28 
  29       bool computeVelocityCommands(geometry_msgs::Twist& cmd_vel);
  30 
  31       bool isGoalReached();
  32 
  33     private:
  34   };
  35 };
  36 
  37 #endif
  38 

Class Implementation Source

For this, you need to create a source file, that we will call in our case, local_planner.cpp with the following:

   1 #include "local_planner/local_planner.h"
   2 
   3 #include <pluginlib/class_list_macros.h>
   4 
   5 PLUGINLIB_DECLARE_CLASS(local_planner, LocalPlanner, local_planner::LocalPlanner, nav_core::BaseLocalPlanner)
   6 
   7 namespace local_planner{
   8 
   9         LocalPlanner::LocalPlanner() : costmap_ros_(NULL), tf_(NULL), initialized_(false) {}
  10 
  11         LocalPlanner::LocalPlanner(std::string name, tf::TransformListener* tf, costmap_2d::Costmap2DROS* costmap_ros)
  12          : costmap_ros_(NULL), tf_(NULL), initialized_(false)
  13          {
  14                 initialize(name, tf, costmap_ros);
  15          }
  16 
  17         LocalPlanner::~LocalPlanner() {}
  18 
  19         void LocalPlanner::initialize(std::string name, tf::TransformListener* tf, costmap_2d::Costmap2DROS* costmap_ros)
  20         {
  21 
  22                 if(!initialized_)
  23                 {
  24         }
  25 
  26         }
  27 
  28         bool LocalPlanner::setPlan(const std::vector<geometry_msgs::PoseStamped>& orig_global_plan)
  29         {
  30 
  31                 if(!initialized_)
  32                 {
  33                 ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
  34                 return false;
  35                 }
  36 
  37                 return true;
  38         }
  39 
  40         bool LocalPlanner::computeVelocityCommands(geometry_msgs::Twist& cmd_vel)
  41         {
  42 
  43                 if(!initialized_)
  44                 {
  45                     ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
  46                     return false;
  47                 }
  48 
  49                 return true;
  50 
  51         }
  52 
  53         bool LocalPlanner::isGoalReached()
  54         {
  55                 if(!initialized_)
  56                 {
  57                         ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
  58                         return false;
  59                 }
  60 
  61                 return false;
  62         }
  63 }

Writing your Plugin

Create a file called lp_plugin.xml but the file can be named just about anything

<library path="lib/lib_local_planner">
        <class name ="local_planner/LocalPlanner" type ="local_planner::LocalPlanner" base_class_type= "nav_core::BaseLocalPlanner">
                <description>
                        Simplest Plugin for BaseLocalPlanner
                </description>
        </class>
</library>

Plugin Registration

In the package.xml of this package, export the file created in the previous section

  <export>
    <nav_core plugin="${prefix}/lp_plugin.xml"/>
  </export>

Use the following command and type it into the terminal. Your new local planner should now be registered. Take note that this happens after catkin build or catkin_make

rospack plugins --attrib=plugin nav_core

Usage in move_base

Add the following parameter to move_base to use the plugin that was just created. Congrats on creating your very own local planner!

 <param name="base_local_planner" value="local_planner/LocalPlanner" />

Wiki: navigation/Tutorials/Writing a Local Path Planner As Plugin in ROS (last edited 2021-10-10 15:28:23 by den-globotix)