DubinsAirplane.py
1 #!/usr/bin/env python3
2 
3 
36 
37 # Author: Mark Moll
38 
39 import sys
40 try:
41  from ompl import util as ou
42  from ompl import base as ob
43  from ompl import geometric as og
44 except ImportError:
45  # if the ompl module is not in the PYTHONPATH assume it is installed in a
46  # subdirectory of the parent directory called "py-bindings."
47  from os.path import abspath, dirname, join
48  sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'py-bindings'))
49  from ompl import util as ou
50  from ompl import base as ob
51  from ompl import geometric as og
52 import argparse
53 from math import fmod, sqrt
54 from typing import Union
55 
56 radius = 1.
57 maxPitch = .5
58 
59 def allocSpace(space: str):
60  bounds = ob.RealVectorBounds(3)
61  bounds.setLow(-10)
62  bounds.setHigh(10)
63  if space.lower() == "vana":
64  stateSpace = ob.VanaStateSpace(radius, maxPitch)
65  stateSpace.setBounds(bounds)
66  return stateSpace
67  elif space.lower() == "owen":
68  stateSpace = ob.OwenStateSpace(radius, maxPitch)
69  stateSpace.setBounds(bounds)
70  return stateSpace
71  else:
72  if space.lower() != "vanaowen":
73  ou.OMPL_WARN(f"Unknown state space {space}; defaulting to VanaOwen")
74  stateSpace = ob.VanaOwenStateSpace(radius, maxPitch)
75  stateSpace.setBounds(bounds)
76  return stateSpace
77 
78 def allocatePlanner(si: ob.SpaceInformation, planner: str):
79  if planner.lower() == "rrt":
80  return og.RRT(si)
81  elif planner.lower() == "rrtstar":
82  return og.RRTstar(si)
83  elif planner.lower() == "est":
84  return og.EST(si)
85  elif planner.lower() == "kpiece":
86  return og.KPIECE1(si)
87  elif planner.lower() == "sst":
88  return og.SST(si)
89  else:
90  ou.OMPL_ERROR(f"Planner type {planner} is not implemented in allocation function.")
91 
92 
93 def isStateValid(state: Union[ob.OwenState, ob.VanaState, ob.VanaOwenState]):
94  dist = 0.
95  for i in range(3):
96  d = fmod(abs(state[i]), 2. * radius) - radius
97  dist += d * d
98  return sqrt(dist) > .75 * radius
99 
100 
101 def plan(space : str, planner : str):
102  stateSpace = allocSpace(space)
103  start = ob.State(stateSpace)
104  goal = ob.State(stateSpace)
105  ss = og.SimpleSetup(stateSpace)
106  ss.setStateValidityChecker(ob.StateValidityCheckerFn(isStateValid))
107  while True:
108  start.random()
109  if isStateValid(start):
110  break
111  while True:
112  goal.random()
113  if isStateValid(goal):
114  break
115  ss.setStartAndGoalStates(start, goal)
116  si = ss.getSpaceInformation()
117  si.setStateValidityCheckingResolution(0.001)
118  ss.setPlanner(allocatePlanner(si, planner))
119  ss.setup()
120  print(ss)
121  result = ss.solve(10.0)
122  if result:
123  path = ss.getSolutionPath()
124  length = path.length()
125  path.interpolate()
126  print(path.printAsMatrix())
127 
128  if result == ob.PlannerStatus.APPROXIMATE_SOLUTION:
129  print("Approximate solution. Distance to goal is ", ss.getProblemDefinition().getSolutionDifference())
130  print("Path length is ", length)
131 
132 if __name__ == "__main__":
133  # Create an argument parser
134  parser = argparse.ArgumentParser(description='Optimal motion planning demo program.')
135 
136  # Add a filename argument
137  parser.add_argument('-s', '--space', default="VanaOwen", \
138  choices=['Owen', 'Vana', 'VanaOwen'], \
139  help='Type of 3D Dubins state space to use, defaults to vanaowen.')
140  parser.add_argument('-p', '--planner', default='KPIECE', \
141  choices=['RRT', 'RRTstar', 'EST', 'KPIECE', 'SST'], \
142  help='Planning algorithm to use, defaults to KPIECE if not given.')
143 
144  # Parse the arguments
145  args = parser.parse_args()
146  # Solve the planning problem
147  plan(args.space, args.planner)
Optimal Rapidly-exploring Random Trees.
Definition: RRTstar.h:138
An R^4 x SO(2) state space where distance is measured by the length of a type of Dubins airplane curv...
Definition of an abstract state.
Definition: State.h:113
Create the set of classes typically needed to solve a geometric problem.
Definition: SimpleSetup.h:126
An R^4 x SO(2) state space where distance is measured by the length of a type Dubins airplane curves.
Rapidly-exploring Random Trees.
Definition: RRT.h:129
An R^3 x SO(2) state space where distance is measured by the length of a type Dubins airplane curves.
Expansive Space Trees.
Definition: EST.h:129
std::function< bool(const State *)> StateValidityCheckerFn
If no state validity checking class is specified (StateValidityChecker), a std::function can be speci...
Kinematic Planning by Interior-Exterior Cell Exploration.
Definition: KPIECE1.h:135
The lower and upper bounds for an Rn space.