-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpb_ompl.py
289 lines (245 loc) · 10.2 KB
/
pb_ompl.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
try:
from ompl import util as ou
from ompl import base as ob
from ompl import geometric as og
except ImportError:
# if the ompl module is not in the PYTHONPATH assume it is installed in a
# subdirectory of the parent directory called "py-bindings."
from os.path import abspath, dirname, join
import sys
sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'ompl/py-bindings'))
# sys.path.insert(0, join(dirname(abspath(__file__)), '../whole-body-motion-planning/src/ompl/py-bindings'))
print(sys.path)
from ompl import util as ou
from ompl import base as ob
from ompl import geometric as og
import pybullet as p
import utils
import time
from itertools import product
import copy
INTERPOLATE_NUM = 500
DEFAULT_PLANNING_TIME = 5.0
class PbOMPLRobot():
'''
To use with Pb_OMPL. You need to construct a instance of this class and pass to PbOMPL.
Note:
This parent class by default assumes that all joints are acutated and should be planned. If this is not your desired
behaviour, please write your own inheritated class that overrides respective functionalities.
'''
def __init__(self, id) -> None:
# Public attributes
self.id = id
# prune fixed joints
all_joint_num = p.getNumJoints(id)
all_joint_idx = list(range(all_joint_num))
joint_idx = [j for j in all_joint_idx if self._is_not_fixed(j)]
self.num_dim = len(joint_idx)
self.joint_idx = joint_idx
print(self.joint_idx)
self.joint_bounds = []
self.reset()
def _is_not_fixed(self, joint_idx):
joint_info = p.getJointInfo(self.id, joint_idx)
return joint_info[2] != p.JOINT_FIXED
def get_joint_bounds(self):
'''
Get joint bounds.
By default, read from pybullet
'''
for i, joint_id in enumerate(self.joint_idx):
joint_info = p.getJointInfo(self.id, joint_id)
low = joint_info[8] # low bounds
high = joint_info[9] # high bounds
if low < high:
self.joint_bounds.append([low, high])
print("Joint bounds: {}".format(self.joint_bounds))
return self.joint_bounds
def get_cur_state(self):
return copy.deepcopy(self.state)
def set_state(self, state):
'''
Set robot state.
To faciliate collision checking
Args:
state: list[Float], joint values of robot
'''
self._set_joint_positions(self.joint_idx, state)
self.state = state
def reset(self):
'''
Reset robot state
Args:
state: list[Float], joint values of robot
'''
state = [0] * self.num_dim
self._set_joint_positions(self.joint_idx, state)
self.state = state
def _set_joint_positions(self, joints, positions):
for joint, value in zip(joints, positions):
p.resetJointState(self.id, joint, value, targetVelocity=0)
class PbStateSpace(ob.RealVectorStateSpace):
def __init__(self, num_dim) -> None:
super().__init__(num_dim)
self.num_dim = num_dim
self.state_sampler = None
def allocStateSampler(self):
'''
This will be called by the internal OMPL planner
'''
# WARN: This will cause problems if the underlying planner is multi-threaded!!!
if self.state_sampler:
return self.state_sampler
# when ompl planner calls this, we will return our sampler
return self.allocDefaultStateSampler()
def set_state_sampler(self, state_sampler):
'''
Optional, Set custom state sampler.
'''
self.state_sampler = state_sampler
class PbOMPL():
def __init__(self, robot, obstacles = []) -> None:
'''
Args
robot: A PbOMPLRobot instance.
obstacles: list of obstacle ids. Optional.
'''
self.robot = robot
self.robot_id = robot.id
self.obstacles = obstacles
print(self.obstacles)
self.space = PbStateSpace(robot.num_dim)
bounds = ob.RealVectorBounds(robot.num_dim)
joint_bounds = self.robot.get_joint_bounds()
for i, bound in enumerate(joint_bounds):
bounds.setLow(i, bound[0])
bounds.setHigh(i, bound[1])
self.space.setBounds(bounds)
self.ss = og.SimpleSetup(self.space)
self.ss.setStateValidityChecker(ob.StateValidityCheckerFn(self.is_state_valid))
self.si = self.ss.getSpaceInformation()
# self.si.setStateValidityCheckingResolution(0.005)
# self.collision_fn = pb_utils.get_collision_fn(self.robot_id, self.robot.joint_idx, self.obstacles, [], True, set(),
# custom_limits={}, max_distance=0, allow_collision_links=[])
self.set_obstacles(obstacles)
self.set_planner("RRT") # RRT by default
def set_obstacles(self, obstacles):
self.obstacles = obstacles
# update collision detection
self.setup_collision_detection(self.robot, self.obstacles)
def add_obstacles(self, obstacle_id):
self.obstacles.append(obstacle_id)
def remove_obstacles(self, obstacle_id):
self.obstacles.remove(obstacle_id)
def is_state_valid(self, state):
# satisfy bounds TODO
# Should be unecessary if joint bounds is properly set
# check self-collision
self.robot.set_state(self.state_to_list(state))
for link1, link2 in self.check_link_pairs:
if utils.pairwise_link_collision(self.robot_id, link1, self.robot_id, link2):
# print(get_body_name(body), get_link_name(body, link1), get_link_name(body, link2))
return False
# check collision against environment
for body1, body2 in self.check_body_pairs:
if utils.pairwise_collision(body1, body2):
# print('body collision', body1, body2)
# print(get_body_name(body1), get_body_name(body2))
return False
return True
def setup_collision_detection(self, robot, obstacles, self_collisions = True, allow_collision_links = []):
self.check_link_pairs = utils.get_self_link_pairs(robot.id, robot.joint_idx) if self_collisions else []
moving_links = frozenset(
[item for item in utils.get_moving_links(robot.id, robot.joint_idx) if not item in allow_collision_links])
moving_bodies = [(robot.id, moving_links)]
self.check_body_pairs = list(product(moving_bodies, obstacles))
def set_planner(self, planner_name):
'''
Note: Add your planner here!!
'''
if planner_name == "PRM":
self.planner = og.PRM(self.ss.getSpaceInformation())
elif planner_name == "RRT":
self.planner = og.RRT(self.ss.getSpaceInformation())
elif planner_name == "RRTConnect":
self.planner = og.RRTConnect(self.ss.getSpaceInformation())
elif planner_name == "RRTstar":
self.planner = og.RRTstar(self.ss.getSpaceInformation())
elif planner_name == "EST":
self.planner = og.EST(self.ss.getSpaceInformation())
elif planner_name == "FMT":
self.planner = og.FMT(self.ss.getSpaceInformation())
elif planner_name == "BITstar":
self.planner = og.BITstar(self.ss.getSpaceInformation())
else:
print("{} not recognized, please add it first".format(planner_name))
return
self.ss.setPlanner(self.planner)
def plan_start_goal(self, start, goal, allowed_time = DEFAULT_PLANNING_TIME):
'''
plan a path to gaol from the given robot start state
'''
print("start_planning")
print(self.planner.params())
orig_robot_state = self.robot.get_cur_state()
# set the start and goal states;
s = ob.State(self.space)
g = ob.State(self.space)
for i in range(len(start)):
s[i] = start[i]
g[i] = goal[i]
self.ss.setStartAndGoalStates(s, g)
# attempt to solve the problem within allowed planning time
solved = self.ss.solve(allowed_time)
res = False
sol_path_list = []
if solved:
print("Found solution: interpolating into {} segments".format(INTERPOLATE_NUM))
# print the path to screen
sol_path_geometric = self.ss.getSolutionPath()
sol_path_geometric.interpolate(INTERPOLATE_NUM)
sol_path_states = sol_path_geometric.getStates()
sol_path_list = [self.state_to_list(state) for state in sol_path_states]
# print(len(sol_path_list))
# print(sol_path_list)
for sol_path in sol_path_list:
self.is_state_valid(sol_path)
res = True
else:
print("No solution found")
# reset robot state
self.robot.set_state(orig_robot_state)
return res, sol_path_list
def plan(self, goal, allowed_time = DEFAULT_PLANNING_TIME):
'''
plan a path to gaol from current robot state
'''
start = self.robot.get_cur_state()
return self.plan_start_goal(start, goal, allowed_time=allowed_time)
def execute(self, path, dynamics=False):
'''
Execute a planned plan. Will visualize in pybullet.
Args:
path: list[state], a list of state
dynamics: allow dynamic simulation. If dynamics is false, this API will use robot.set_state(),
meaning that the simulator will simply reset robot's state WITHOUT any dynamics simulation. Since the
path is collision free, this is somewhat acceptable.
'''
for q in path:
if dynamics:
for i in range(self.robot.num_dim):
p.setJointMotorControl2(self.robot.id, i, p.POSITION_CONTROL, q[i],force=5 * 240.)
else:
self.robot.set_state(q)
p.stepSimulation()
time.sleep(0.01)
# -------------
# Configurations
# ------------
def set_state_sampler(self, state_sampler):
self.space.set_state_sampler(state_sampler)
# -------------
# Util
# ------------
def state_to_list(self, state):
return [state[i] for i in range(self.robot.num_dim)]