import sensor, image, time, math,screen
from pyb import UART,LED
uart = UART(3, 9600)
thresholds = (0, 100, 19, 127, -128, 127)
clock = time.clock()
global x_red
global x_green
global y_red
global y_green
global flag
flag = 0
screen.init()
def color_blob(threshold):
blobs = img.find_blobs([threshold])
if len(blobs) == 1:
b = blobs[0]
cx = b[5]
cy = b[6]
return 160, 120
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
def sensor1():
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(False)
sensor.skip_frames(20)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_windowing((240, 240))
sensor.set_auto_exposure(False,300)
def sensor2():
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_windowing((120, 120))
sensor1()
while(True):
img = sensor.snapshot()
img = sensor.snapshot()
print('点')
img = sensor.snapshot()
blobs = img.find_blobs([thresholds])
if blobs:
color_blob(thresholds)
max_blob = find_max(blobs)
pan_error = max_blob.cx()-83
tilt_error = max_blob.cy()-155
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
uart.write('%d %d %d\r\n'%(pan_error,tilt_error,flag))
print('pan_error',pan_error,'tilt_error',tilt_error)
if abs(pan_error) < 2 and abs(tilt_error) < 2:
flag = 1
uart.write('%d %d %d\r\n'%(pan_error,tilt_error,flag))
2023电赛追踪代码存库自用
最新推荐文章于 2024-11-11 11:17:11 发布