# Initialize some values
error_last = 0
error_sum = 0

# Set controller gains
#Kp = .00075
#Kd = .00001
#Ki = .00001

Kp = .001
Kd = .001
Ki = .000001

while True:
    # load the current image
    image = HAL.getImage()

    # Find the red line in the image
    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask = mask)

    # Locate the centroid of the red line
    M = cv2.moments(mask, binaryImage=True)

    # Display a white circle at the centroid of the line
    if M['m00'] != 0:
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.circle(image, (cx, cy), 10, (255, 255, 255), -1)
    else:
        cx = 0
from GUI import GUI
from HAL import HAL
# Enter sequential code!
import cv2
import numpy as np

while True:
    frame = HAL.getImage()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (30, 30), 0)
    
    #Otsu's Thresholding
    ret, th = cv2.threshold(blur, 0, 255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    #Adaptive Gaussian Thresholding
    th1 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
            
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    blue_lower = np.array([0, 120, 97], dtype='uint8')
    blue_upper = np.array([179, 255, 255], dtype='uint8')
    
    mask = cv2.inRange(hsv, blue_lower, blue_upper)
    erosion = cv2.erode(mask, kernel, iterations=2)
    dialation = cv2.dialate(erosion, kernel, iterations=2)
    
    cont = cv2.findContours(dialation, cv2.RETR_LIST, cv2.cv2.CHAIN_APPROX_SIMPLE)
    
    areas = [cv2.contourArea(c) for c in cont]
    
    if len(areas) > 0:
        area_max = np.argmax(areas)