-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (49 loc) · 1.96 KB
/
main.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
from drone_control import DroneControl
from motion_planner import MotionPlanner
import matplotlib.pyplot as plt
import numpy as np
from udacidrone.connection import MavlinkConnection
from udacidrone.frame_utils import global_to_local
from utils import load_grid_from_csv, get_random_point_from_grid
import time
FLIGHT_ALTITUDE = 6
if __name__ == "__main__":
# Create coneection to Udacity's Simulator
conn = MavlinkConnection('tcp:127.0.0.1:5760', threaded=True)
drone = DroneControl(conn)
drone.start()
drone.takeoff_altitude = FLIGHT_ALTITUDE
# Setup grid, start and goal
grid, n_offset, e_offset = load_grid_from_csv("colliders.csv", FLIGHT_ALTITUDE)
start_grid = (int(drone.local_position[0] - n_offset),
int(drone.local_position[1] - e_offset))
goal_grid = get_random_point_from_grid(grid)
# Plot grid and goal
plt.matshow(grid, origin='lower')
plt.plot(start_grid[1], start_grid[0], 'go')
plt.plot(goal_grid[1], goal_grid[0], 'rx')
plt.show()
# Path planner
print(f"Start: {start_grid}, Goal: {goal_grid}")
mp = MotionPlanner(grid)
success, path, cost = mp.run_planner(start_grid, goal_grid)
if success:
pruned_path = np.array(mp.prune_path(path))
plt.matshow(grid, origin='lower')
plt.plot(pruned_path[:, 1], pruned_path[:, 0], 'b')
plt.show()
waypoints = [[p[0] + n_offset, p[1] + e_offset,
FLIGHT_ALTITUDE, 0] for p in pruned_path]
print(waypoints)
drone.waypoints = waypoints
# Draw position
plt.matshow(grid, origin='lower')
plt.plot(pruned_path[:, 1], pruned_path[:, 0], 'b')
while drone.in_mission:
position = (int(drone.local_position[0] - n_offset),
int(drone.local_position[1] - e_offset))
plt.scatter(position[1], position[0],
5, "#14d9b1")
plt.pause(0.05)
pass
plt.show()