Loading...
Searching...
No Matches
RigidBodyPlanning.py
1#!/usr/bin/env python3
2
3
36
37# Author: Mark Moll, Weihang Guo
38
39from ompl import base as ob
40from ompl import geometric as og
41
42
43def isStateValid(state):
44 # Some arbitrary condition on the state (note that thanks to
45 # dynamic type checking we can just call getX() and do not need
46 # to convert state to an SE2State.)
47 return state.getX() < 0.6
48
49
50def planWithSimpleSetup():
51 # create an SE2 state space
52 space = ob.SE2StateSpace()
53
54 # set lower and upper bounds
55 bounds = ob.RealVectorBounds(2)
56 bounds.setLow(-1)
57 bounds.setHigh(1)
58 space.setBounds(bounds)
59
60 # create a simple setup object
61 ss = og.SimpleSetup(space)
62 ss.setStateValidityChecker(isStateValid)
63
64 sampler = ss.getStateSpace().allocDefaultStateSampler()
65 start = ss.getStateSpace().allocState()
66 # we can pick a random start state...
67 sampler.sampleUniform(start)
68 # ... or set specific values
69 start.setX(0.5)
70
71 goal = ss.getStateSpace().allocState()
72 # we can pick a random goal state...
73 sampler.sampleUniform(goal)
74 # ... or set specific values
75 goal.setX(-0.5)
76
77 ss.setStartAndGoalStates(start, goal)
78
79 # this will automatically choose a default planner with
80 # default parameters
81 solved = ss.solve(1.0)
82
83 if solved:
84 # try to shorten the path
85 ss.simplifySolution()
86 # print the simplified path
87 print(ss.getSolutionPath())
88
89
90def planTheHardWay():
91 # create an SE2 state space
92 space = ob.SE2StateSpace()
93 # set lower and upper bounds
94 bounds = ob.RealVectorBounds(2)
95 bounds.setLow(-1)
96 bounds.setHigh(1)
97 space.setBounds(bounds)
98 # construct an instance of space information from this state space
99 si = ob.SpaceInformation(space)
100 # set state validity checking for this space
101 si.setStateValidityChecker(isStateValid)
102
103 sampler = si.getStateSpace().allocDefaultStateSampler()
104 # create a random start state
105 start = si.getStateSpace().allocState()
106 sampler.sampleUniform(start)
107 # create a random goal state
108 goal = si.getStateSpace().allocState()
109 sampler.sampleUniform(goal)
110 # create a problem instance
111 pdef = ob.ProblemDefinition(si)
112 # set the start and goal states
113 pdef.setStartAndGoalStates(start, goal)
114 # create a planner for the defined space
115 planner = og.RRTConnect(si)
116 # set the problem we are trying to solve for the planner
117 planner.setProblemDefinition(pdef)
118 # perform setup steps for the planner
119 planner.setup()
120 # print the settings for this space
121 si.printSettings()
122 # print the problem settings
123 print(pdef)
124 # attempt to solve the problem within one second of planning time
125 solved = planner.solve(1.0)
126
127 if solved:
128 # get the goal representation from the problem definition (not the same as the goal state)
129 # and inquire about the found path
130 path = pdef.getSolutionPath()
131 print("Found solution:\n%s" % path)
132 else:
133 print("No solution found")
134
135
136if __name__ == "__main__":
137 planWithSimpleSetup()
138 print("")
139 planTheHardWay()
Definition of a problem to be solved. This includes the start state(s) for the system and a goal spec...
The lower and upper bounds for an Rn space.
A state space representing SE(2).
The base class for space information. This contains all the information about the space planning is d...
RRT-Connect (RRTConnect).
Definition RRTConnect.h:62
Create the set of classes typically needed to solve a geometric problem.
Definition SimpleSetup.h:63