ompl/extensions/opende/src/OpenDESimpleSetup.cpp
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/extensions/opende/OpenDESimpleSetup.h" 00038 #include "ompl/util/Exception.h" 00039 #include <boost/thread.hpp> 00040 00041 ompl::control::OpenDESimpleSetup::OpenDESimpleSetup(const ControlSpacePtr &space) : SimpleSetup(space) 00042 { 00043 if (!dynamic_cast<OpenDEControlSpace*>(space.get())) 00044 throw Exception("OpenDE Control Space needed for OpenDE Simple Setup"); 00045 useEnvParams(); 00046 } 00047 00048 ompl::control::OpenDESimpleSetup::OpenDESimpleSetup(const base::StateSpacePtr &space) : 00049 SimpleSetup(ControlSpacePtr(new OpenDEControlSpace(space))) 00050 { 00051 useEnvParams(); 00052 } 00053 00054 ompl::control::OpenDESimpleSetup::OpenDESimpleSetup(const OpenDEEnvironmentPtr &env) : 00055 SimpleSetup(ControlSpacePtr(new OpenDEControlSpace(base::StateSpacePtr(new OpenDEStateSpace(env))))) 00056 { 00057 useEnvParams(); 00058 } 00059 00060 void ompl::control::OpenDESimpleSetup::useEnvParams() 00061 { 00062 si_->setPropagationStepSize(getStateSpace()->as<OpenDEStateSpace>()->getEnvironment()->stepSize_); 00063 si_->setMinMaxControlDuration(getStateSpace()->as<OpenDEStateSpace>()->getEnvironment()->minControlSteps_, 00064 getStateSpace()->as<OpenDEStateSpace>()->getEnvironment()->maxControlSteps_); 00065 si_->setStatePropagator(StatePropagatorPtr(new OpenDEStatePropagator(si_))); 00066 } 00067 00068 ompl::base::ScopedState<ompl::control::OpenDEStateSpace> ompl::control::OpenDESimpleSetup::getCurrentState() const 00069 { 00070 base::ScopedState<OpenDEStateSpace> current(getStateSpace()); 00071 getStateSpace()->as<OpenDEStateSpace>()->readState(current.get()); 00072 return current; 00073 } 00074 00075 void ompl::control::OpenDESimpleSetup::setCurrentState(const base::State *state) 00076 { 00077 getStateSpace()->as<OpenDEStateSpace>()->writeState(state); 00078 } 00079 00080 void ompl::control::OpenDESimpleSetup::setCurrentState(const base::ScopedState<> &state) 00081 { 00082 getStateSpace()->as<OpenDEStateSpace>()->writeState(state.get()); 00083 } 00084 00085 void ompl::control::OpenDESimpleSetup::setup() 00086 { 00087 if (!si_->getStateValidityChecker()) 00088 { 00089 OMPL_INFORM("Using default state validity checker for OpenDE"); 00090 si_->setStateValidityChecker(base::StateValidityCheckerPtr(new OpenDEStateValidityChecker(si_))); 00091 } 00092 if (pdef_->getStartStateCount() == 0) 00093 { 00094 OMPL_INFORM("Using the initial state of OpenDE as the starting state for the planner"); 00095 pdef_->addStartState(getCurrentState()); 00096 } 00097 SimpleSetup::setup(); 00098 } 00099 00100 void ompl::control::OpenDESimpleSetup::playSolutionPath(double timeFactor) const 00101 { 00102 if (haveSolutionPath()) 00103 playPath(pdef_->getSolutionPath(), timeFactor); 00104 } 00105 00106 void ompl::control::OpenDESimpleSetup::playPath(const base::PathPtr &path, double timeFactor) const 00107 { 00108 bool ctl = false; 00109 if (dynamic_cast<PathControl*>(path.get())) 00110 ctl = true; 00111 else 00112 if (!dynamic_cast<geometric::PathGeometric*>(path.get())) 00113 throw Exception("Unknown type of path"); 00114 00115 const geometric::PathGeometric &pg = ctl ? 00116 static_cast<PathControl*>(path.get())->asGeometric() : *static_cast<geometric::PathGeometric*>(path.get()); 00117 00118 if (pg.getStateCount() > 0) 00119 { 00120 OMPL_DEBUG("Playing through %u states (%0.3f seconds)", (unsigned int)pg.getStateCount(), 00121 timeFactor * si_->getPropagationStepSize() * (double)(pg.getStateCount() - 1)); 00122 time::duration d = time::seconds(timeFactor * si_->getPropagationStepSize()); 00123 getStateSpace()->as<OpenDEStateSpace>()->writeState(pg.getState(0)); 00124 for (unsigned int i = 1 ; i < pg.getStateCount() ; ++i) 00125 { 00126 boost::this_thread::sleep(d); 00127 getStateSpace()->as<OpenDEStateSpace>()->writeState(pg.getState(i)); 00128 } 00129 } 00130 } 00131 00132 ompl::base::PathPtr ompl::control::OpenDESimpleSetup::simulateControl(const double *control, unsigned int steps) const 00133 { 00134 Control *c = si_->allocControl(); 00135 memcpy(c->as<OpenDEControlSpace::ControlType>()->values, control, sizeof(double) * getControlSpace()->getDimension()); 00136 base::PathPtr path = simulateControl(c, steps); 00137 si_->freeControl(c); 00138 return path; 00139 } 00140 00141 ompl::base::PathPtr ompl::control::OpenDESimpleSetup::simulateControl(const Control *control, unsigned int steps) const 00142 { 00143 PathControl *p = new PathControl(si_); 00144 00145 base::State *s0 = si_->allocState(); 00146 getStateSpace()->as<OpenDEStateSpace>()->readState(s0); 00147 p->getStates().push_back(s0); 00148 00149 base::State *s1 = si_->allocState(); 00150 si_->propagate(s0, control, steps, s1); 00151 p->getStates().push_back(s1); 00152 00153 p->getControls().push_back(si_->cloneControl(control)); 00154 p->getControlDurations().push_back(steps); 00155 return base::PathPtr(p); 00156 } 00157 00158 ompl::base::PathPtr ompl::control::OpenDESimpleSetup::simulate(unsigned int steps) const 00159 { 00160 Control *c = si_->allocControl(); 00161 si_->nullControl(c); 00162 base::PathPtr path = simulateControl(c, steps); 00163 si_->freeControl(c); 00164 return path; 00165 }