Error when adding new planner to OMPL library for use with MoveIt!

147 views
Skip to first unread message

Ray Chang

unread,
Aug 14, 2017, 1:17:23 PM8/14/17
to MoveIt! Users
I'm following this tutorial (http://picknik.io/moveit_wiki/index.php?title=OMPL/Add_New_Planner)  to add a new planner to the existing OMPL library. I added MyPlanner in geometric/planners/rrt alongside all the other rrt planners. Currently, the header file and cpp file are both just copies of RRT.h and RRT.cpp, but with class names and function names changed from RRT to MyPlanner.

Here is MyPlanner.h:

#ifndef OMPL_GEOMETRIC_PLANNERS_RRT_MYPLANNER_
#define OMPL_GEOMETRIC_PLANNERS_RRT_MYPLANNER_

#include "ompl/geometric/planners/PlannerIncludes.h"
#include "ompl/datastructures/NearestNeighbors.h"


namespace ompl
{
   
namespace geometric
   
{
       
class MyPlanner : public base::Planner
       
{
       
public:
           
/** \brief Constructor */
           
MyPlanner(const base::SpaceInformationPtr &si);

           
~MyPlanner() override;

           
void getPlannerData(base::PlannerData &data) const override;

           
base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override;

           
void clear() override;

           
/** \brief Set the goal bias

                In the process of randomly selecting states in
                the state space to attempt to go towards, the
                algorithm may in fact choose the actual goal state, if
                it knows it, with some probability. This probability
                is a real number between 0.0 and 1.0; its value should
                usually be around 0.05 and should not be too large. It
                is probably a good idea to use the default value. */

           
void setGoalBias(double goalBias)
           
{
                goalBias_
= goalBias;
           
}

           
/** \brief Get the goal bias the planner is using */
           
double getGoalBias() const
           
{
               
return goalBias_;
           
}

           
/** \brief Set the range the planner is supposed to use.

                This parameter greatly influences the runtime of the
                algorithm. It represents the maximum length of a
                motion to be added in the tree of motions. */

           
void setRange(double distance)
           
{
                maxDistance_
= distance;
           
}

           
/** \brief Get the range the planner is using */
           
double getRange() const
           
{
               
return maxDistance_;
           
}

           
/** \brief Set a different nearest neighbors datastructure */
           
template <template <typename T> class NN>
           
void setNearestNeighbors()
           
{
               
if (nn_ && nn_->size() != 0)
                    OMPL_WARN
("Calling setNearestNeighbors will clear all states.");
                clear
();
                nn_
= std::make_shared<NN<Motion *>>();
                setup
();
           
}

           
void setup() override;

       
protected:
           
/** \brief Representation of a motion

                This only contains pointers to parent motions as we
                only need to go backwards in the tree. */

           
class Motion
           
{
           
public:
               
Motion() = default;

               
/** \brief Constructor that allocates memory for the state */
               
Motion(const base::SpaceInformationPtr &si) : state(si->allocState())
               
{
               
}

               
~Motion() = default;

               
/** \brief The state contained by the motion */
               
base::State *state{nullptr};

               
/** \brief The parent motion in the exploration tree */
               
Motion *parent{nullptr};
           
};

           
/** \brief Free the memory allocated by this planner */
           
void freeMemory();

           
/** \brief Compute distance between motions (actually distance between contained states) */
           
double distanceFunction(const Motion *a, const Motion *b) const
           
{
               
return si_->distance(a->state, b->state);
           
}

           
/** \brief State sampler */
           
base::StateSamplerPtr sampler_;

           
/** \brief A nearest-neighbors datastructure containing the tree of motions */
            std
::shared_ptr<NearestNeighbors<Motion *>> nn_;

           
/** \brief The fraction of time the goal is picked as the state to expand towards (if such a state is
             * available) */

           
double goalBias_{.05};

           
/** \brief The maximum length of a motion to be added to a tree */
           
double maxDistance_{0.};

           
/** \brief The random number generator */
            RNG rng_
;

           
/** \brief The most recent goal motion.  Used for PlannerData computation */
           
Motion *lastGoalMotion_{nullptr};
       
};
   
}
}


#endif


And here is MyPlanner.cpp:

#include "ompl/geometric/planners/rrt/MyPlanner.h"
#include "ompl/base/goals/GoalSampleableRegion.h"
#include "ompl/tools/config/SelfConfig.h"
#include <limits>

ompl
::geometric::MyPlanner::MyPlanner(const base::SpaceInformationPtr &si) : base::Planner(si, "MyPlanner")
{
    specs_
.approximateSolutions = true;
    specs_
.directed = true;

   
Planner::declareParam<double>("range", this, &MyPlanner::setRange, &MyPlanner::getRange, "0.:1.:10000.");
   
Planner::declareParam<double>("goal_bias", this, &MyPlanner::setGoalBias, &MyPlanner::getGoalBias, "0.:.05:1.");
}


ompl
::geometric::MyPlanner::~MyPlanner()
{
    freeMemory
();
}


void ompl::geometric::MyPlanner::clear()
{
   
Planner::clear();
    sampler_
.reset();
    freeMemory
();
   
if (nn_)
        nn_
->clear();
    lastGoalMotion_
= nullptr;
}

void ompl::geometric::MyPlanner::setup()
{
   
Planner::setup();
    tools
::SelfConfig sc(si_, getName());
    sc
.configurePlannerRange(maxDistance_);

   
if (!nn_)
        nn_
.reset(tools::SelfConfig::getDefaultNearestNeighbors<Motion *>(this));
    nn_
->setDistanceFunction([this](const Motion *a, const Motion *b)
                             
{
                                 
return distanceFunction(a, b);
                             
});
}

void ompl::geometric::MyPlanner::freeMemory()
{
   
if (nn_)
   
{
        std
::vector<Motion *> motions;
        nn_
->list(motions);
       
for (auto &motion : motions)
       
{
           
if (motion->state != nullptr)
                si_
->freeState(motion->state);
           
delete motion;
       
}
   
}
}

ompl
::base::PlannerStatus ompl::geometric::MyPlanner::solve(const base::PlannerTerminationCondition &ptc)
{
    checkValidity
();
   
base::Goal *goal = pdef_->getGoal().get();
   
auto *goal_s = dynamic_cast<base::GoalSampleableRegion *>(goal);

   
while (const base::State *st = pis_.nextStart())
   
{
       
auto *motion = new Motion(si_);
        si_
->copyState(motion->state, st);
        nn_
->add(motion);
   
}

   
if (nn_->size() == 0)
   
{
        OMPL_ERROR
("%s: There are no valid initial states!", getName().c_str());
       
return base::PlannerStatus::INVALID_START;
   
}

   
if (!sampler_)
        sampler_
= si_->allocStateSampler();

    OMPL_INFORM
("%s: Starting planning with %u states already in datastructure", getName().c_str(), nn_->size());

   
Motion *solution = nullptr;
   
Motion *approxsol = nullptr;
   
double approxdif = std::numeric_limits<double>::infinity();
   
auto *rmotion = new Motion(si_);
   
base::State *rstate = rmotion->state;
   
base::State *xstate = si_->allocState();

   
while (!ptc)
   
{
       
/* sample random state (with goal biasing) */
       
if ((goal_s != nullptr) && rng_.uniform01() < goalBias_ && goal_s->canSample())
            goal_s
->sampleGoal(rstate);
       
else
            sampler_
->sampleUniform(rstate);

       
/* find closest state in the tree */
       
Motion *nmotion = nn_->nearest(rmotion);
       
base::State *dstate = rstate;

       
/* find state to add */
       
double d = si_->distance(nmotion->state, rstate);
       
if (d > maxDistance_)
       
{
            si_
->getStateSpace()->interpolate(nmotion->state, rstate, maxDistance_ / d, xstate);
            dstate
= xstate;
       
}

       
if (si_->checkMotion(nmotion->state, dstate))
       
{
           
/* create a motion */
           
auto *motion = new Motion(si_);
            si_
->copyState(motion->state, dstate);
            motion
->parent = nmotion;

            nn_
->add(motion);
           
double dist = 0.0;
           
bool sat = goal->isSatisfied(motion->state, &dist);
           
if (sat)
           
{
                approxdif
= dist;
                solution
= motion;
               
break;
           
}
           
if (dist < approxdif)
           
{
                approxdif
= dist;
                approxsol
= motion;
           
}
       
}
   
}

   
bool solved = false;
   
bool approximate = false;
   
if (solution == nullptr)
   
{
        solution
= approxsol;
        approximate
= true;
   
}

   
if (solution != nullptr)
   
{
        lastGoalMotion_
= solution;

       
/* construct the solution path */
        std
::vector<Motion *> mpath;
       
while (solution != nullptr)
       
{
            mpath
.push_back(solution);
            solution
= solution->parent;
       
}

       
/* set the solution path */
       
auto path(std::make_shared<PathGeometric>(si_));
       
for (int i = mpath.size() - 1; i >= 0; --i)
            path
->append(mpath[i]->state);
        pdef_
->addSolutionPath(path, approximate, approxdif, getName());
        solved
= true;
   
}

    si_
->freeState(xstate);
   
if (rmotion->state != nullptr)
        si_
->freeState(rmotion->state);
   
delete rmotion;

    OMPL_INFORM
("%s: Created %u states", getName().c_str(), nn_->size());

   
return base::PlannerStatus(solved, approximate);
}

void ompl::geometric::MyPlanner::getPlannerData(base::PlannerData &data) const
{
   
Planner::getPlannerData(data);

    std
::vector<Motion *> motions;
   
if (nn_)
        nn_
->list(motions);

   
if (lastGoalMotion_ != nullptr)
        data
.addGoalVertex(base::PlannerDataVertex(lastGoalMotion_->state));

   
for (auto &motion : motions)
   
{
       
if (motion->parent == nullptr)
            data
.addStartVertex(base::PlannerDataVertex(motion->state));
       
else
            data
.addEdge(base::PlannerDataVertex(motion->parent->state), base::PlannerDataVertex(motion->state));
   
}

}


As you can see, the changes from the RRT files are very minimal.

However, at the last step of the tutorial, when using catkin_make to install MoveIt!, I run into this error:

make[2]: *** No rule to make target '/opt/ros/kinetic/lib/x86_64-linux-gnu/libompl.so', needed by '/home/ray/ros_ws/devel/lib/libmoveit_ompl_interface.so.0.9.9'.  Stop.
CMakeFiles/Makefile2:18665: recipe for target 'moveit/moveit_planners/ompl/ompl_interface/CMakeFiles/moveit_ompl_interface.dir/all' failed
make
[1]: *** [moveit/moveit_planners/ompl/ompl_interface/CMakeFiles/moveit_ompl_interface.dir/all] Error 2
make
[1]: *** Waiting for unfinished jobs....


Not sure where to move on from here, and thanks for any help in advance.
Reply all
Reply to author
Forward
0 new messages