-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencv2.py
More file actions
307 lines (265 loc) · 8.4 KB
/
opencv2.py
File metadata and controls
307 lines (265 loc) · 8.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from imutils.video import VideoStream
from reference.pid import PID
import imutils
import cv2
import time
import RPi.GPIO as GPIO
import os
import pigpio
MIN_ANG = 30 #degrees
MAX_ANG = 150 #degrees
MIN_PW = 800 # microseconds
MAX_PW = 2200 # microseconds
ANG_RANGE = MAX_ANG-MIN_ANG
PW_RANGE = MAX_PW-MIN_PW
PWAR = float(PW_RANGE)/ANG_RANGE
colorLower =(10,100,100)
colorUpper = (50,255,255)
panServo = 27
tiltServo = 17
panAngle = 90
tiltAngle = 90
prev = 0
vs = 0
p = 0 ###for y movement
r = 0 ###for x movement
move = 0
def motor_init():
global motorPwm
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(21, GPIO.OUT) #enable pin of motor
motorPwm = GPIO.PWM(21, 1000)
motorPwm.start(0)
def left():
GPIO.output(26, False)
GPIO.output(19, True)
GPIO.output(13, False)
GPIO.output(6, True)
## sleep(tf)
def right():
GPIO.output(26, True)
GPIO.output(19, False)
GPIO.output(13, True)
GPIO.output(6, False)
## sleep(tf)
def forward():
GPIO.output(26, True)
GPIO.output(19, False)
GPIO.output(13, False)
GPIO.output(6, True)
## sleep(tf)
def reverse():
GPIO.output(26, False)
GPIO.output(19, True)
GPIO.output(13, True)
GPIO.output(6, False)
## sleep(tf)
def stop():
GPIO.output(26, False)
GPIO.output(19, False)
GPIO.output(13, False)
GPIO.output(6, False)
## sleep(tf)
def driveMotor(dc):
global motorPwm
assert dc >=0 and dc <= 100
###dutyCycle = angle / 18. + 3.
motorPwm.ChangeDutyCycle(dc)
###### time.sleep(0.3)
def system_init():
global vs
global p
global r
global panAngle
global tiltAngle
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
os.system('sudo pigpiod')
print("PIGPIOD STARTED!!!")
time.sleep(1)
motor_init()
GPIO.setup(panServo, GPIO.OUT)
GPIO.setup(tiltServo, GPIO.OUT)
positionServo(panServo, panAngle)
positionServo(tiltServo, tiltAngle)
# create a PID and initialize it
p = PID(0.15, 0.04, 0.02) #p.value, i.value, d.value 0.2, 0.06, 0.021
## p = PID(1, 0.0, 0.0)
p.initialize()
# create a PID and initialize it
r = PID(0.0017, 0.0001, 0.0002)###(1.0, 0.0 , 0.0) ###
r.initialize()
print("Waiting for for camera to warmup...")
vs = VideoStream(0).start()
time.sleep(2.0)
#position servo
def positionServo(servo,angle):
############
############ global prev
############ assert angle >=0 and angle <= 180
############ pwm = GPIO.PWM(servo, 50)
############ pwm.start(prev)
############ dutyCycle = angle / 18. + 3.
############ pwm.ChangeDutyCycle(dutyCycle)
############ time.sleep(0.3)
###################### pwm.stop()
############ prev = dutyCycle
############ time.sleep(0.2)
assert MIN_ANG <= angle <= MAX_ANG
pi = pigpio.pi()
angle = (MIN_PW + ((angle - MIN_ANG) * PWAR))
pi.set_servo_pulsewidth(servo, angle)
## time.sleep(0.1)
pi.stop()
def mapServo(x,y):
global move
###### if(x>=0 and x<= 180):
###### positionServo(panServo, x)
###### if(y>=0 and y<=180):
###### positionServo(tiltServo, y)
y = y//2
x = round( x/2, 2)
## print("X->{0}".format(y))
#time.sleep(0.08)
if(y < 0):
if(y< -60):
y = 0
p.initialize()
########## for k in range(0,int(abs(y))):
tilt = 90 + abs(y)
positionServo(tiltServo, tilt)
#time.sleep(0.1)
## print("opX->{0} & opY->{1}".format(x, tilt))
if(y > 0):
if(y> 60):
y = 0
p.initialize()
########## for k in range(0,int(abs(y))):
tilt = 90 - abs(y)
positionServo(tiltServo, tilt)
#time.sleep(0.1)
## print("opX->{0} & opY->{1}".format(x, tilt))
## if(y == 0):
## positionServo(tiltServo, 90)
## #time.sleep(0.1)
## print("opX->{0} & opY->{1}".format(x, y))
########################################
if(x > 0):
if(x > 0.2):
x = 0
r.initialize()
driveMotor(50)
left()
time.sleep(x)#0.65 x
stop()
if(x < 0):
if(x < -0.2):
x = 0
r.initialize()
driveMotor(50)###abs(x)
right()
time.sleep(abs(x))#0.65 abs(x)
stop()
if(x >= -0.01 and x<= 0.01):
stop()
def obj_center():
global vs
global motorPwm
while True:
# grab the next frame from the video stream, Invert 180o, resize the
# frame, and convert it to the HSV color space
frame = vs.read()
frame = imutils.resize(frame, width=360, height=360)
frame = imutils.rotate(frame, angle=180)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# calculate the center of the frame as this is where we will
# try to keep the object
(H,W) = frame.shape[:2]
centerX = W//2
centerY = H//2
cv2.circle(frame, (centerX, centerY), 5, (0, 255, 0), -1)
# construct a mask for the object color, then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, colorLower, colorUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# find contours in the mask and initialize the current
# (x, y) center of the object
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
#find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
(objX, objY) = center
# only proceed if the radius meets a minimum size
###print("Radius:{0}".format(radius))
if (radius > 50):
driveMotor(50)
reverse()
time.sleep(0.16)
stop()
elif (radius < 20):
driveMotor(50)
forward()
time.sleep(0.16)
stop()
else:
stop()
#################### if radius > 10:
#Draw the circle and centroid on the frame,
# then update the list of tracked points
cv2.circle(frame, (int(x), int(y)), int(radius),(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
opY = pid_process(objY, centerY, 0)
opX = pid_process(objX, centerX, 1)
mapServo(opX, opY)
## print("[INFO] center->{0}\n".format(centerY))
## print("[INFO] object->{0}\n".format(objY))
# show the frame to our screen
cv2.imshow("Frame", frame)
## p.initialize()
## r.initialize()
##
# if [ctrl+c] key is pressed, stop the loop
key = cv2.waitKey(1) & 0xFF
if key == ord('c') or key == ord('C'):
positionServo(panServo, 90)
positionServo(tiltServo, 90)
motorPwm.stop()
stop()
os.system('sudo killall pigpiod')
GPIO.cleanup()
cv2.destroyAllWindows()
vs.stop()
break
def pid_process(objCoord, centerCoord, motion):
global p
global r
if(motion == 0):
# calculate the error
error = centerCoord - objCoord
# update the value
output = p.update(error)
if(motion == 1):
# calculate the error
error = centerCoord - objCoord
# update the value
output = r.update(error)
########## print("o/p = {0}".format(output))
return output
if __name__ == '__main__':
###### pi = piGPIO.pi()
system_init()
obj_center()