为什么没有识别到相应颜色时pan_error显示没有定义,运行开始时识别到相应颜色就可以运行?
-
import sensor, image, time from pyb import UART from pid import PID sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.set_vflip(True)#垂直方向的翻转 sensor.set_hmirror(True)#水平方向的翻转 sensor.skip_frames(10) sensor.set_auto_whitebal(False) clock = time.clock() uart = UART(3,115200,timeout_char=1000) yellow_threshold = [(17, 96, -17, 40, 88, 19),#黄色 (37, 100, 3, 47, -31, 2)] #红色) size_threshold = 2000 def find_max(blobs): max_size=0 max_blob=None 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() blobs = img.find_blobs(yellow_threshold) max_blob = None if blobs: max_blob = find_max(blobs) img.draw_rectangle(max_blob[0:4]) img.draw_cross(max_blob[5], max_blob[6]) if max_blob: pan_error = max_blob.cx()-img.width()/2 if max_blob == None: print("旋转") uart.write("$ZY!") time.sleep_ms(100) if pan_error >0: #此处显示没有定义 print("左移") uart.write("$ZY!") time.sleep_ms(100)
-
你的代码有问题,当没有找到色块的时候,max_blob就是None,pan_error这一行就不会执行,自然就没有定义。