All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
RigidBodyPlanningWithControls.py
1 #!/usr/bin/env python
2 
3 ######################################################################
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2010, Rice University
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of the Rice University nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 ######################################################################
36 
37 # Author: Mark Moll
38 
39 from math import sin, cos
40 from functools import partial
41 try:
42  from ompl import util as ou
43  from ompl import base as ob
44  from ompl import control as oc
45  from ompl import geometric as og
46 except:
47  # if the ompl module is not in the PYTHONPATH assume it is installed in a
48  # subdirectory of the parent directory called "py-bindings."
49  from os.path import basename, abspath, dirname, join
50  import sys
51  sys.path.insert(0, join(dirname(dirname(abspath(__file__))),'py-bindings'))
52  from ompl import util as ou
53  from ompl import base as ob
54  from ompl import control as oc
55  from ompl import geometric as og
56 
57 ## @cond IGNORE
58 # a decomposition is only needed for SyclopRRT and SyclopEST
59 class MyDecomposition(oc.GridDecomposition):
60  def __init__(self, length, bounds):
61  super(MyDecomposition, self).__init__(length, 2, bounds)
62  self.rng_ = ou.RNG()
63  def project(self, s, coord):
64  coord[0] = s.getX()
65  coord[1] = s.getY()
66  def sampleFromRegion(self, rid, sampler, s):
67  sampler.sampleUniform(s)
68  regionBounds = self.getRegionBounds(rid)
69  s.setX(self.rng_.uniformReal(regionBounds.low[0], regionBounds.high[0]))
70  s.setY(self.rng_.uniformReal(regionBounds.low[1], regionBounds.high[1]))
71 ## @endcond
72 
73 def isStateValid(spaceInformation, state):
74  # perform collision checking or check if other constraints are
75  # satisfied
76  return spaceInformation.satisfiesBounds(state)
77 
78 def propagate(start, control, duration, state):
79  state.setX( start.getX() + control[0] * duration * cos(start.getYaw()) )
80  state.setY( start.getY() + control[0] * duration * sin(start.getYaw()) )
81  state.setYaw(start.getYaw() + control[1] * duration)
82 
83 def plan():
84  # construct the state space we are planning in
85  space = ob.SE2StateSpace()
86 
87  # set the bounds for the R^2 part of SE(2)
88  bounds = ob.RealVectorBounds(2)
89  bounds.setLow(-1)
90  bounds.setHigh(1)
91  space.setBounds(bounds)
92 
93  # create a control space
94  cspace = oc.RealVectorControlSpace(space, 2)
95 
96  # set the bounds for the control space
97  cbounds = ob.RealVectorBounds(2)
98  cbounds.setLow(-.3)
99  cbounds.setHigh(.3)
100  cspace.setBounds(cbounds)
101 
102  # define a simple setup class
103  ss = oc.SimpleSetup(cspace)
104  ss.setStateValidityChecker(ob.StateValidityCheckerFn(partial(isStateValid, ss.getSpaceInformation())))
105  ss.setStatePropagator(oc.StatePropagatorFn(propagate))
106 
107  # create a start state
108  start = ob.State(space)
109  start().setX(-0.5);
110  start().setY(0.0);
111  start().setYaw(0.0);
112 
113  # create a goal state
114  goal = ob.State(space);
115  goal().setX(0.0);
116  goal().setY(0.5);
117  goal().setYaw(0.0);
118 
119  # set the start and goal states
120  ss.setStartAndGoalStates(start, goal, 0.05)
121 
122  # (optionally) set planner
123  si = ss.getSpaceInformation()
124  #planner = oc.RRT(si)
125  #planner = oc.EST(si)
126  #planner = oc.KPIECE1(si) # this is the default
127  # SyclopEST and SyclopRRT require a decomposition to guide the search
128  decomp = MyDecomposition(32, bounds)
129  planner = oc.SyclopEST(si, decomp)
130  #planner = oc.SyclopRRT(si, decomp)
131  ss.setPlanner(planner)
132  # (optionally) set propagation step size
133  si.setPropagationStepSize(.1)
134 
135  # attempt to solve the problem
136  solved = ss.solve(20.0)
137 
138  if solved:
139  # print the path to screen
140  print("Found solution:\n%s" % ss.getSolutionPath().asGeometric())
141 
142 if __name__ == "__main__":
143  plan()