OpenMV颜色识别控制机械臂的移动

import sensor, image, time

from pid import PID
from pyb import Servo
from pyb import UART

uart = UART(3, 115200)

#pan_servo=Servo(1)
#tilt_servo=Servo(2)

yellow_threshold  = (64, 92, -13, 29, 25, 78)

#pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10)
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob
z=1500

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        max_blob = find_max(blobs)
        pan_error = max_blob.cx()-img.width()/2 # 什么意思
        tilt_error = max_blob.cy()-img.height()/2 #??????

#        print("pan_error: ", pan_error)
#        print("tilt_error: ", tilt_error)

        img.draw_rectangle(max_blob.rect()) # rect
        img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy

        pan_output=pan_pid.get_pid(pan_error,1)/2 #增益值(由pid得出的值)
        tilt_output=tilt_pid.get_pid(tilt_error,1)
        print("pan_output",pan_output)
#        print("tilt_output",tilt_output)

#108*pan_output
        x=float(pan_output)
        y=108*x
        j=y+1500 
        z=int(j)  #类型转换
        print(z)
        z=str(z)  #使符合字符串输出
        time.sleep(1000)
        uart.write("#1P1500#2P1500#3P1500#4P1500#5P1500#6P"+z+"T1000\r\n") #控制机械臂移动的指令
        time.sleep(1000)

 

你可能感兴趣的:(OpenMV颜色识别控制机械臂的移动)