RigidBodyPlanningWithODESolverAndControls.py
1 #!/usr/bin/env python
2 
3 
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
A control space representing Rn.
A state space representing SE(2)
Definition: SE2StateSpace.h:49
Definition of an abstract state.
Definition: State.h:49
Basic solver for ordinary differential equations of the type q' = f(q, u), where q is the current sta...
Definition: ODESolver.h:195
The lower and upper bounds for an Rn space.
std::function< bool(const State *)> StateValidityCheckerFn
If no state validity checking class is specified (StateValidityChecker), a std::function can be speci...