Loading...
Searching...
No Matches
RigidBodyPlanningWithControls.py
1#!/usr/bin/env python3
2
3
36
37# Author: Mark Moll, Weihang Guo
38
39from math import sin, cos
40from functools import partial
41from ompl import base as ob
42from ompl import control as oc
43
44
45
47class MyDecomposition(oc.GridDecomposition):
48 def __init__(self, length, bounds):
49 super().__init__(length, 2, bounds)
50
51 def project(self, s, coord):
52 coord[0] = s.getX()
53 coord[1] = s.getY()
54
55 def sampleFullState(self, sampler, coord, s):
56 sampler.sampleUniform(s)
57 s.setXY(coord[0], coord[1])
58
59
60
61
62
63def isStateValid(space, state):
64 # perform collision checking or check if other constraints are
65 # satisfied
66 return space.satisfiesBounds(state)
67
68
69def propagate(start, control, duration, state):
70 state.setX(start.getX() + control[0] * duration * cos(start.getYaw()))
71 state.setY(start.getY() + control[0] * duration * sin(start.getYaw()))
72 state.setYaw(start.getYaw() + control[1] * duration)
73
74
75def plan():
76 # construct the state space we are planning in
77 space = ob.SE2StateSpace()
78
79 # set the bounds for the R^2 part of SE(2)
80 bounds = ob.RealVectorBounds(2)
81 bounds.setLow(-1)
82 bounds.setHigh(1)
83 space.setBounds(bounds)
84
85 # create a control space
86 cspace = oc.RealVectorControlSpace(space, 2)
87
88 # set the bounds for the control space
89 cbounds = ob.RealVectorBounds(2)
90 cbounds.setLow(-0.3)
91 cbounds.setHigh(0.3)
92 cspace.setBounds(cbounds)
93
94 # define a simple setup class
95 ss = oc.SimpleSetup(cspace)
96 ss.setStateValidityChecker(partial(isStateValid, space))
97 ss.setStatePropagator(propagate)
98
99 # create a start state
100 start = ss.getStateSpace().allocState()
101 start.setX(-0.5)
102 start.setY(0.0)
103 start.setYaw(0.0)
104
105 # create a goal state
106 goal = ss.getStateSpace().allocState()
107 goal.setX(0.0)
108 goal.setY(0.5)
109 goal.setYaw(0.0)
110
111 # set the start and goal states
112 ss.setStartAndGoalStates(start, goal, 0.05)
113
114 # (optionally) set planner
115 si = ss.getSpaceInformation()
116 # planner = oc.RRT(si)
117 # planner = oc.EST(si)
118 # planner = oc.KPIECE1(si) # this is the default
119 # SyclopEST and SyclopRRT require a decomposition to guide the search
120 decomp = MyDecomposition(32, bounds)
121 planner = oc.SyclopEST(si, decomp)
122 # planner = oc.SyclopRRT(si, decomp)
123 ss.setPlanner(planner)
124 # (optionally) set propagation step size
125 si.setPropagationStepSize(0.1)
126
127 # attempt to solve the problem
128 solved = ss.solve(1.0)
129 if solved == ob.PlannerStatus.EXACT_SOLUTION:
130 # print the path to screen
131 print("Found solution:\n")
132 ss.getSolutionPath().printAsMatrix()
133
134
135if __name__ == "__main__":
136 plan()
The lower and upper bounds for an Rn space.
A state space representing SE(2).
A GridDecomposition is a Decomposition implemented using a grid.
A control space representing Rn.
Create the set of classes typically needed to solve a control problem.
Definition SimpleSetup.h:63
SyclopEST is Syclop with EST as its low-level tree planner. .
Definition SyclopEST.h:52