00001 #!/usr/bin/env python 00002 00003 ###################################################################### 00004 # Software License Agreement (BSD License) 00005 # 00006 # Copyright (c) 2010, Rice University 00007 # All rights reserved. 00008 # 00009 # Redistribution and use in source and binary forms, with or without 00010 # modification, are permitted provided that the following conditions 00011 # are met: 00012 # 00013 # * Redistributions of source code must retain the above copyright 00014 # notice, this list of conditions and the following disclaimer. 00015 # * Redistributions in binary form must reproduce the above 00016 # copyright notice, this list of conditions and the following 00017 # disclaimer in the documentation and/or other materials provided 00018 # with the distribution. 00019 # * Neither the name of the Rice University nor the names of its 00020 # contributors may be used to endorse or promote products derived 00021 # from this software without specific prior written permission. 00022 # 00023 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00024 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00025 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00026 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00027 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00028 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00029 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00033 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00034 # POSSIBILITY OF SUCH DAMAGE. 00035 ###################################################################### 00036 00037 # Author: Mark Moll 00038 00039 from math import sin, cos 00040 try: 00041 from ompl import base as ob 00042 from ompl import control as oc 00043 from ompl import geometric as og 00044 except: 00045 # if the ompl module is not in the PYTHONPATH assume it is installed in a 00046 # subdirectory of the parent directory called "py-bindings." 00047 from os.path import basename, abspath, dirname, join 00048 import sys 00049 sys.path.insert(0, join(dirname(dirname(abspath(__file__))),'py-bindings')) 00050 from ompl import base as ob 00051 from ompl import control as oc 00052 from ompl import geometric as og 00053 00054 def isStateValid(spaceInformation, state): 00055 # perform collision checking or check if other constraints are 00056 # satisfied 00057 return spaceInformation.satisfiesBounds(state) 00058 00059 def propagate(cspace, start, control, duration, state): 00060 state.setX( start.getX() + control[0] * duration * cos(start.getYaw()) ) 00061 state.setY( start.getY() + control[0] * duration * sin(start.getYaw()) ) 00062 state.setYaw(start.getYaw() + control[1] * duration) 00063 00064 def plan(): 00065 # construct the state space we are planning in 00066 space = ob.SE2StateSpace() 00067 00068 # set the bounds for the R^2 part of SE(2) 00069 bounds = ob.RealVectorBounds(2) 00070 bounds.setLow(-1) 00071 bounds.setHigh(1) 00072 space.setBounds(bounds) 00073 00074 # create a control space 00075 cspace = oc.RealVectorControlSpace(space, 2) 00076 00077 # set the bounds for the control space 00078 cbounds = ob.RealVectorBounds(2) 00079 cbounds.setLow(-.3) 00080 cbounds.setHigh(.3) 00081 cspace.setBounds(cbounds) 00082 00083 # set the state propagation routine 00084 cspace.setPropagationFunction(propagate) 00085 00086 # define a simple setup class 00087 ss = oc.SimpleSetup(cspace) 00088 ss.setStateValidityChecker(isStateValid) 00089 00090 # create a start state 00091 start = ob.State(space) 00092 start().setX(-0.5); 00093 start().setY(0.0); 00094 start().setYaw(0.0); 00095 00096 # create a goal state 00097 goal = ob.State(space); 00098 goal().setX(0.0); 00099 goal().setY(0.5); 00100 goal().setYaw(0.0); 00101 00102 # set the start and goal states 00103 ss.setStartAndGoalStates(start, goal, 0.05) 00104 00105 # attempt to solve the problem 00106 solved = ss.solve(20.0) 00107 00108 if solved: 00109 # print the path to screen 00110 print "Found solution:", ss.getSolutionPath().asGeometric() 00111 00112 if __name__ == "__main__": 00113 plan()