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



    • import sensor, image, time
      
      from pid import PID
      from pyb import Servo
      
      pan_servo=Servo(1)
      tilt_servo=Servo(2)
      
      pan_servo.calibration(500,2500,500)
      tilt_servo.calibration(500,2500,500)
      
      red_threshold  = (13, 49, 18, 61, 6, 47)
      
      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_auto_gain(False)
      sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
      sensor.set_framesize(sensor.  QVGA) # or sensor.QVGA (or others)
      sensor.skip_frames(time=900) # Let new settings take affect.
      sensor.set_auto_exposure(False, 1000)#在这里调节曝光度,调节完可以比较清晰地看清激光点
      sensor.set_auto_whitebal(False) # turn this off.
      sensor.set_auto_gain(False) # 关闭增益(色块识别时必须要关)
      
      
      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() # Track elapsed milliseconds between snapshots().
          img = sensor.snapshot() # Take a picture and return the image.
      
          blobs = img.find_blobs(threshold,x_stride=1, y_stride=1, area_threshold=0, pixels_threshold=0,merge=False,margin=1)
      
          if len(blobs)>=1 :#有色块
              # Draw a rect around the blob.
              b = blobs[0]
              #img.draw_rectangle(b[0:4]) # rect
              cx = b[5]
              cy = b[6]
              for i in range(len(blobs)-1):
                  #img.draw_rectangle(b[0:4]) # rect
                  cx = blobs[i][5]+cx
                  cy = blobs[i][6]+cy
              cx=int(cx/len(blobs))
              cy=int(cy/len(blobs))
              #img.draw_cross(cx, cy) # cx, cy
              print(cx,cy)
              return int(cx), int(cy)
          return -1, -1 #表示没有找到
      

      0_1690975204820_fb196ff5-a9f1-4905-8ab8-d5df1bd04ed1-image.png



    • while True不是函数,是主循环,不能返回