Professional Documents
Culture Documents
import numpy as np
from adafruit_servokit import ServoKit
#hardware work
GPIO.setmode(GPIO.BOARD)
# That was the distance there and back so halve the value
distance = distance / 2
GPIO.setup(MOTOR1B, GPIO.OUT)
GPIO.setup(MOTOR1E, GPIO.OUT)
GPIO.setup(MOTOR2B, GPIO.OUT)
GPIO.setup(MOTOR2E, GPIO.OUT)
def forward():
GPIO.output(MOTOR1B, GPIO.LOW)
GPIO.output(MOTOR1E, GPIO.HIGH)
GPIO.output(MOTOR2B, GPIO.HIGH)
GPIO.output(MOTOR2E, GPIO.LOW)
print("forward")
def reverse():
GPIO.output(MOTOR1B, GPIO.HIGH)
GPIO.output(MOTOR1E, GPIO.LOW)
GPIO.output(MOTOR2B, GPIO.LOW)
GPIO.output(MOTOR2E, GPIO.HIGH)
#print("revrse")
def rightturn():
GPIO.output(MOTOR1B,GPIO.HIGH)
GPIO.output(MOTOR1E,GPIO.LOW)
GPIO.output(MOTOR2B,GPIO.HIGH)
GPIO.output(MOTOR2E,GPIO.LOW)
#time.sleep(0.1)
#print("right")
def leftturn():
GPIO.output(MOTOR1B,GPIO.LOW)
GPIO.output(MOTOR1E,GPIO.HIGH)
GPIO.output(MOTOR2B,GPIO.LOW)
GPIO.output(MOTOR2E,GPIO.HIGH)
#print("left ")
#time.sleep(0.1)
def stop():
GPIO.output(MOTOR1E,GPIO.LOW)
GPIO.output(MOTOR1B,GPIO.LOW)
GPIO.output(MOTOR2E,GPIO.LOW)
GPIO.output(MOTOR2B,GPIO.LOW)
print("stop")
def close():
kit.servo[0].angle = 75
time.sleep(1)
def open():
kit.servo[0].angle = 0
time.sleep(1)
cont_index=idx
#if res>15 and res<18:
# cont_index=idx
r=(0,0,2,2)
if len(contours) > 0:
r = cv2.boundingRect(contours[cont_index])
return r,largest_contour
def target_hist(frame):
hsv_img=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hist=cv2.calcHist([hsv_img],[0],None,[50],[0,255])
return hist
#CAMERA CAPTURE
#initialize the camera and grab a reference to the raw camera capture
else:
#if it founds the ball and it is too close it lights up the
led.
GPIO.output(LED_PIN,GPIO.HIGH)
time.sleep(0.1)
stop()
time.sleep(0.1)
cv2.imshow("draw",frame)
#rawCapture.truncate(0) # clear the stream in preparation for the next frame