-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigate.py
229 lines (132 loc) · 5.76 KB
/
navigate.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
"""
Plans and drives paths
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
from control import Controller
class Navigation(object):
""" provides path and controls from start to goal """
def __init__(self, grid, astar, plan_and_drive):
# other classes
self.grid = grid
self. astar = astar
self.controller = Controller()
# visual display
self.fig, self.ax = plt.subplots()
if plan_and_drive:
self.display(False)
else:
self.display(True)
self.l = 0.1
self.wait = 0.01
# threshold distance goal/waypoint
self.threshold = 0.15
def plan_drive(self, pose, u):
""" plan and drive, the current path is not known a priori
Args:
pose (np.array): init pose
u (np.array): init contols
"""
# record current velocities
self.controller.record_velocities(u)
# final destination
goal = self.astar.goal_world
# get first waypoint
waypoint = self.astar.drive_online()
# waypoint list
way_x = []
way_y = []
way_x.append(waypoint[0])
way_y.append(waypoint[1])
# keep navigating until goal is reached
while not self.goal_reached(pose, goal):
# plan from robots position
grid_pos = self.grid.cell_index([pose[0], pose[1]])
self.astar.curr_node.position = grid_pos
# waypoint achieved
if self.dist_to_waypoint(pose, waypoint) <= self.threshold:
# plan to next waypoint
waypoint = self.astar.drive_online()
way_x.append(waypoint[0])
way_y.append(waypoint[1])
# update controls
u = self.controller.controls(pose, waypoint)
# update robots position
pose = self.controller.update_pose(pose, u)
# show waypoint
plt.scatter(waypoint[0], waypoint[1], color='red', linewidth=1)
plt.plot(way_x, way_y, color='red', linewidth=1)
# draw robot
plt.arrow(pose[0], pose[1], self.l*np.cos(pose[2]), self.l*np.sin(pose[2]),
head_width=0.2, color='yellow', ec='k')
plt.scatter(pose[0], pose[1], s=90, color='purple', alpha=0.2)
plt.pause(self.wait)
plt.show()
def drive_paths(self, path, pose, u):
""" given a path the robot will drive it
Args:
path (np.array): waypoints in world, first row is x second in y
pose (np.array): init pose
u (np.array): init contols
"""
# record current velocities
self.controller.record_velocities(u)
# index 1 contains first waypoint
i = 1
waypoint = [path[0][i], path[1][i]]
# last waypoint if the goal
goal = [path[0][path.shape[1]-1], path[1][path.shape[1]-1]]
# keep navigating until goal is reached
while not self.goal_reached(pose, goal):
# waypoint achieved
if self.dist_to_waypoint(pose, waypoint) <= self.threshold:
#print("Waypoint: ", i, "reached!")
i += 1
waypoint = [path[0][i], path[1][i]]
# update controls
u = self.controller.controls(pose, waypoint)
# update robots position
pose = self.controller.update_pose(pose, u)
# draw robot
plt.arrow(pose[0], pose[1], self.l*np.cos(pose[2]), self.l*np.sin(pose[2]),
head_width=0.2, color='yellow', ec='k')
plt.scatter(pose[0], pose[1], s=90, color='purple', alpha=0.2)
plt.pause(self.wait)
plt.show()
def goal_reached(self, pose, goal):
""" returns true if goal reached """
if self.dist_to_waypoint(pose, goal) <= self.threshold:
print("Goal reached by driving!")
return True
return False
def dist_to_waypoint(self, pose, waypoint):
""" distance from robot to waypoint """
d = np.sqrt((pose[0]-waypoint[0])**2 + (pose[1]-waypoint[1])**2)
return d
def display(self, given_path=True):
""" provides a visual display """
#loc = plticker.MultipleLocator(base=self.grid.cell_size)
loc = plticker.MultipleLocator(base=1)
self.ax.xaxis.set_major_locator(loc)
self.ax.yaxis.set_major_locator(loc)
self.ax.grid(which='major', axis='both')
plt.grid(True, color='blue', linestyle='--')
plt.imshow(self.grid.world.T, cmap=plt.cm.binary,
interpolation='none', origin='lower', extent=[-2, 5, -6, 6])
if given_path:
plt.plot(self.astar.path_world[0], self.astar.path_world[1], color='red', linewidth=1)
plt.scatter(self.astar.path_world[0], self.astar.path_world[1], color='red', s=10)
plt.scatter(self.astar.start_world[0], self.astar.start_world[1], s=50, color='blue')
self.ax.annotate('Start', xy=(self.astar.start_world[0], self.astar.start_world[1]),
xytext=(30, -20), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
plt.scatter(self.astar.goal_world[0], self.astar.goal_world[1], s=50, color='blue', label='Goal')
self.ax.annotate('Goal', xy=(self.astar.goal_world[0], self.astar.goal_world[1]),
xytext=(-10, 20), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
plt.axis([-2, 5, -6, 6])
plt.title("Grid World")
plt.xlabel('x position (m)')
plt.ylabel('y position (m)')
#