-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar_algorithmn.py
171 lines (143 loc) · 5.48 KB
/
astar_algorithmn.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
from pyamaze import maze, agent, COLOR, textLabel
from queue import PriorityQueue
import serial
def h(cell1, cell2):
x1, y1 = cell1
x2, y2 = cell2
return (abs(x1 - x2) + abs(y1 - y2))
def aStar(m):
(m.rowS, m.colS) = (1, 1)
(m.rowG, m.colG) = (5, 5)
print("Star Point:", (m.rowS, m.colS))
ser = serial.Serial('COM5', 9600, timeout=1)
ser.write(str(m.rowS).encode())
while True:
data = ser.readline().decode().strip()
if data == '0':
break
elif data:
print('Unexpected data:', data)
ser.write(str(m.colS).encode())
while True:
data = ser.readline().decode().strip()
if data == '1':
break
elif data:
print('Unexpected data:', data)
print("End Point:", (m.rowG, m.colG))
# create a serial port object with the appropriate parameters for the xbee module
ser.write(str(m.rowG).encode())
while True:
data = ser.readline().decode().strip()
if data == '0':
break
elif data:
print('Unexpected data:', data)
ser.write(str(m.colG).encode())
open = PriorityQueue()
open.put((h((m.rowG, m.colG), (m.rowS, m.colS)), h((m.rowG, m.colG), (m.rowS, m.colS)), (m.rowG, m.colG)))
aPath = {}
g_score = {row: float("inf") for row in m.grid}
g_score[(m.rowG, m.colG)] = 0
f_score = {row: float("inf") for row in m.grid}
f_score[(m.rowG, m.colG)] = h((m.rowG, m.colG), (m.rowS, m.colS))
searchPath = [(m.rowG, m.colG)]
while not open.empty():
currCell = open.get()[2]
searchPath.append(currCell)
if currCell == (m.rowS, m.colS):
break
for d in 'ESNW':
if m.maze_map[currCell][d] == True:
if d == 'E':
childCell = (currCell[0], currCell[1] + 1)
elif d == 'W':
childCell = (currCell[0], currCell[1] - 1)
elif d == 'N':
childCell = (currCell[0] - 1, currCell[1])
elif d == 'S':
childCell = (currCell[0] + 1, currCell[1])
temp_g_score = g_score[currCell] + 1
temp_f_score = temp_g_score + h(childCell, (m.rowS, m.colS))
if temp_f_score < f_score[childCell]:
aPath[childCell] = currCell
g_score[childCell] = temp_g_score
f_score[childCell] = temp_g_score + h(childCell, (m.rowS, m.colS))
open.put((f_score[childCell], h(childCell, (m.rowS, m.colS)), childCell))
fwdPath = {}
cell = (m.rowS, m.colS)
while cell != (m.rowG, m.colG):
fwdPath[aPath[cell]] = cell
cell = aPath[cell]
return searchPath, aPath, fwdPath
if __name__ == '__main__':
m = maze(6, 6)
m.CreateMaze(loadMaze='map.csv')
searchPath, aPath, fwdPath = aStar(m)
a = agent(m, footprints=True, color=COLOR.blue, filled=True)
b = agent(m, m.rowS, m.colS, footprints=True, color=COLOR.yellow, filled=True, goal=(m.rowG, m.colG))
c = agent(m, footprints=True, color=COLOR.red)
m.tracePath({a: searchPath}, delay=300)
m.tracePath({b: aPath}, delay=300)
m.tracePath({c: fwdPath}, delay=300)
shortest_path = [b.goal]
cell = b.goal
while cell != (m.rowS, m.colS):
shortest_path.append(fwdPath[cell])
cell = fwdPath[cell]
shortest_path.reverse()
print("Forward Path:", shortest_path)
# create a serial port object with the appropriate parameters for the xbee module
ser = serial.Serial('COM5', 9600, timeout=1)
l = textLabel(m, 'A Star Path Length', len(fwdPath) + 1)
l = textLabel(m, 'A Star Search Length', len(searchPath))
# Show the animation first
m.run()
# Send the cell address after the animation is complete
for cell in shortest_path:
# send the x-coordinate to the microcontroller
ser.write(str(cell[0]).encode())
# wait for confirmation from the microcontroller
while True:
data = ser.readline().decode().strip()
if data == '0':
break
elif data:
print('Unexpected data:', data)
# send the y-coordinate to the microcontroller
ser.write(str(cell[1]).encode())
print("Move to ➡️", cell)
# wait for confirmation from the microcontroller
while True:
data = ser.readline().decode().strip()
if data == '1':
break
elif data:
print('Unexpected data:', data)
print("Reverse Path:", shortest_path[::-1])
for cell in reversed(shortest_path[:-1]):
# send the x-coordinate to the microcontroller
ser.write(str(cell[0]).encode())
# wait for confirmation from the microcontroller
while True:
data = ser.readline().decode().strip()
if data == '0':
break
elif data:
print('Unexpected data:', data)
# send the y-coordinate to the microcontroller
ser.write(str(cell[1]).encode())
print("Move to ➡️", cell)
# wait for confirmation from the microcontroller
while True:
data = ser.readline().decode().strip()
if data == '1':
break
elif data:
print('Unexpected data:', data)
# read the data from the microcontroller
data = ser.readline().decode()
# print the data
print(data)
# close the serial port
ser.close()