-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomated.py
More file actions
238 lines (189 loc) · 5.07 KB
/
Automated.py
File metadata and controls
238 lines (189 loc) · 5.07 KB
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
230
231
232
233
234
import RPi.GPIO as GPIO
import time
import cv2
import numpy as np
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT) # Motor 1 (IN1)
GPIO.setup(23, GPIO.OUT) # Motor 2 (IN2)
GPIO.setup(24, GPIO.OUT) # Motor 3 (IN4)
GPIO.setup(25, GPIO.OUT) # Motor 4 (IN3)
GPIO.setup(5, GPIO.OUT) # PIN NO 29
GPIO.setup(6, GPIO.OUT) # PIN NO 31
GPIO.setup(15, GPIO.OUT) #PIN NO 10
GPIO.setup(18, GPIO.OUT) #PIN NO 12
GPIO.setup(4, GPIO.OUT) #PIN NO 7
GPIO.setup(17, GPIO.IN) #PIN NO 11
GPIO.setup(14, GPIO.IN) #PIN NO 8
GPIO.setup(27, GPIO.IN) #PIN NO 13
def to_fire(k):
forward()
while k<322 and k>317:
(grabbed, frame) = video.read()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(gray_frame)
k,l = max_loc
dist = get_dist()
fire_cond = ir_config()
if dist < 25:
stop()
time.sleep(0.5)
back()
time.sleep(3)
stop()
return 'stop'
elif dist < 50:
obj_avoid()
return 'again'
if fire_cond:
stop()
xxxtinguish()
return 'complete'
def xxxtinguish():
start_water()
UP()
right()
time.sleep(1)
for i in range (3):
DOWN()
left()
time.sleep(2)
UP()
right()
time.sleep(2)
stop_arm()
stop_water()
stop()
def ir_config():
ir_state1 = GPIO.input(14)
ir_state2 = GPIO.input(27)
if ir_state1 == GPIO.LOW or ir_state2 == GPIO.LOW:
return True
else:
return False
def obj_avoid():
right()
time.sleep(5)
forward()
time.sleep(5)
left()
time.sleep(5)
stop()
dist = get_dist()
if dist < 50 and dist >25:
return obj_avoid()
def get_dist():
# Set trigger to HIGH
GPIO.output(4, True)
# Wait 0.01ms
time.sleep(0.00001)
# Set trigger to LOW
GPIO.output(4, False)
# Record the last LOW timestamp for ECHO
while GPIO.input(17) == 0:
pulse_start = time.time()
# Record the last HIGH timestamp for ECHO
while GPIO.input(17) == 1:
pulse_end = time.time()
# Calculate pulse duration
pulse_duration = pulse_end - pulse_start
# Convert pulse duration to distance (in cm)
distance = pulse_duration * 17150
# Round distance to 2 decimal places
distance = round(distance, 2)
time.sleep(0.5)
print(distance)
return distance
def back():
GPIO.output(22, False)
GPIO.output(23, True)
GPIO.output(24, True)
GPIO.output(25, False)
def forward():
GPIO.output(22, True)
GPIO.output(23, False)
GPIO.output(24, False)
GPIO.output(25, True)
def left():
GPIO.output(22, True)
GPIO.output(23, False)
GPIO.output(24, True)
GPIO.output(25, False)
def right():
GPIO.output(22, False)
GPIO.output(23, True)
GPIO.output(24, False)
GPIO.output(25, True)
def stop():
GPIO.output(22, False)
GPIO.output(23, False)
GPIO.output(24, False)
GPIO.output(25, False)
def DOWN():
GPIO.output(5, False)
GPIO.output(6, True)
def UP():
GPIO.output(6, False)
GPIO.output(5, True)
def stop_arm():
GPIO.output(5, False)
GPIO.output(6, False)
def stop_water():
GPIO.output(15, False)
GPIO.output(18, False)
def start_water():
GPIO.output(15, True)
GPIO.output(18, False)
video = cv2.VideoCapture(0)
while True:
fire_cond = ir_config()
if fire_cond:
stop()
xxxtinguish()
(grabbed, frame) = video.read()
frame = cv2.resize(frame, (640, 480))
# Find brightest location
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(gray_frame)
x,y = max_loc
#print(x)
cv2.circle(frame, max_loc, 20, (0, 0, 255), 2)
cv2.imshow('livefeed', frame)
if x<327 and x>313:
value = to_fire(x)
if value == 'stop':
break
dist = get_dist()
if dist < 25:
stop()
break
if x>322:#rotate right
k=x
while k>322:
(grabbed, frame) = video.read()
frame = cv2.resize(frame, (640, 480))
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(gray_frame)
k,l = max_loc
#print(k)
right()
stop()
#time.sleep(2)
if x<317:#rotate left
k=x
while k<317:
(grabbed, frame) = video.read()
frame = cv2.resize(frame, (640, 480))
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(gray_frame)
k,l = max_loc
#print(k)
left()
stop()
#time.sleep(2)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()
stop()
video.release()