forked from Ridhwanluthra/NLMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_traversal.py
195 lines (182 loc) · 5.12 KB
/
first_traversal.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
# code for making the bot move in the grid
# ALL BLOCK COMMENTS ANSWER THE QUESTION "WHAT DO I HAVE AT THIS POINT?"
# take input of the matrix of the image
# store this x,y in a different variable
# take input of the start and the end point
from bot_movement import *
from time import sleep
#from click_picture import click_picture
"""
I get a matrix which has some 0's and 1's
I get a start point and an end point
"""
"""
I have a way to move in different directions
I still have to configure these functions
I am working on it lets see what happens.
so now it just becomes a problem of changing my control
from one location to the other in a matrix
"""
"""
/* values represent:
0=free path
1=blocked path
3=valid path
4=invalid path
5=goal
"""
"""
This program gives the best path to move from source to destination.
"""
mapp=[[]]
def first_look(x, y):
global mapp
rows = len(mapp)
columns = len(mapp[0])
if (not((x < rows and x >= 0) and (y < columns and y >= 0))):
return False
if (mapp[x][y]==5):
return True
if (mapp[x][y]!=0):
return False
mapp[x][y] = 3
if (first_look(x-1,y) == True):
return True
if (first_look(x,y+1) == True):
return True
if (first_look(x+1,y) == True):
return True
if (first_look(x,y-1) == True):
return True
mapp[x][y] = 0
return False
"""
Now I can create a matrix which has a path path of 3's
which i can follow to get my bot to the final location
"""
def first_find_path(x, y):
global mapp
print mapp
rows = len(mapp)
columns = len(mapp[0])
mapp[x][y] = 0
while True:
if x-1 >= 0:
if (mapp[x-1][y] == 3 or mapp[x-1][y] == 5):
up()
x -= 1
if mapp[x][y] == 5:
mapp[x][y] = 0
print "up"
break
else:
mapp[x][y] = 0
print "up"
continue
if x+1 < rows:
if (mapp[x+1][y] == 3 or mapp[x+1][y] == 5):
down()
x += 1
if mapp[x][y] == 5:
mapp[x][y] = 0
print "down"
break
else:
mapp[x][y] = 0
print "down"
continue
if y+1 < columns:
if (mapp[x][y+1] == 3 or mapp[x][y+1] == 5):
right()
y += 1
if mapp[x][y] == 5:
mapp[x][y] = 0
print "right"
break
else:
mapp[x][y] = 0
print "right"
continue
if y-1 >=0:
if (mapp[x][y-1] == 3 or mapp[x][y-1] == 5):
left()
y -= 1
if mapp[x][y] == 5:
mapp[x][y] = 0
print "left"
break
else:
mapp[x][y] = 0
print "left"
continue
else:
return "you have reached your destination" # put a different kind of result
"""
I have reached my final destination
using the matrix with 3's
I found where there was 3 and accordingly
I moved the bot to the location needed
"""
"""
Now i need to create a function that can make
each location i have to go to 5 in turn so that
i can go and take pictures of each obstacle
"""
# x and y being the current position of the bot
def first_mapping(x, y, maps):
#ADD A FILE SAVING MECHANISM
global mapp
mapp = maps
rows = len(mapp)
columns = len(mapp[0])
#first_look(x,y)
#first_find_path(x,y)
for i in range(rows):
for j in range(columns):
if (mapp[i][j] == 1):
if (i-1 >= 0 and mapp[i-1][j] == 0):
global mapp
mapp[i-1][j] = 5;
first_look(x,y)
first_find_path(x,y)
look_down()
sleep(2)
x = i-1
y = j
#click_picture(i,j)
mapp[i-1][j] = 0
elif (j+1 < columns and mapp[i][j+1] == 0):
global mapp
mapp[i][j+1] = 5;
first_look(x,y)
first_find_path(x,y)
look_left()
sleep(2)
x = i
y = j+1
#click_picture(i,j)
mapp[i][j+1] = 0
elif (i+1 < rows and mapp[i+1][j] == 0):
global mapp
mapp[i+1][j] = 5;
first_look(x,y)
first_find_path(x,y)
look_up()
sleep(2)
x = i+1
y = j
#click_picture(i,j)
mapp[i+1][j] = 0
elif (j-1 >= 0 and mapp[i][j-1] == 0):
global mapp
mapp[i][j-1] = 5;
first_look(x,y)
first_find_path(x,y)
look_right()
sleep(2)
x = i
y = j-1
#click_picture(i,j)
mapp[i][j-1] = 0
else:
print "there is some error in mapping function in file traversal.py"