00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Rice University 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 the Rice University 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 "ompl/control/SimpleSetup.h" 00038 #include "ompl/control/planners/rrt/RRT.h" 00039 #include "ompl/control/planners/kpiece/KPIECE1.h" 00040 00041 ompl::base::PlannerPtr ompl::control::getDefaultPlanner(const base::GoalPtr &goal) 00042 { 00043 base::PlannerPtr planner; 00044 if (!goal) 00045 throw Exception("Unable to allocate default planner for unspecified goal definition"); 00046 00047 SpaceInformationPtr si = boost::static_pointer_cast<SpaceInformation, base::SpaceInformation>(goal->getSpaceInformation()); 00048 if (si->getStateManifold()->hasDefaultProjection()) 00049 planner = base::PlannerPtr(new KPIECE1(si)); 00050 else 00051 planner = base::PlannerPtr(new RRT(si)); 00052 00053 return planner; 00054 } 00055 00056 void ompl::control::SimpleSetup::setup(void) 00057 { 00058 if (!configured_) 00059 { 00060 if (!si_) 00061 throw Exception("No space information defined"); 00062 00063 if (!si_->isSetup()) 00064 si_->setup(); 00065 if (!planner_) 00066 { 00067 if (pa_) 00068 planner_ = pa_(si_); 00069 else 00070 { 00071 msg_.inform("No planner specified. Using default."); 00072 planner_ = getDefaultPlanner(getGoal()); 00073 } 00074 } 00075 planner_->setProblemDefinition(pdef_); 00076 if (!planner_->isSetup()) 00077 planner_->setup(); 00078 configured_ = true; 00079 } 00080 } 00081 00082 void ompl::control::SimpleSetup::clear(void) 00083 { 00084 if (planner_) 00085 planner_->clear(); 00086 if (pdef_ && pdef_->getGoal()) 00087 pdef_->getGoal()->clearSolutionPath(); 00088 } 00089 00090 bool ompl::control::SimpleSetup::solve(double time) 00091 { 00092 setup(); 00093 time::point start = time::now(); 00094 bool result = planner_->solve(time); 00095 planTime_ = time::seconds(time::now() - start); 00096 if (result) 00097 msg_.inform("Solution found in %f seconds", planTime_); 00098 else 00099 msg_.inform("No solution found after %f seconds", planTime_); 00100 return result; 00101 } 00102 00103 void ompl::control::SimpleSetup::updateProjectionCellSizes(void) 00104 { 00105 const std::vector<const base::State*> &states = getPlannerData().states; 00106 if (states.empty()) 00107 msg_.warn("There are no states in the exploration data structure of the planner"); 00108 else 00109 { 00110 const std::map<std::string, base::ProjectionEvaluatorPtr> &prj = getStateManifold()->getRegisteredProjections(); 00111 for (std::map<std::string, base::ProjectionEvaluatorPtr>::const_iterator it = prj.begin() ; it != prj.end() ; ++it) 00112 it->second->computeCellSizes(states); 00113 } 00114 } 00115 00116 ompl::control::PathControl& ompl::control::SimpleSetup::getSolutionPath(void) const 00117 { 00118 if (pdef_ && pdef_->getGoal()) 00119 { 00120 const base::PathPtr &p = pdef_->getGoal()->getSolutionPath(); 00121 if (p) 00122 return static_cast<PathControl&>(*p); 00123 } 00124 throw Exception("No solution path"); 00125 } 00126 00127 ompl::base::PlannerData ompl::control::SimpleSetup::getPlannerData(void) const 00128 { 00129 base::PlannerData pd; 00130 if (planner_) 00131 planner_->getPlannerData(pd); 00132 return pd; 00133 }