dense_planner.cpp
Go to the documentation of this file.
00001 /*
00002  * dense_planner.cpp
00003  *
00004  *  Created on: Feb 9, 2015
00005  *      Author: ros developer 
00006  */
00007 
00008 #include <descartes_planner/dense_planner.h>
00009 
00010 namespace descartes_planner
00011 {
00012 
00013 using namespace descartes_core;
00014 DensePlanner::DensePlanner():
00015     planning_graph_(),
00016     error_code_(descartes_core::PlannerError::UNINITIALIZED)
00017 {
00018  error_map_ = {
00019                 {PlannerError::OK,"OK"},
00020                 {PlannerError::EMPTY_PATH,"No path plan has been generated"},
00021                 {PlannerError::INVALID_ID,"ID is nil or isn't part of the path"},
00022                 {PlannerError::IK_NOT_AVAILABLE,"One or more ik solutions could not be found"},
00023                 {PlannerError::UNINITIALIZED,"Planner has not been initialized with a robot model"},
00024                 {PlannerError::INCOMPLETE_PATH,"Input trajectory and output path point cound differ"}
00025               };
00026 }
00027 
00028 DensePlanner::~DensePlanner()
00029 {
00030 
00031 }
00032 
00033 bool DensePlanner::initialize(descartes_core::RobotModelConstPtr model)
00034 {
00035   planning_graph_ = boost::shared_ptr<descartes_planner::PlanningGraph>(
00036       new descartes_planner::PlanningGraph(std::move(model)));
00037   error_code_ = descartes_core::PlannerErrors::EMPTY_PATH;
00038   return true;
00039 }
00040 
00041 bool DensePlanner::setConfig(const descartes_core::PlannerConfig& config)
00042 {
00043   config_ = config;
00044   config_.clear();
00045   return true;
00046 }
00047 
00048 void DensePlanner::getConfig(descartes_core::PlannerConfig& config) const
00049 {
00050   config = config_;
00051 }
00052 
00053 descartes_core::TrajectoryPt::ID DensePlanner::getPrevious(const descartes_core::TrajectoryPt::ID& ref_id)
00054 {
00055   descartes_core::TrajectoryPt::ID id;
00056   auto predicate = [&ref_id](descartes_core::TrajectoryPtPtr p)
00057     {
00058       return ref_id == p->getID();
00059     };
00060 
00061   auto pos = std::find_if(path_.begin()++,path_.end(),predicate);
00062   if(pos == path_.end() )
00063   {
00064     id =  descartes_core::TrajectoryID::make_nil();
00065   }
00066   else
00067   {
00068     pos--;
00069     id =  (*pos)->getID();
00070   }
00071 
00072   return id;
00073 }
00074 
00075 bool DensePlanner::updatePath()
00076 {
00077   std::vector<descartes_core::TrajectoryPtPtr> traj;
00078   const CartesianMap& cart_map = planning_graph_->getCartesianMap();
00079   descartes_core::TrajectoryPt::ID first_id = descartes_core::TrajectoryID::make_nil();
00080   auto predicate = [&first_id](const std::pair<descartes_core::TrajectoryPt::ID,CartesianPointInformation>& p)
00081     {
00082       const auto& info = p.second;
00083       if(info.links_.id_previous == descartes_core::TrajectoryID::make_nil())
00084       {
00085         first_id = p.first;
00086         return true;
00087       }
00088       else
00089       {
00090         return false;
00091       }
00092     };
00093 
00094   // finding first point
00095   if(cart_map.empty()
00096       || (std::find_if(cart_map.begin(),cart_map.end(),predicate) == cart_map.end())
00097       || first_id == descartes_core::TrajectoryID::make_nil())
00098   {
00099     error_code_ = descartes_core::PlannerError::INVALID_ID;
00100     return false;
00101   }
00102 
00103   // retrieving original trajectory
00104   traj.resize(cart_map.size());
00105   descartes_core::TrajectoryPt::ID current_id = first_id;
00106   for(int i = 0; i < traj.size(); i++)
00107   {
00108     if(cart_map.count(current_id) == 0)
00109     {
00110       ROS_ERROR_STREAM("Trajectory point "<<current_id<<" was not found in cartesian trajectory.");
00111       return false;
00112     }
00113     const CartesianPointInformation& info  = cart_map.at(current_id);
00114     traj[i] = info.source_trajectory_;
00115     current_id = info.links_.id_next;
00116   }
00117 
00118   // updating planned path
00119   std::list<descartes_trajectory::JointTrajectoryPt> list;
00120   double cost;
00121   if(planning_graph_->getShortestPath(cost,list) )
00122   {
00123     ROS_INFO_STREAM("Traj size: " << traj.size() << " List size: " << list.size());
00124     if(traj.size() == list.size())
00125     {
00126       error_code_ = descartes_core::PlannerError::OK;
00127 
00128       // reassigning ids
00129       int counter = 0;
00130       path_.resize(list.size());
00131       for(auto p : list)
00132       {
00133         path_[counter] = descartes_core::TrajectoryPtPtr(new descartes_trajectory::JointTrajectoryPt(p));
00134         path_[counter]->setID(traj[counter]->getID());
00135         counter++;
00136       }
00137     }
00138     else
00139     {
00140       error_code_ = descartes_core::PlannerError::INCOMPLETE_PATH;
00141     }
00142   }
00143   else
00144   {
00145     error_code_ = descartes_core::PlannerError::IK_NOT_AVAILABLE;
00146   }
00147 
00148   return error_code_ == descartes_core::PlannerError::OK;
00149 }
00150 
00151 descartes_core::TrajectoryPt::ID DensePlanner::getNext(const descartes_core::TrajectoryPt::ID& ref_id)
00152 {
00153   descartes_core::TrajectoryPt::ID id;
00154   auto predicate = [&ref_id](descartes_core::TrajectoryPtPtr p)
00155     {
00156       return ref_id == p->getID();
00157     };
00158 
00159   auto pos = std::find_if(path_.begin(),path_.end()-2,predicate);
00160   if(pos == path_.end() )
00161   {
00162     id =  descartes_core::TrajectoryID::make_nil();
00163   }
00164   else
00165   {
00166     pos++;
00167     id =  (*pos)->getID();
00168   }
00169   return id;
00170 }
00171 
00172 descartes_core::TrajectoryPtPtr DensePlanner::get(const descartes_core::TrajectoryPt::ID& ref_id)
00173 {
00174   descartes_core::TrajectoryPtPtr p;
00175   auto predicate = [&ref_id](descartes_core::TrajectoryPtPtr p)
00176     {
00177       return ref_id == p->getID();
00178     };
00179 
00180   auto pos = std::find_if(path_.begin(),path_.end()-2,predicate);
00181   if(pos == path_.end() )
00182   {
00183     p.reset();
00184   }
00185   else
00186   {
00187     p  =  *pos;
00188   }
00189   return p;
00190 }
00191 
00192 bool DensePlanner::planPath(const std::vector<descartes_core::TrajectoryPtPtr>& traj)
00193 {
00194   if(error_code_ == descartes_core::PlannerError::UNINITIALIZED)
00195   {
00196     ROS_ERROR_STREAM("Planner has not been initialized");
00197     return false;
00198   }
00199 
00200   double cost;
00201   path_.clear();
00202   error_code_ = descartes_core::PlannerError::EMPTY_PATH;
00203 
00204   if(planning_graph_->insertGraph(&traj))
00205   {
00206     updatePath();
00207   }
00208   else
00209   {
00210     error_code_ = descartes_core::PlannerError::IK_NOT_AVAILABLE;
00211   }
00212 
00213   return descartes_core::PlannerError::OK == error_code_;
00214 }
00215 
00216 bool DensePlanner::getPath(std::vector<descartes_core::TrajectoryPtPtr>& path) const
00217 {
00218   if (path_.empty()) return false;
00219   
00220   path.assign(path_.begin(),path_.end());
00221   return error_code_ == descartes_core::PlannerError::OK;
00222 }
00223 
00224 bool DensePlanner::addAfter(const descartes_core::TrajectoryPt::ID& ref_id, descartes_core::TrajectoryPtPtr tp)
00225 {
00226   if(path_.empty())
00227   {
00228     return false;
00229   }
00230 
00231   descartes_core::TrajectoryPt::ID next_id = getNext(ref_id);
00232   if(!next_id.is_nil())
00233   {
00234     if(planning_graph_->addTrajectory(tp,ref_id,next_id))
00235     {
00236       if(updatePath())
00237       {
00238         error_code_ = descartes_core::PlannerError::OK;
00239       }
00240       else
00241       {
00242         return false;
00243       }
00244     }
00245     else
00246     {
00247       error_code_ = descartes_core::PlannerErrors::IK_NOT_AVAILABLE;
00248       return false;
00249     }
00250   }
00251   else
00252   {
00253     error_code_ = descartes_core::PlannerError::INVALID_ID;
00254     return false;
00255   }
00256 
00257   return true;
00258 }
00259 
00260 bool DensePlanner::addBefore(const descartes_core::TrajectoryPt::ID& ref_id, descartes_core::TrajectoryPtPtr tp)
00261 {
00262   if(path_.empty())
00263   {
00264     return false;
00265   }
00266 
00267   descartes_core::TrajectoryPt::ID prev_id = getPrevious(ref_id);
00268   if(!prev_id.is_nil())
00269   {
00270     if(planning_graph_->addTrajectory(tp,prev_id,ref_id))
00271     {
00272       if(updatePath())
00273       {
00274         error_code_ = descartes_core::PlannerError::OK;
00275       }
00276       else
00277       {
00278         return false;
00279       }
00280     }
00281     else
00282     {
00283       error_code_ = descartes_core::PlannerErrors::IK_NOT_AVAILABLE;
00284       return false;
00285     }
00286   }
00287   else
00288   {
00289     error_code_ = descartes_core::PlannerError::INVALID_ID;
00290     return false;
00291   }
00292 
00293   return true;
00294 }
00295 
00296 bool DensePlanner::remove(const descartes_core::TrajectoryPt::ID& ref_id)
00297 {
00298   if(path_.empty())
00299   {
00300     return false;
00301   }
00302 
00303   descartes_core::TrajectoryPtPtr tp = get(ref_id);
00304   if(tp)
00305   {
00306     tp->setID(ref_id);
00307     if(planning_graph_->removeTrajectory(tp))
00308     {
00309       if(updatePath())
00310       {
00311         error_code_ = descartes_core::PlannerError::OK;
00312       }
00313       else
00314       {
00315         return false;
00316       }
00317     }
00318     else
00319     {
00320       error_code_ = descartes_core::PlannerErrors::IK_NOT_AVAILABLE;
00321       return false;
00322     }
00323   }
00324   else
00325   {
00326     error_code_ = descartes_core::PlannerError::INVALID_ID;
00327     return false;
00328   }
00329 
00330   return true;
00331 }
00332 
00333 bool DensePlanner::modify(const descartes_core::TrajectoryPt::ID& ref_id, descartes_core::TrajectoryPtPtr tp)
00334 {
00335   if(path_.empty())
00336   {
00337     return false;
00338   }
00339 
00340   if(!ref_id.is_nil())
00341   {
00342     tp->setID(ref_id);
00343     if(planning_graph_->modifyTrajectory(tp))
00344     {
00345       if(updatePath())
00346       {
00347         error_code_ = descartes_core::PlannerError::OK;
00348       }
00349       else
00350       {
00351         return false;
00352       }
00353     }
00354     else
00355     {
00356       error_code_ = descartes_core::PlannerErrors::IK_NOT_AVAILABLE;
00357       return false;
00358     }
00359   }
00360   else
00361   {
00362     error_code_ = descartes_core::PlannerError::INVALID_ID;
00363     return false;
00364   }
00365 
00366   return true;
00367 }
00368 
00369 int DensePlanner::getErrorCode() const
00370 {
00371   return error_code_;
00372 }
00373 
00374 bool DensePlanner::getErrorMessage(int error_code, std::string& msg) const
00375 {
00376   std::map<int,std::string>::const_iterator it = error_map_.find(error_code);
00377 
00378   if(it != error_map_.cend())
00379   {
00380     msg = it->second;
00381   }
00382   else
00383   {
00384     return false;
00385   }
00386   return true;
00387 }
00388 
00389 
00390 } /* namespace descartes_core */


descartes_planner
Author(s): Jorge Nicho
autogenerated on Wed Aug 26 2015 11:21:35