All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
RigidBodyPlanningWithODESolverAndControls.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, tan
40 from functools import partial
41 try:
42  from ompl import base as ob
43  from ompl import control as oc
44  from ompl import geometric as og
45 except:
46  # if the ompl module is not in the PYTHONPATH assume it is installed in a
47  # subdirectory of the parent directory called "py-bindings."
48  from os.path import abspath, dirname, join
49  import sys
50  sys.path.insert(0, join(dirname(dirname(abspath(__file__))),'py-bindings'))
51  from ompl import base as ob
52  from ompl import control as oc
53  from ompl import geometric as og
54 
55 def kinematicCarODE(q, u, qdot):
56  theta = q[2];
57  carLength = 0.2;
58  qdot[0] = u[0] * cos(theta)
59  qdot[1] = u[0] * sin(theta)
60  qdot[2] = u[0] * tan(u[1]) / carLength
61 
62 
63 def isStateValid(spaceInformation, state):
64  # perform collision checking or check if other constraints are
65  # satisfied
66  return spaceInformation.satisfiesBounds(state)
67 
68 def plan():
69  # construct the state space we are planning in
70  space = ob.SE2StateSpace()
71 
72  # set the bounds for the R^2 part of SE(2)
73  bounds = ob.RealVectorBounds(2)
74  bounds.setLow(-1)
75  bounds.setHigh(1)
76  space.setBounds(bounds)
77 
78  # create a control space
79  cspace = oc.RealVectorControlSpace(space, 2)
80 
81  # set the bounds for the control space
82  cbounds = ob.RealVectorBounds(2)
83  cbounds.setLow(-.3)
84  cbounds.setHigh(.3)
85  cspace.setBounds(cbounds)
86 
87  # define a simple setup class
88  ss = oc.SimpleSetup(cspace)
89  validityChecker = ob.StateValidityCheckerFn(partial(isStateValid, ss.getSpaceInformation()))
90  ss.setStateValidityChecker(validityChecker)
91  ode = oc.ODE(kinematicCarODE)
92  odeSolver = oc.ODEBasicSolver(ss.getSpaceInformation(), ode)
93  propagator = oc.ODESolver.getStatePropagator(odeSolver)
94  ss.setStatePropagator(propagator)
95 
96  # create a start state
97  start = ob.State(space)
98  start().setX(-0.5);
99  start().setY(0.0);
100  start().setYaw(0.0);
101 
102  # create a goal state
103  goal = ob.State(space);
104  goal().setX(0.0);
105  goal().setY(0.5);
106  goal().setYaw(0.0);
107 
108  # set the start and goal states
109  ss.setStartAndGoalStates(start, goal, 0.05)
110 
111  # attempt to solve the problem
112  solved = ss.solve(120.0)
113 
114  if solved:
115  # print the path to screen
116  print("Found solution:\n%s" % ss.getSolutionPath().asGeometric().printAsMatrix())
117 
118 if __name__ == "__main__":
119  plan()
Create the set of classes typically needed to solve a control problem.
Definition: SimpleSetup.h:63
boost::function< bool(const State *)> StateValidityCheckerFn
If no state validity checking class is specified (StateValidityChecker), a boost function can be spec...
A control space representing Rn.
A state space representing SE(2)
Definition: SE2StateSpace.h:50
Definition of an abstract state.
Definition: State.h:50
Basic solver for ordinary differential equations of the type q' = f(q, u), where q is the current sta...
Definition: ODESolver.h:203
The lower and upper bounds for an Rn space.