All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
RigidBodyPlanningWithControls.py
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(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     # define a simple setup class
00084     ss = oc.SimpleSetup(cspace)
00085     ss.setStateValidityChecker(isStateValid)
00086     ss.setStatePropagatorFn(propagate)
00087 
00088     # create a start state
00089     start = ob.State(space)
00090     start().setX(-0.5);
00091     start().setY(0.0);
00092     start().setYaw(0.0);
00093 
00094     # create a goal state
00095     goal = ob.State(space);
00096     goal().setX(0.0);
00097     goal().setY(0.5);
00098     goal().setYaw(0.0);
00099 
00100     # set the start and goal states
00101     ss.setStartAndGoalStates(start, goal, 0.05)
00102 
00103     # attempt to solve the problem
00104     solved = ss.solve(20.0)
00105 
00106     if solved:
00107         # print the path to screen
00108         print "Found solution:", ss.getSolutionPath().asGeometric()
00109 
00110 if __name__ == "__main__":
00111     plan()