planning_request_adapter.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2012, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include <moveit/planning_request_adapter/planning_request_adapter.h>
00038 #include <boost/bind.hpp>
00039 #include <algorithm>
00040 
00041 // we could really use some c++11 lambda functions here :)
00042 
00043 namespace planning_request_adapter
00044 {
00045 namespace
00046 {
00047 bool callPlannerInterfaceSolve(const planning_interface::PlannerManager* planner,
00048                                const planning_scene::PlanningSceneConstPtr& planning_scene,
00049                                const planning_interface::MotionPlanRequest& req,
00050                                planning_interface::MotionPlanResponse& res)
00051 {
00052   planning_interface::PlanningContextPtr context = planner->getPlanningContext(planning_scene, req, res.error_code_);
00053   if (context)
00054     return context->solve(res);
00055   else
00056     return false;
00057 }
00058 }
00059 }
00060 
00061 bool planning_request_adapter::PlanningRequestAdapter::adaptAndPlan(
00062     const planning_interface::PlannerManagerPtr& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
00063     const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
00064     std::vector<std::size_t>& added_path_index) const
00065 {
00066   return adaptAndPlan(boost::bind(&callPlannerInterfaceSolve, planner.get(), _1, _2, _3), planning_scene, req, res,
00067                       added_path_index);
00068 }
00069 
00070 bool planning_request_adapter::PlanningRequestAdapter::adaptAndPlan(
00071     const planning_interface::PlannerManagerPtr& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
00072     const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res) const
00073 {
00074   std::vector<std::size_t> dummy;
00075   return adaptAndPlan(planner, planning_scene, req, res, dummy);
00076 }
00077 
00078 namespace planning_request_adapter
00079 {
00080 namespace
00081 {
00082 // boost bind is not happy with overloading, so we add intermediate function objects
00083 
00084 bool callAdapter1(const PlanningRequestAdapter* adapter, const planning_interface::PlannerManagerPtr& planner,
00085                   const planning_scene::PlanningSceneConstPtr& planning_scene,
00086                   const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
00087                   std::vector<std::size_t>& added_path_index)
00088 {
00089   try
00090   {
00091     return adapter->adaptAndPlan(planner, planning_scene, req, res, added_path_index);
00092   }
00093   catch (std::runtime_error& ex)
00094   {
00095     logError("Exception caught executing *final* adapter '%s': %s", adapter->getDescription().c_str(), ex.what());
00096     added_path_index.clear();
00097     return callPlannerInterfaceSolve(planner.get(), planning_scene, req, res);
00098   }
00099   catch (...)
00100   {
00101     logError("Exception caught executing *final* adapter '%s'", adapter->getDescription().c_str());
00102     added_path_index.clear();
00103     return callPlannerInterfaceSolve(planner.get(), planning_scene, req, res);
00104   }
00105 }
00106 
00107 bool callAdapter2(const PlanningRequestAdapter* adapter, const PlanningRequestAdapter::PlannerFn& planner,
00108                   const planning_scene::PlanningSceneConstPtr& planning_scene,
00109                   const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
00110                   std::vector<std::size_t>& added_path_index)
00111 {
00112   try
00113   {
00114     return adapter->adaptAndPlan(planner, planning_scene, req, res, added_path_index);
00115   }
00116   catch (std::runtime_error& ex)
00117   {
00118     logError("Exception caught executing *next* adapter '%s': %s", adapter->getDescription().c_str(), ex.what());
00119     added_path_index.clear();
00120     return planner(planning_scene, req, res);
00121   }
00122   catch (...)
00123   {
00124     logError("Exception caught executing *next* adapter '%s'", adapter->getDescription().c_str());
00125     added_path_index.clear();
00126     return planner(planning_scene, req, res);
00127   }
00128 }
00129 }
00130 }
00131 
00132 bool planning_request_adapter::PlanningRequestAdapterChain::adaptAndPlan(
00133     const planning_interface::PlannerManagerPtr& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
00134     const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res) const
00135 {
00136   std::vector<std::size_t> dummy;
00137   return adaptAndPlan(planner, planning_scene, req, res, dummy);
00138 }
00139 
00140 bool planning_request_adapter::PlanningRequestAdapterChain::adaptAndPlan(
00141     const planning_interface::PlannerManagerPtr& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
00142     const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
00143     std::vector<std::size_t>& added_path_index) const
00144 {
00145   // if there are no adapters, run the planner directly
00146   if (adapters_.empty())
00147   {
00148     added_path_index.clear();
00149     return callPlannerInterfaceSolve(planner.get(), planning_scene, req, res);
00150   }
00151   else
00152   {
00153     // the index values added by each adapter
00154     std::vector<std::vector<std::size_t> > added_path_index_each(adapters_.size());
00155 
00156     // if there are adapters, construct a function pointer for each, in order,
00157     // so that in the end we have a nested sequence of function pointers that call the adapters in the correct order.
00158     PlanningRequestAdapter::PlannerFn fn = boost::bind(&callAdapter1, adapters_.back().get(), planner, _1, _2, _3,
00159                                                        boost::ref(added_path_index_each.back()));
00160     for (int i = adapters_.size() - 2; i >= 0; --i)
00161       fn = boost::bind(&callAdapter2, adapters_[i].get(), fn, _1, _2, _3, boost::ref(added_path_index_each[i]));
00162     bool result = fn(planning_scene, req, res);
00163     added_path_index.clear();
00164 
00165     // merge the index values from each adapter
00166     for (std::size_t i = 0; i < added_path_index_each.size(); ++i)
00167       for (std::size_t j = 0; j < added_path_index_each[i].size(); ++j)
00168       {
00169         for (std::size_t k = 0; k < added_path_index.size(); ++k)
00170           if (added_path_index_each[i][j] <= added_path_index[k])
00171             added_path_index[k]++;
00172         added_path_index.push_back(added_path_index_each[i][j]);
00173       }
00174     std::sort(added_path_index.begin(), added_path_index.end());
00175     return result;
00176   }
00177 }


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Wed Jun 19 2019 19:23:49