import sensor, image, time
from machine import I2C
from vl53l1x import VL53L1X
from pyb import LED
from pyb import UART, Timer
from pyb import Servo
from pid import PID
uart = UART(1,115200)
i2c = I2C(2)
distance = VL53L1X(i2c)
test_state = 1
L = 0
chang = 0
#cir.r = 300
pan_pid=PID(p=0.05, i=0.01, d=0, imax=90) #在线调试PID
tilt_pid=PID(p=0.05, i=0.012,d=0, imax=90)
#pan_pid=PID(p=0.06, i=0.1, d=0.02, imax=100) #脱机运行PID
#tilt_pid=PID(P=0.05, i=0.05, d=0, imax=90)
'''
rect_threshold=(20, 47, 29, 70, 12, 58) #(24, 45, 32, 70, 12, 58)
circle_threshold=(61, 23, 31, 81, 5, 59)
'''
all_thresholds = [(30, 100, 15, 127, 15, 127), # _red_thresholds
(30, 100, -64, -8, -32, 32), # _green_thresholds
(0, 15, 0, 40, -80, -20)] # _blue_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
def uart_singal():
buff = 0
buff = uart.any()
if buff > 0:
_data_receive(readchar())
else:
pass
def _data_receive(data):
if data == 65:
test_state = 1
elif data == 66:
test_state = 2
else:
print('error!!!')
def test1_2():
for r in img.find_rects(threshold = 20000):
area_rectangle = (r.rect()) #是为了在检测矩形的基础上检测有颜色的矩形
for blob in img.find_blobs(all_thresholds, pixels_threshold=100, area_threshold=100, merge=True, margin=10,roi = area_rectangle):
img.draw_rectangle(area_rectangle, color = (0, 255, 0))
LED(1).toggle()
for c in img.find_circles(threshold = 2200):
area_circle = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())#是为了在检测有圆的基础上检测有颜色的圆
draw_cir = (c.x(), c.y(), c.r())#画圆的轮廓
for blob in img.find_blobs(all_thresholds, pixels_threshold=100, area_threshold=100, merge=True, margin=10, roi = area_circle):
img.draw_circle(draw_cir, thickness=1, color = (0,255,0), fill=False)
LED(3).toggle()
L = distance.read()-50
print(L)
chang = L
uart.write(chang)
def test3_4():
pass
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
while(True):
clock.tick()
img = sensor.snapshot()
if(test_state == 1):
test1_2()
elif(test_state == 2):
test3_4()
else:
pass
uart_singal()
L是int,uart.write只能发送字节串(字符串)。
uart.write(str(L))#转为字符串