-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnaive_rrt_path.py
229 lines (187 loc) · 6.63 KB
/
naive_rrt_path.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
'''Planning according to naive RRT.
'''
import numpy as np
import pybullet as p
from scipy import stats, optimize, special
import matplotlib.pyplot as plt
import seaborn as sns
try:
from ompl import base as ob
from ompl import geometric as og
except ImportError:
print("Could not find OMPL")
raise ImportError("Run inside docker!!")
# Import project specific files
from models import point
from config import box_width, box_length, xy, cir_radius
obstacles, robot = point.set_env()
# Define LTI system
A, B, M, N= point.get_dyn()
# Noise model
N_n = stats.multivariate_normal(cov=N) # Observation Noise
M_n = stats.multivariate_normal(cov=M) # Motion Noise
# Define gamma function for evaluating path
S = lambda x: special.gammainc(1, x)
# Threshold for each point.
thresh = 0.1
fig, ax = plt.subplots()
sns.set()
ax.set_xlim((-0.2, 10.2))
ax.set_ylim((-0.2, 10.2))
# Initialize the position of obstacles
dimensions = [box_length, box_width]
rectangle_corner = np.r_[(-dimensions[0]/2, -dimensions[1]/2)]
for xy_i in point.xy_circle:
plt_cir = plt.Circle(xy_i, radius=cir_radius, color='r')
ax.add_patch(plt_cir)
for xy_i in point.xy:
plt_box = plt.Rectangle(xy_i+rectangle_corner, dimensions[0], dimensions[1], color='r')
ax.add_patch(plt_box)
class ValidityCheckerDistance(ob.StateValidityChecker):
'''A class to check the validity of the state, by checking distance function
'''
defaultOrientation = p.getQuaternionFromEuler([0., 0., 0.])
def isValid(self, state):
'''
Check if the given state is valid.
:param state: An ob.STate object to be checked.
:return bool: True if the state is valid.
'''
return self.getDistance(state)>0
def getDistance(self, state):
'''
Get the shortest distance from robot to obstacle.
:param x: A numpy array of state x.
:returns float: The closest distance between the robot and obstacle.
'''
p.resetBasePositionAndOrientation(robot, np.r_[state.getX(), state.getY(), 0.1], self.defaultOrientation)
return point.get_distance(obstacles, robot)
# Define the space
space = ob.SE2StateSpace()
# Set the bounds
bounds = ob.RealVectorBounds(2)
bounds.setLow(-0.2)
bounds.setHigh(10)
space.setBounds(bounds)
# Define the start and goal location
start = ob.State(space)
# start.random()
# while not ValidityChecker_obj.isValid(start()):
# start.random()
# start[0] = 1.0
# start[1] = 8.0
goal = ob.State(space)
goal[0] = 8.5
goal[1] = 8.5
# Function to get the distribution of path.
A = A[:2, :2]
B = B[:2, :2]
# LQG parameter:
C = np.eye(2)
D = np.eye(2)
Q = np.block([
[M, np.zeros((2,2))],
[np.zeros((2,2)), N]
])
# try:
# path = np.load("path_lqg.npy")
# path_interpolated = np.load("path_interpolated_lqg.npy")
# # start[0] = path[0,0]
# # start[1] = path[0,1]
# except FileNotFoundError:
def get_path(start, goal):
'''
Get a RRT path from start and goal.
:param start: og.State object.
:param goal: og.State object.
returns (np.array, np.array): A tuple of numpy arrays of a valid path and
interpolated path.
'''
# Define the SpaceInformation object.
si = ob.SpaceInformation(space)
ValidityChecker_obj = ValidityCheckerDistance(si)
si.setStateValidityChecker(ValidityChecker_obj)
# Create a simple setup
ss = og.SimpleSetup(si)
# Set the start and goal states:
ss.setStartAndGoalStates(start, goal)
# define the planner
planner = og.RRT(si)
ss.setPlanner(planner)
# Attempt to solve within the given time
solved = ss.solve(10.0)
if solved:
print("Found solution")
path = [
[ss.getSolutionPath().getState(i).getX(), ss.getSolutionPath().getState(i).getY()]
for i in range(ss.getSolutionPath().getStateCount())
]
ss.getSolutionPath().interpolate(100)
path_obj = ss.getSolutionPath()
path_interpolated = np.array([
[path_obj.getState(i).getX(), path_obj.getState(i).getY()]
for i in range(path_obj.getStateCount())
])
else:
path = []
path_interpolated = []
return np.array(path), np.array(path_interpolated)
import os
import pickle
def start_experiment():
'''
Run the LQG experiment for the start and goal points for the same RRT-paths
'''
# Define path
folder_loc = '/root/data'
# for file_i in os.listdir(folder_loc):
for i in range(350, 400):
# if '.p' in file_i:
file_i = 'path_{}.p'.format(i)
data = pickle.load(open(os.path.join(folder_loc, file_i), 'rb'))
start_array = data['path'][0]
goal_array = data['path'][-1]
start = ob.State(space)
start[0] = start_array[0]
start[1] = start_array[1]
goal = ob.State(space)
goal[0] = goal_array[0]
goal[1] = goal_array[1]
path_param = {}
path, path_interpolated = get_path(start, goal)
path_param['path'] = path
path_param['path_interpolated'] = path_interpolated
# Evaluate 100 paths
accuracy = 0
si_check = ob.SpaceInformation(space)
ValidityChecker_dis_obj = ValidityCheckerDistance(si_check)
si_check.setStateValidityChecker(ValidityChecker_dis_obj)
for _ in range(100):
_, _, done = point.execute_path(path_interpolated, C, D, si_check)
if done:
accuracy += 1
path_param['accuracy'] = accuracy
pickle.dump(path_param, open(os.path.join(folder_loc, 'rrt', file_i), 'wb'))
if __name__ == "__main__":
start_experiment()
if False:
ax.scatter(start[0], start[1], color='r', marker='x')
ax.scatter(goal[0], goal[1], color='g', marker='o')
if path!=[]:
ax.plot(path[:,0], path[:,1], color='b', alpha=0.5, label='RRT path')
ax.scatter(path[:,0], path[:,1], color='b', label='RRT path')
# Evaluate 10 paths
successfull = 0
si_check = ob.SpaceInformation(space)
ValidityChecker_obj = ValidityCheckerDistance(si_check)
si_check.setStateValidityChecker(ValidityChecker_obj)
for _ in range(10):
path_est, path_noisy, done = point.execute_path(path_interpolated, C, D, si_check)
# if not done:
path_est = np.array(path_est)
# ax.plot(path_est[:,0], path_est[:,1], color='r', label='estimated state')
path_noisy = np.array(path_noisy)
ax.plot(path_noisy[:,0], path_noisy[:,1], '--',color='g', alpha=0.5, label='real state')
# else:
# successfull+=1
print("Total succesful paths {}".format(successfull))