Repository navigation
Expand file tree
/
Copy pathplanner.py
More file actions
130 lines (99 loc) · 4.25 KB
/
Copy pathplanner.py
File metadata and controls
130 lines (99 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pybullet as p
import config
class OMPLPlanner:
"""OMPL-based planner."""
def __init__(self, robot_id, movable_joints, ignored_ids=None):
self.robot_id = robot_id
self.joints = movable_joints
self.ndof = len(movable_joints)
self.ignored_ids = ignored_ids or []
self.locked_joint_values = {}
from ompl import base as ob
from ompl import geometric as og
self.ob = ob
self.og = og
def _get_joint_bounds(self, joint_idx):
info = p.getJointInfo(self.robot_id, joint_idx)
joint_name = info[1].decode("utf-8")
lower, upper = info[8], info[9]
if "mobile_joint_x" in joint_name or ("x" in joint_name and "base" in joint_name):
return config.MOBILE_BASE_BOUNDS
if "mobile_joint_y" in joint_name or ("y" in joint_name and "base" in joint_name):
return config.MOBILE_BASE_BOUNDS
if lower >= upper:
return -config.DEFAULT_JOINT_LIMIT, config.DEFAULT_JOINT_LIMIT
if abs(upper - lower) < 1e-5:
return lower, upper + 0.01
return lower, upper
def is_state_valid(self, state):
for i, joint_idx in enumerate(self.joints):
p.resetJointState(self.robot_id, joint_idx, state[i])
p.performCollisionDetection()
for body_id in range(p.getNumBodies()):
if body_id == self.robot_id or body_id in self.ignored_ids:
continue
if p.getClosestPoints(self.robot_id, body_id, config.COLLISION_MARGIN):
return False
return True
def _setup_space(self):
space = self.ob.RealVectorStateSpace(self.ndof)
bounds = self.ob.RealVectorBounds(self.ndof)
for i, joint_idx in enumerate(self.joints):
if i in self.locked_joint_values:
val = self.locked_joint_values[i]
bounds.setLow(i, val - 1e-6)
bounds.setHigh(i, val + 1e-6)
else:
lower, upper = self._get_joint_bounds(joint_idx)
bounds.setLow(i, lower)
bounds.setHigh(i, upper)
space.setBounds(bounds)
self.space = space
self.si = self.ob.SpaceInformation(space)
self.si.setStateValidityChecker(self.ob.StateValidityCheckerFn(self.is_state_valid))
self.si.setup()
def _create_state(self, configuration):
state = self.ob.State(self.space)
for i in range(self.ndof):
state[i] = configuration[i]
return state
def _extract_path(self, solution_path):
solution_path.interpolate(config.PATH_INTERPOLATION_STEPS)
return [
[solution_path.getState(i)[j] for j in range(self.ndof)]
for i in range(solution_path.getStateCount())
]
def plan(self, start_conf, goal_conf, timeout=None, locked_indices=None):
if timeout is None:
timeout = config.PLANNING_TIMEOUT
self.locked_joint_values = {}
if locked_indices:
for i in locked_indices:
self.locked_joint_values[i] = start_conf[i]
self._setup_space()
start = self._create_state(start_conf)
goal = self._create_state(goal_conf)
pdef = self.ob.ProblemDefinition(self.si)
pdef.setStartAndGoalStates(start, goal)
planner = self.og.RRTstar(self.si)
planner.setRange(config.PLANNER_RANGE)
planner.setProblemDefinition(pdef)
planner.setup()
p.configureDebugVisualizer(p.COV_ENABLE_RENDERING, 0)
state_id = p.saveState()
print("OMPL: Planning...")
solved = planner.solve(timeout)
p.restoreState(state_id)
p.configureDebugVisualizer(p.COV_ENABLE_RENDERING, 1)
if solved:
print("OMPL: Path Found!")
return self._extract_path(pdef.getSolutionPath())
print("OMPL: No Path Found.")
return None
def create_planner(robot_id, movable_joints, ignored_ids=None):
"""Factory function to create planner based on config."""
backend = getattr(config, 'PLANNER_BACKEND', 'custom')
if backend == 'ompl':
return OMPLPlanner(robot_id, movable_joints, ignored_ids)
from rrt_star import RRTStarPlanner
return RRTStarPlanner(robot_id, movable_joints, ignored_ids)