• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • local variable referenced before assignment,求解



    • 0_1690859370691_e7090fdf-aa40-4763-8a17-784d6626d613-image.png

      import sensor, image, time,math,pyb
      
      from pid import PID
      from pyb import Servo
      from pyb import UART,LED
      import json
      import ustruct
      
      pan_servo=Servo(1) #定义两个舵机
      tilt_servo=Servo(2)
      
      pan_servo.calibration(500,2500,500)
      tilt_servo.calibration(500,2500,500)
      
      red_threshold_01 = ((14, 88, 24, 116, -18, 106));
      bule_threshold_01 = ((16, 65, -20, 35, -64, -21));
      yello_threshold_01 = ((53, 71, -14, 0, -7, 22));
      
      
      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) # Let new settings take affect.
      sensor.set_auto_gain(False) # 关闭自动增益
      sensor.set_auto_whitebal(False) # turn this off.关闭白平衡
      #sensor.set_vflip(True)#垂直方向翻转
      clock = time.clock() # Tracks FPS.
      
      uart = UART(3,115200)
      uart.init(115200, bits=8, parity=None, stop=1)
      
      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 sending_data(flag):
          global uart;
          data = ustruct.pack("<bbhb",
                         0x2C,
                         0x12,
                         int(flag),
                         0x5B)
          uart.write(data);
      
      while(True):
      
              flag = None
              clock.tick()
              img = sensor.snapshot()
              blobs = img.find_blobs([red_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);      #红色物块
              blobs1 = img.find_blobs([bule_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10); #绿色物块
              blobs2 = img.find_blobs([yello_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);   #黄色物块
      
              if blobs:
                  max_blob = find_max(blobs)#判断最大的色块为哪一个
                  #如果找到了目标颜色
                  print("red")
                  flag = 1
                  for b in blobs:
                  #迭代找到的目标颜色区域
                      # Draw a rect around the blob.
                      img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
                      #用矩形标记出目标颜色区域
                      img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
                      #在目标颜色区域的中心画十字形标记
                      data = bytearray([0x2C,0x12,flag,0x5B])
                      uart.write(data)
      
                  pan_error = max_blob.cx()-img.width()/2
                  tilt_error = max_blob.cy()-img.height()/2
      
                  print("pan_error: ", pan_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
                  tilt_output=tilt_pid.get_pid(tilt_error,1)
                  print("pan_output",pan_output)
                  pan_servo.angle(pan_servo.angle()+pan_output)#控制舵机转动的角度
                  tilt_servo.angle(tilt_servo.angle()-tilt_output)
              elif blobs1:
                  max_blob = find_max(blobs)#判断最大的色块为哪一个
                  #如果找到了目标颜色
                  print("bule")
                  flag = 2
                  for b in blobs1:
                  #迭代找到的目标颜色区域
                      # Draw a rect around the blob.
                      img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
                      #用矩形标记出目标颜色区域
                      img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
                      #在目标颜色区域的中心画十字形标记
                      data = bytearray([0x2C,0x12,flag,0x5B])
                      uart.write(data)
      
                  pan_error = max_blob.cx()-img.width()/2
                  tilt_error = max_blob.cy()-img.height()/2
      
                  print("pan_error: ", pan_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
                  tilt_output=tilt_pid.get_pid(tilt_error,1)
                  print("pan_output",pan_output)
                  pan_servo.angle(pan_servo.angle()+pan_output)#控制舵机转动的角度
                  tilt_servo.angle(tilt_servo.angle()-tilt_output)
              elif blobs2:
                  max_blob = find_max(blobs)#判断最大的色块为哪一个
                  #如果找到了目标颜色
                  print("yello")
                  flag = 1
                  for b in blobs2:
                  #迭代找到的目标颜色区域
                      # Draw a rect around the blob.
                      img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
                      #用矩形标记出目标颜色区域
                      img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
                      #在目标颜色区域的中心画十字形标记
                      data = bytearray([0x2C,0x12,flag,0x5B])
                      uart.write(data)
      
                  pan_error = max_blob.cx()-img.width()/2
                  tilt_error = max_blob.cy()-img.height()/2
      
                  print("pan_error: ", pan_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
                  tilt_output=tilt_pid.get_pid(tilt_error,1)
                  print("pan_output",pan_output)
                  pan_servo.angle(pan_servo.angle()+pan_output)#控制舵机转动的角度
                  tilt_servo.angle(tilt_servo.angle()-tilt_output)
      


    • 92行应该是blobs1而不是blobs.

      120行应该是blobs2而不是blobs.