-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanomaly.py
216 lines (197 loc) · 6.21 KB
/
anomaly.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
from bot_globals import bot
from ultrasonic import ultra
import RPi.GPIO as gpio
from time import sleep
import file_handling as file_h
import bot_movement as bm
from callibration import callibrate
pin1 = 7
pin2 = 8
gpio.setmode(gpio.BCM)
gpio.setup(pin1, gpio.OUT)
gpio.setup(pin2, gpio.OUT)
mapp = [[]]
def check():
if ultra() <= 20:
return False
else:
return True
def anomaly_look(cx, cy):
global mapp
rows = len(mapp)
columns = len(mapp[0])
if (not((cx < rows and cx >= 0) and (cy < columns and cy >= 0))):
return False
if (mapp[cx][cy]==5):
return True
if (mapp[cx][cy]!=0):
return False
mapp[cx][cy] = 3
if (anomaly_look(cx-1,cy) == True):
return True
if (anomaly_look(cx,cy+1) == True):
return True
if (anomaly_look(cx+1,cy) == True):
return True
if (anomaly_look(cx,cy-1) == True):
return True
mapp[cx][cy] = 0
return False
def anomaly_find_path(cx, cy):
global mapp
print mapp
rows = len(mapp)
columns = len(mapp[0])
mapp[cx][cy] = 0
while True:
if cx-1 >= 0:
if (mapp[cx-1][cy] == 3 or mapp[cx-1][cy] == 5):
bm.up()
cx -= 1
callibrate(rows, columns, cx, cy, mapp)
if mapp[cx][cy] == 5:
mapp[cx][cy] = 0
print "up"
break
else:
mapp[cx][cy] = 0
print "up"
continue
if cx+1 < rows:
if (mapp[cx+1][cy] == 3 or mapp[cx+1][cy] == 5):
bm.down()
cx += 1
callibrate(rows, columns, cx, cy, mapp)
if mapp[cx][cy] == 5:
mapp[cx][cy] = 0
print "down"
break
else:
mapp[cx][cy] = 0
print "down"
continue
if cy+1 < columns:
if (mapp[cx][cy+1] == 3 or mapp[cx][cy+1] == 5):
bm.right()
cy += 1
callibrate(rows, columns, cx, cy, mapp)
if mapp[cx][cy] == 5:
mapp[cx][cy] = 0
print "right"
break
else:
mapp[cx][cy] = 0
print "right"
continue
if cy-1 >=0:
if (mapp[cx][cy-1] == 3 or mapp[cx][cy-1] == 5):
bm.left()
cy -= 1
callibrate(rows, columns, cx, cy, mapp)
if mapp[cx][cy] == 5:
mapp[cx][cy] = 0
print "left"
break
else:
mapp[cx][cy] = 0
print "left"
continue
else:
return "you have reached your destination" # put a different kind of result
"""
now i will first chec the distance and then get to a certain distance
then i will go down and hold and lift and then
start movement
when reached the location then drop and go to origin #for now
later go to the same place and start again
"""
def pick_the_block():
global pin1
global pin2
gpio.output(pin1, 1)
gpio.output(pin2, 1)
#for the time that is enough
sleep(15)
def drop_the_block():
global pin1
global pin2
gpio.output(pin1, 0)
gpio.output(pin2, 0)
#for the time that is enough
sleep(15)
def rest():
global pin1
global pin2
gpio.output(pin1, 1)
gpio.output(pin2, 0)
#if needed
sleep(10)
def go_to_location(digit):
global mapp
cx = getcoords(digit)[0]
cy = getcoords(digit)[1]
if cx-1 >= 0:
if mapp[cx-1][cy] == 0:
anomaly_look(cx, cy)
anomaly_find_path(cx, cy)
bm.look_down()
elif cx+1 < rows:
if mapp[cx+1][cy] == 0:
anomaly_look(cx, cy)
anomaly_find_path(cx, cy)
bm.look_up()
elif cy+1 < columns:
if mapp[cx][cy+1] == 0:
anomaly_look(cx, cy)
anomaly_find_path(cx, cy)
bm.look_left()
elif cy-1 >=0:
if mapp[cx][cy-1] == 0:
anomaly_look(cx, cy)
anomaly_find_path(cx, cy)
bm.look_right()
else:
print "there is no place from where i can place the block"
def correct_the_location(image_matrix):
global mapp
mapp = image_matrix
global pin1
global pin2
try:
gpio.output(pin1, 0)
gpio.output(pin2, 0)
distance = ultra()
the_required_distance = 10
the_required_distance_picture = 15
if distance <= the_required_distance_picture:
bm.backward(the_required_distance_picture - distance)
else:
bm.forward(distance - the_required_distance_picture)
digit = click_picture()
distance = ultra()
if distance <= the_required_distance:
bm.backward(the_required_distance - distance)
else:
bm.forward(distance - the_required_distance)
pick_the_block()
go_to_location(digit)
drop_the_block()
rest()
"""
if(ultra()<the_required_distance):
print "gone in"
pick_the_block()
sleep(10)
drop_the_block()
sleep(10)
rest()
sleep(10)
else:
rest()
print "gone out"
"""
#go_to_origin()
except KeyboardInterrupt:
gpio.cleanup()
finally:
gpio.cleanup()