星瞳实验室APP,快速收到回复
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 为什么红色和圆形识别都无法实现?



    • import sensor, image, time
      import json
      import math
      from pyb import UART #通过pyb模块引入UART类
      from pid import PID
      from pyb import Servo

      pan_servo=Servo(1)
      tilt_servo=Servo(2)

      red_threshold =(60, 70, 40, 75, 30, 50) #(60,65,40,60,40,45)

      pan_pid = PID(p=0.2 , i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
      tilt_pid = PID(p=0.18, 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_vflip(F)
      sensor.set_auto_gain(True) #关闭自动亮度增益,太暗了可以自行打开
      sensor.set_auto_whitebal(False) # turn this off.
      clock = time.clock() # Tracks FPS.
      uart = UART(3, 115200)#设置波特率,串口号
      #确定最大球
      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

      K=1250 #测距参数,在2.5m附近较准确

      while(True):
      clock.tick() # Track elapsed milliseconds between snapshots().
      img = sensor.snapshot() # Take a picture and return the image.
      blobs = img.find_circles(threshold = 3000, x_margin = 10, y_margin = 10, r_margin = 20,r_min = 5, r_max = 100, r_step = 2)
      blobs = img.find_blobs([red_threshold])
      #寻块,舵机转动
      if blobs:
      max_blob = find_max(blobs)
      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)
      

      #if blobs == 1:测距部分
      b = blobs[0]
      img.draw_rectangle(b[0:4]) # rect
      #img.draw_cross(b[5], b[6]) # cx, cy
      Lm = (b[2]+b[3])/2
      length = K/Lm
      print("length:",length)
      #检测点的坐标
      if blobs:
      data2=[]
      for b in blobs:
      # img.draw_rectangle(b.rect())
      # img.draw_cross(b.cx(), b.cy())
      data2.append((b.cx(),b.cy()))
      x = str(max_blob.cx())
      y = str(max_blob.cy())
      for a in range(2):
      if (len(x)!=3):
      x='0'+x
      for b in range(2):
      if (len(y)!=3):
      y='0'+y
      uart.write('x'+x+'y'+y+'\r\n')
      print('x'+x+'y'+y+'\n') #得到x,y坐标
      tan=abs(max_blob.cx()-80)/length #利用dx和测得的距离求出直角三角形的tan
      a=math.atan(tan)*57.325 #反三角函数得角度

              print("arctan=",a)
          else:
              print("not found!")
      

      错误提示
      Traceback (most recent call last):
      File "", line 41, in
      Exception: IDE interrupt
      MicroPython v1.9.4-4548-g24c934f79 on 2019-03-23; OPENMV4 with STM32H743
      Type "help()" for more information.