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


descartes_planner
Author(s): Jorge Nicho
autogenerated on Thu Jun 6 2019 21:36:12