Loading...
Searching...
No Matches
Point2DPlanning.py
1#!/usr/bin/env python3
2
3
36
37# Author: Ioan Sucan, Mark Moll, Weihang Guo
38
39from os.path import abspath, dirname, join
40from ompl import util as ou
41from ompl import base as ob
42from ompl import geometric as og
43from functools import partial
44
45
47 def __init__(self, ppm_file):
48 self.ppm_ = ou.PPM()
49 self.ppm_.loadFile(ppm_file)
51 space.addDimension(0.0, self.ppm_.getWidth())
52 space.addDimension(0.0, self.ppm_.getHeight())
53 self.maxWidth_ = self.ppm_.getWidth() - 1
54 self.maxHeight_ = self.ppm_.getHeight() - 1
55 self.ss_ = og.SimpleSetup(space)
56
57 # set state validity checking for this space
58 self.ss_.setStateValidityChecker(partial(Plane2DEnvironment.isStateValid, self))
59 space.setup()
60 self.ss_.getSpaceInformation().setStateValidityCheckingResolution(
61 1.0 / space.getMaximumExtent()
62 )
63 # self.ss_.setPlanner(og.RRTConnect(self.ss_.getSpaceInformation()))
64
65 def plan(self, start_row, start_col, goal_row, goal_col):
66 if not self.ss_:
67 return False
68 start = self.ss_.getStateSpace().allocState()
69 start[0] = start_row
70 start[1] = start_col
71 goal = self.ss_.getStateSpace().allocState()
72 goal[0] = goal_row
73 goal[1] = goal_col
74 self.ss_.setStartAndGoalStates(start, goal)
75 # generate a few solutions; all will be added to the goal
76 for _ in range(10):
77 if self.ss_.getPlanner():
78 self.ss_.getPlanner().clear()
79 self.ss_.solve()
80 ns = self.ss_.getProblemDefinition().getSolutionCount()
81 print("Found %d solutions" % ns)
82 if self.ss_.haveSolutionPath():
83 self.ss_.simplifySolution()
84 p = self.ss_.getSolutionPath()
85 ps = og.PathSimplifier(self.ss_.getSpaceInformation())
86 ps.simplifyMax(p)
87 ps.smoothBSpline(p)
88 return True
89 return False
90
91 def recordSolution(self):
92 if not self.ss_ or not self.ss_.haveSolutionPath():
93 return
94 p = self.ss_.getSolutionPath()
95 p.interpolate()
96 for i in range(p.getStateCount()):
97 w = min(self.maxWidth_, int(p.getState(i)[0]))
98 h = min(self.maxHeight_, int(p.getState(i)[1]))
99 c = self.ppm_.getPixel(h, w)
100 c.red = 255
101 c.green = 0
102 c.blue = 0
103
104 def save(self, filename):
105 if not self.ss_:
106 return
107 self.ppm_.saveFile(filename)
108
109 def isStateValid(self, state):
110 w = min(int(state[0]), self.maxWidth_)
111 h = min(int(state[1]), self.maxHeight_)
112
113 c = self.ppm_.getPixel(h, w)
114 return c.red > 127 and c.green > 127 and c.blue > 127
115
116
117if __name__ == "__main__":
118 fname = join(
119 join(
120 join(
121 join(dirname(dirname(dirname(abspath(__file__)))), "tests"), "resources"
122 ),
123 "ppm",
124 ),
125 "floor.ppm",
126 )
127 env = Plane2DEnvironment(fname)
128
129 if env.plan(0, 0, 777, 1265):
130 env.recordSolution()
131 env.save("result_demo.ppm")
A state space representing Rn. The distance function is the L2 norm.
This class contains routines that attempt to simplify geometric paths.
Create the set of classes typically needed to solve a geometric problem.
Definition SimpleSetup.h:63