# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark green objects.
import sensor, image, time #导入模块
from pid import PID #导入PID控制模块
from pyb import Pin, Timer #导入引脚和定时器
from pyb import UART
import display
uart = UART(3, 115200)
lcd = display.SPIDisplay() # Initialize the lcd screen.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA2) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False) # turn this off.
#################阈值定义################################
fenbian_thresholds = [ (0, 100, 18, 127, 10, 127),#000-红
(0, 46, -128, -13, 6, 127) ,#001-绿
(0, 39, -128, 127, -128, -10) ]#020-蓝
yidong_thresholds = [ (44, 100, 15, 127, -9, 127),#000-红
(64, 100, -16, 3, 0, 127) ,#001-绿
(65, 100, 6, 127, -128, -18) ]#020-紫
#######################################################
#########电机控制函数##############################################
def run(left_speed, right_speed):
if inverse_left==True: #判断左轮是否需要反转
left_speed=(-left_speed)
if inverse_right==True: #判断右轮是否需要反转
right_speed=(-right_speed)
#设置通道PWM,应改为串口发送
l_speed="#006P"+str(int(abs(left_speed)))+"T1000!"
uart.write(l_speed)
r_speed="#007P"+str(int(abs(right_speed)))+"T1000!"
uart.write(r_speed)
#########寻找最大色块函数###########################################
####返回最大色块max_blob的blob参数######
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
########变量定义###################################################
fenbian_mode=0 #方块 分辨模式
yidong_mode=000 #移动 分辨区域模式
inverse_left=False #将其更改为True以反转左轮
inverse_right=False #将其更改为True以反转右轮
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
yidong_finish_flag=0
################################################################
clock = time.clock() # Tracks FPS.
#############主循环###############################################
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() #获取一张图片
lcd.write(img)
blobs = img.find_blobs([yidong_thresholds[0]])
if blobs and yidong_mode == 000 and yidong_finish_flag==0 : ##########分辨红色###########
yidong_blobs = img.find_blobs([yidong_thresholds[0]])
yidong_max_blob = find_max(yidong_blobs)
img.draw_rectangle(yidong_max_blob[0:4]) #绘制矩形
img.draw_cross(yidong_max_blob[5], yidong_max_blob[6]) #绘制中心十字
print('test3')
img.draw_string(10,10,"test3",scale=1.2,mono_space=False)
if fenbian_mode == 0 :
max_blob = find_max(blobs)
x_error = max_blob[5]-img.width()/2 #计算x轴离中心点位置
h_error = max_blob[2]*max_blob[3]-size_threshold #距离计算,计算色块面积-滤波面积
img.draw_string(10,50,f"x_error:{x_error}",scale=1.2,mono_space=False)
img.draw_rectangle(max_blob[0:4]) #绘制矩形
img.draw_cross(max_blob[5], max_blob[6]) #绘制中心十字
x_output=x_pid.get_pid(x_error,1) #速度计算
h_output=h_pid.get_pid(h_error,1) #速度计算
run(-h_output-x_output+1500,-h_output+x_output-1500)
print('test1')
print(-h_output-x_output+1500,-h_output+x_output-1500)
img.draw_string(10,30,"test1",scale=1.2,mono_space=False)
if yidong_max_blob[4]>=3000:
run(1500,1500)
fenbian_mode=1
yidong_finish_flag=1
elif blobs and yidong_finish_flag==1 : ##################进入分辨模式################## 色块像素大于3000进入
for i in range (0,3) :
fenbian_blobs = img.find_blobs([fenbian_thresholds[i]])
if fenbian_blobs :
fenbian_max_blob = find_max(fenbian_blobs)
img.draw_rectangle(fenbian_max_blob[0:4]) #绘制矩形
img.draw_cross(fenbian_max_blob[5], fenbian_max_blob[6]) #绘制中心十字
print('test2')
img.draw_string(10,30,"test2",scale=1.2,mono_space=False)
# else:
# run(18,-18) #寻找不到色块则原地自转
# if blobs and fenbian_mode==0 :
# max_blob = find_max(blobs)
# x_error = max_blob[5]-img.width()/2 #计算x轴离中心点位置
# h_error = max_blob[2]*max_blob[3]-size_threshold #滤波,计算色块面积-滤波面积
# img.draw_string(10,10,f"x_error:{x_error}",scale=1.2,mono_space=False)
# img.draw_rectangle(max_blob[0:4]) #绘制矩形
# img.draw_cross(max_blob[5], max_blob[6]) #绘制中心十字
# x_output=x_pid.get_pid(x_error,1) #速度计算
# h_output=h_pid.get_pid(h_error,1) #速度计算
# run(-h_output-x_output,-h_output+x_output)
# else:
# run(18,-18) #寻找不到色块则原地自转
原因:img还没画线就直接发送到lcd了。
解决办法:lcd.write(img)移到最下面。
原来是这样!解决了,太感谢了