-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_plan_rrt.py
476 lines (349 loc) · 15.2 KB
/
motion_plan_rrt.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import sys
import argparse
import time
import msgpack
from enum import Enum, auto
import numpy as np
import decimal
# This file is subject to the terms and conditions defined in
# file 'LICENSE', which is part of this source code package.
from operator import itemgetter
#from rrt import create_grid as grid_gen
#from planning_utils import a_star, heuristic
#from rrt import create_grid, rrt_vertices
#from rrt import create_grid
#from planning_utils import create_grid
# from planning_utils import a_star, heuristic, create_grid
from udacidrone import Drone
from udacidrone.connection import MavlinkConnection
from udacidrone.messaging import MsgID
from udacidrone.frame_utils import global_to_local
# coding: utf-8
# # Rapidly-Exploring Random Tree (RRT)
#
# Your task is to generate an RRT based on the following pseudocode:
#
# ```
# def generate_RRT(x_init, num_vertices, dt):
# rrt = RRT(x_init)
# for k in range(num_vertices):
# x_rand = sample_state()
# x_near = nearest_neighbor(x_rand, rrt)
# u = select_input(x_rand, x_near)
# x_new = new_state(x_near, u, dt)
# # directed edge
# rrt.add_edge(x_near, x_new, u)
# return rrt
# ```
#
# The `RRT` class has already been implemented. Your task is to complete the implementation of the following functions:
#
# * `sample_state`
# * `nearest_neighbor`
# * `select_input`
# * `new_state`
#
import numpy as np
import matplotlib
#matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from sklearn.neighbors import KDTree
import networkx as nx
from IPython import get_ipython
import time
from enum import Enum
from queue import PriorityQueue
import sys
get_ipython().run_line_magic('matplotlib', 'inline')
plt.switch_backend('Qt5agg')
plt.rcParams['figure.figsize'] = 12, 12
class RRT:
def __init__(self, x_init):
# A tree is a special case of a graph with
# directed edges and only one path to any vertex.
self.tree = nx.DiGraph()
self.tree.add_node(self.x_init)
self.x_init = x_init(tuple)
def add_vertex(self, x_new):
self.tree.add_node(tuple(self.x_init))
def add_edge(self, x_near, x_new, u):
self.tree.add_edge(tuple(x_near), tuple(x_new), orientation=u)
@property
def vertices(self):
return self.tree.nodes()
@property
def edges(self):
return self.tree.edges()
def create_grid(self, data, drone_altitude, safety_distance):
"""
Returns a grid representation of a 2D configuration space
based on given obstacle data, drone altitude and safety distance
arguments.
"""
# minimum and maximum north coordinates
north_min = np.floor(np.min(data[:, 0] - data[:, 3]))
north_max = np.ceil(np.max(data[:, 0] + data[:, 3]))
# minimum and maximum east coordinates
east_min = np.floor(np.min(data[:, 1] - data[:, 4]))
east_max = np.ceil(np.max(data[:, 1] + data[:, 4]))
# given the minimum and maximum coordinates we can
# calculate the size of the grid.
north_size = int(np.ceil(north_max - north_min))
east_size = int(np.ceil(east_max - east_min))
# Initialize an empty grid
grid = np.zeros((north_size, east_size))
# Populate the grid with obstacles
for i in range(data.shape[0]):
north, east, alt, d_north, d_east, d_alt = data[i, :]
if alt + d_alt + safety_distance > drone_altitude:
obstacle = [
int(np.clip(north - d_north - safety_distance - north_min, 0, north_size-1)),
int(np.clip(north + d_north + safety_distance - north_min, 0, north_size-1)),
int(np.clip(east - d_east - safety_distance - east_min, 0, east_size-1)),
int(np.clip(east + d_east + safety_distance - east_min, 0, east_size-1)),
]
grid[obstacle[0]:obstacle[1]+1, obstacle[2]:obstacle[3]+1] = 1
# ~print('INFO', grid, drone_altitude, safety_distance)
# ~print(grid, int(north_min), int(east_min))
#print(grid, drone_altitude, safety_distance)
#print(grid, int(north_min), int(east_min))
return grid, int(north_min), int(east_min)
def sample_state(self, grid):
x = np.random.uniform(0, grid.shape[0])
y = np.random.uniform(0, grid.shape[1])
return (x, y)
# ### Nearest Neighbors
#
# A critical part of the RRT procedure is finding the closest vertex to the sampled random point. This the most computationally intensive part so be mindful of that. Depending on the number of vertices a naive implementation will run into trouble quickly.
def nearest_neighbor(self, x_rand, rrt):
x_goal = (30, 750)
wp_radius = np.linalg.norm(x_goal)
print ('waypoint radius', wp_radius)
closest_dist = 100000
closest_vertex = None
x_rand = np.array(x_rand)
x_goal = ( 30,750)
print ("Generating RRT")
for v in rrt.vertices:
d = np.linalg.norm(x_rand - np.array(v[:2]))
if d < closest_dist:
closest_dist = d
closest_vertex = v
beans = np.array(v[:2])
spinach = x_goal - np.array(v[:2])
'''
print ("x_goal", x_goal)
print ("np.array",beans)
print ("matrix_norm", spinach)
print ("np.array", beans)
print ("x_rand", x_rand)
'''
'''
print ("matrix_norm", spinach)
print ("np.array", beans)
print ("x_rand", x_rand)
print ("np.array",)'''
# ~arrive at goal
# spinach = np.linalg.norm(v[:2] - x_goal)
# beans = np.array[np.linalg.norm(v[:2])]
#if np.linalg.norm(v[:2] - x_goal) < 1.0:
if np.linalg.norm(spinach) < 1.0:
print("Found Goal")
break
'print (np.array(v[:2])'
print(bool(np.linalg.norm(spinach) < 1.0))
return closest_vertex
# ### Selecting Inputs
#
# Select input which moves `x_near` closer to `x_rand`. This should return the angle or orientation of the vehicle.
def select_input(self, x_rand, x_near):
return np.arctan2(x_rand[1] - x_near[1], x_rand[0] - x_near[0])
# ### New State
#
#
# The new vertex `x_new` is calculated by travelling from the current vertex `x_near` with a orientation `u` for time `dt`.
def new_state(self, x_near, u, dt):
nx = x_near[0] + np.cos(u)*dt
ny = x_near[1] + np.sin(u)*dt
return [nx, ny]
# ### Putting It All Together
#
# Awesome! Now we'll put everything together and generate an RRT.
def generate_RRT(self, grid, x_init, num_vertices, dt,):
'print ("Generating RRT...")'
rrt = RRT(x_init)
for _ in range(num_vertices):
x_rand = self.sample_state(grid)
# sample states until a free state is found
while grid[int(x_rand[0]), int(x_rand[1])] == 1:
x_rand = self.sample_state(grid)
x_near = self.nearest_neighbor(x_rand, rrt)
u = self.select_input(x_rand, x_near)
x_new = self.new_state(x_near, u, dt)
if grid[int(x_new[0]), int(x_new[1])] == 0:
# the orientation `u` will be added as metadata to
# the edge
rrt.add_edge(x_near, x_new, u)
print ("RRT Path Mapped")
return rrt
class States(Enum):
MANUAL = auto()
ARMING = auto()
TAKEOFF = auto()
WAYPOINT = auto()
LANDING = auto()
DISARMING = auto()
PLANNING = auto()
class MotionPlanning(Drone):
def __init__(self, connection):
super().__init__(connection)
self.target_position = np.array([0.0, 0.0, 0.0])
self.waypoints = []
self.in_mission = True
self.check_state = {}
# initial state
self.flight_state = States.MANUAL
# register all your callbacks here
self.register_callback(MsgID.LOCAL_POSITION, self.local_position_callback)
self.register_callback(MsgID.LOCAL_VELOCITY, self.velocity_callback)
self.register_callback(MsgID.STATE, self.state_callback)
def local_position_callback(self):
self.plan_rrt()
print ('local vel norm', np.linalg.norm(self.local_velocity[0:2]))
if self.flight_state == States.TAKEOFF:
if -1.0 * self.local_position[2] > 0.95 * self.target_position[2]:
self.waypoint_transition()
elif self.flight_state == States.WAYPOINT:
if np.linalg.norm(self.target_position[0:2] - self.local_position[0:2]) < 1.0:
if len(self.waypoints) > 0:
self.waypoint_transition()
else:
if np.linalg.norm(self.local_velocity[0:2]) < 1.0:
self.landing_transition()
def velocity_callback(self):
if self.flight_state == States.LANDING:
if self.global_position[2] - self.global_home[2] < 0.1:
if abs(self.local_position[2]) < 0.01:
self.disarming_transition()
def state_callback(self):
if self.in_mission:
if self.flight_state == States.MANUAL:
self.arming_transition()
elif self.flight_state == States.ARMING:
if self.armed:
sys.exit('rrt is next')
self.plan_rrt()
elif self.flight_state == States.PLANNING:
self.takeoff_transition()
elif self.flight_state == States.DISARMING:
if ~self.armed & ~self.guided:
self.manual_transition()
def arming_transition(self):
self.flight_state = States.ARMING
print("arming transition")
self.arm()
self.take_control()
def takeoff_transition(self):
self.flight_state = States.TAKEOFF
print("takeoff transition")
self.takeoff(self.target_position[2])
def waypoint_transition(self):
self.flight_state = States.WAYPOINT
print("waypoint transition")
self.target_position = self.waypoints.pop(0)
print('target position', self.target_position)
self.cmd_position(self.target_position[0], self.target_position[1], self.target_position[2], self.target_position[3])
def landing_transition(self):
self.flight_state = States.LANDING
print("landing transition")
self.land()
def disarming_transition(self):
self.flight_state = States.DISARMING
print("disarm transition")
self.disarm()
self.release_control()
def manual_transition(self):
self.flight_state = States.MANUAL
print("manual transition")
self.stop()
self.in_mission = False
def send_waypoints(self):
print("Sending waypoints to simulator ...")
data = msgpack.dumps(self.waypoints)
self.connection._master.write(data)
def plan_rrt(self):
self.flight_state = States.PLANNING
print("Searching for a path ...")
TARGET_ALTITUDE = 5
SAFETY_DISTANCE = 5
self.target_position[2] = TARGET_ALTITUDE
# TODO: read lat0, lon0 from colliders into floating point values
# TODO: set home position to (lon0, lat0, 0)
# TODO: retrieve current global position
# TODO: convert to current local position using global_to_local()
print('global home {0}, position {1}, local position {2}'.format(self.global_home, self.global_position,
self.local_position))
# Read in obstacle map
data = np.loadtxt('colliders.csv', delimiter=',', dtype='Float64', skiprows=2)
# Define a grid for a particular altitude and safety margin around obstacles
grid, north_offset, east_offset = RRT.create_grid(data, TARGET_ALTITUDE, SAFETY_DISTANCE)
print("North offset = {0}, east offset = {1}".format(north_offset, east_offset))
# Define starting point on the grid (this is just grid center)
grid_start = (-north_offset, -east_offset)
# TODO: convert start position to current position rather than map center
# Set goal as some arbitrary position on the grid
grid_goal = (-north_offset + 10, -east_offset + 10)
# TODO: adapt to set goal as latitude / longitude position and convert
# ~ RRT - SZanlongo
# ~ PRM
# environment encoded as a grid
# ~grid = create_grid()
# Let's take a look at the example environment we'll be using.
# ~plt.imshow(grid, cmap='Greys', origin='upper')
# Run A* to find a path from start to goal
#self.local_position_callback
'''
# TODO: add diagonal motions with a cost of sqrt(2) to your A* implementation
# or move to a different search space such as a graph (not done here)
print('Local Start and Goal: ', grid_start, grid_goal)
#path, _ = a_star(grid, heuristic, grid_start, grid_goal)
'''
x_goal = (30, 750)
num_vertices = 1600
dt = 18
x_init = (20, 150)
path = [(20, 30), (40, 50)]
vertices = RRT.vertices
rrt = RRT.generate_RRT(grid, x_init, num_vertices, dt)
print ('v', rrt)
#print ('a_star', 'grid', grid, 'heuristic', heuristic, 'grid_start', grid_start, 'grid_goal', grid_goal)
#print ('a_star path', path, 'py_interpreter', _)
# Convert path to waypoints
waypoints = [[p[0] + north_offset, p[1] + east_offset, TARGET_ALTITUDE, 0] for p in range(vertices)]
print('wp', waypoints)
# Set self.waypoints
self.waypoints = waypoints
# TODO: send waypoints to sim (this is just for visualization of waypoints)
self.send_waypoints()
#return
#self.path_to_waypoints()
#rrt_motion_plan.rrt_star_path(self)
# TODO: prune path to minimize number of waypoints
# TODO (if you're feeling ambitious): Try a different approach altogether!
def start(self):
self.start_log("Logs", "NavLog.txt")
print("starting connection")
self.connection.start()
# Only required if they do threaded
# while self.in_mission:
# pass
self.stop_log()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=5760, help='Port number')
parser.add_argument('--host', type=str, default='127.0.0.1', help="host address, i.e. '127.0.0.1'")
args = parser.parse_args()
conn = MavlinkConnection('tcp:{0}:{1}'.format(args.host, args.port), timeout=120)
drone = MotionPlanning(conn)
time.sleep(1)
drone.start()