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 try: 00040 from ompl import base as ob 00041 from ompl import geometric as og 00042 except: 00043 # if the ompl module is not in the PYTHONPATH assume it is installed in a 00044 # subdirectory of the parent directory called "py-bindings." 00045 from os.path import basename, abspath, dirname, join 00046 import sys 00047 sys.path.insert(0, join(dirname(dirname(abspath(__file__))),'py-bindings')) 00048 from ompl import base as ob 00049 from ompl import geometric as og 00050 00051 def isStateValid(spaceInformation, state): 00052 # Some arbitrary condition on the state (note that thanks to 00053 # dynamic type checking we can just call getX() and do not need 00054 # to convert state to an SE2State.) 00055 return state.getX() < .6 00056 00057 def plan(): 00058 # create an SE2 state space 00059 space = ob.SE2StateSpace() 00060 # set lower and upper bounds 00061 bounds = ob.RealVectorBounds(2) 00062 bounds.setLow(-1) 00063 bounds.setHigh(1) 00064 space.setBounds(bounds) 00065 # construct an instance of space information from this state space 00066 si = ob.SpaceInformation(space) 00067 # set state validity checking for this space 00068 si.setStateValidityChecker(isStateValid) 00069 # create a random start state 00070 start = ob.State(space) 00071 start.random() 00072 # create a random goal state 00073 goal = ob.State(space) 00074 goal.random() 00075 # create a problem instance 00076 pdef = ob.ProblemDefinition(si) 00077 # set the start and goal states 00078 pdef.setStartAndGoalStates(start, goal) 00079 # create a planner for the defined space 00080 planner = og.RRTConnect(si) 00081 # set the problem we are trying to solve for the planner 00082 planner.setProblemDefinition(pdef) 00083 # perform setup steps for the planner 00084 planner.setup() 00085 # print the settings for this space 00086 print si.settings() 00087 # print the problem settings 00088 print pdef 00089 # attempt to solve the problem within one second of planning time 00090 solved = planner.solve(1.0) 00091 00092 if solved: 00093 # get the goal representation from the problem definition (not the same as the goal state) 00094 # and inquire about the found path 00095 path = pdef.getGoal().getSolutionPath() 00096 print "Found solution:\n", path 00097 else: 00098 print "No solution found" 00099 00100 00101 def planWithSimpleSetup(): 00102 # create an SE2 state space 00103 space = ob.SE2StateSpace() 00104 00105 # set lower and upper bounds 00106 bounds = ob.RealVectorBounds(2) 00107 bounds.setLow(-1) 00108 bounds.setHigh(1) 00109 space.setBounds(bounds) 00110 00111 # create a simple setup object 00112 ss = og.SimpleSetup(space) 00113 ss.setStateValidityChecker(isStateValid) 00114 00115 start = ob.State(space) 00116 # we can pick a random start state... 00117 start.random() 00118 # ... or set specific values 00119 start().setX(.5) 00120 00121 goal = ob.State(space) 00122 # we can pick a random goal state... 00123 goal.random() 00124 # ... or set specific values 00125 goal().setY(-.5) 00126 00127 ss.setStartAndGoalStates(start, goal) 00128 00129 # this will automatically choose a default planner with 00130 # default parameters 00131 solved = ss.solve(1.0) 00132 00133 if solved: 00134 # try to shorten the path 00135 ss.simplifySolution() 00136 # print the simplified path 00137 print ss.getSolutionPath() 00138 00139 00140 if __name__ == "__main__": 00141 plan() 00142 print "" 00143 planWithSimpleSetup()