• 免费好用的星瞳AI云服务上线!简单标注,云端训练,支持OpenMV H7和OpenMV H7 Plus。可以替代edge impulse。 https://forum.singtown.com/topic/9519
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。



    • uncaught exception in Timer(4) interrupt handler RuntimeError:这定时器多少有点叛逆。
      通过串口把协议发送过去之后,这个定时器工作了两三秒之后就会报这个异常

      import sensor, image,time,lcd
      from pyb import UART,Timer,LED
      
      
      sensor.reset()
      print("初始化摄像头成功")
      sensor.set_pixformat(sensor.RGB565)
      print("设置彩图成功")
      sensor.set_framesize(sensor.QVGA)
      print("设置图像像素为QVGA")
      sensor.skip_frames(1)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数
      print("设置跳帧成功,如果卡摄像头将这句话注释")
      sensor.set_auto_whitebal(False)
      print("关闭白平衡成功")
      sensor.set_auto_gain(False)
      print("关闭自动增益成功")
      clock = time.clock()
      print("记录帧率")
      lcd.init()
      print("初始化LCE屏幕成功")
      uart = UART(3,115200,8,None,1)#初始化串口
      print("初始化串口成功")
                                                                                        #进行初始化
      #===================================================================================================
      data = []#一个列表
      
      LED_Red = LED(1)
      LED_Green = LED(2)
      LED_Blue = LED(3)
      
      #设置了一个感兴趣区,如果框不住请调整 160宽度是最小的,但是不建议
      left_roi= [0,0,240,120]
      
      #第一个模糊阈值
      red_color0	= (0, 100, -26, 65, -25, 59)
      green_color0  = (0, 100, -73, 21, -40, 65)
      yellow_color0 = (43, 100, -37, 58, -22, 72)
      
      #第二个模糊阈值
      red_color1	= (55, 98, 35, 59, 0, 19)
      green_color1  = (0, 100, -128, -35, -128, 127)
      yellow_color1 = (94, 100, -18, 5, 2, 40)
      
      #交通灯的阈值(经过曝光处理)主要使用这个阈值
      red_threshold_01 = (29, 100, 38, 96, 20, 63)
      green_threshold_01 = (29, 91, -96, -18, 19, 67)
      yellow_threshold_01 = (34, 100, -31, 42, 48, 84)
      
                                                                                         #阈值
      #===================================================================================================
      
      Flaglight = 0 #标志灯
      tim = Timer(4,freq=1)
      tim.deinit()
      QR_flag = 0
      light_flag = 0
      show_numTab = ["0","1","2","3","4","5","6","7","8","9"]
      qr_number = 0
      traffic_number = 0
                                                                                         #设置标志位
      #===================================================================================================
      
      
      #摄像头识别停止或者识别失败的的协议?
      returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
      #摄像头正在识别的协议
      runData	=    [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
      #俩控制摄像头循迹和舵机上下调节的协议
      returnData2 = [0x55,0x02,0x91,0x02,0x02,0x00,0x00,0xBB]
      runData2	= [0x55,0x02,0x91,0x03,0x02,0x00,0x00,0xBB]
      
                                                                                         #返回协议
      #===================================================================================================
      
      #二维码定时器
      def QR_timer(timer):
          global QR_flag,qr_number,returnData
          print("Timer Delaying")
          qr_number = qr_number-1
          if(qr_number == 0):
              qr_number = 2
              QR_flag = 2
              tim.deinit()
      
      #交通灯定时器
      def traffic_light_timer(timer):
          global light_flag,traffic_number,returnData2
          print("Timer Delaying")
          traffic_number = traffic_number-1
          if(traffic_number == 0):
              traffic_number = 9
              light_flag = 2
              tim.deinit()
                                                                                          #定时器延时10s
      #===================================================================================================
      
      #发送函数
      def USART_Send(src,length):
          for i in range(length):
              uart.writechar(src[i])
                                                                                          #发送函数
      #===================================================================================================
      
      #二维码 #协议是回传数据 #QR_flag等于1回传,等于2回传识别失败
      def Color_Check(srcbuf):
          global QR_flag,qr_number
          if(QR_flag == 1):
      
              for code in img.find_qrcodes():
      
                  QR_flag = 0
                  tim.deinit()#停止定时器
                  print(code)
                  qr_Tab = code.payload()
                  uart.writechar(0x55)
                  uart.writechar(0x02)
                  uart.writechar(0x92)
                  uart.writechar(0x01)
                  uart.writechar(len(qr_Tab))
                  for qrdata in qr_Tab:
                      uart.writechar(ord(qrdata))
                  uart.writechar(0xBB)
          if(QR_flag == 2):
              for rdata in returnData:
                  uart.writechar(rdata)
              QR_flag = 0
                                                                                          #二维码识别函数
      #===================================================================================================
      def find_max(blobs):
          max_size=0
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob = blob
                  max_size = blob.pixels()
          return max_blob                                                                 #寻找最大的色块
      #==================================================================================================
      #交通灯的核对
      def traffic_Check(srcbuf):
          global light_flag,traffic_number
          if(light_flag == 1):
      
      
              #看参数根据距离更改
              #,x_stride =5,y_stride=5  增加识别的速度
              # pixels_threshold=30  最小的色块
              # area_threshold=30  最小边界框区域
              # merge=True 合并色块
              # margin=2 合并的时候边界距离
              yellow = img.find_blobs([yellow_threshold_01],roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
              green  = img.find_blobs([green_threshold_01] ,roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
              red    = img.find_blobs([red_threshold_01],  roi=left_roi, x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
      
              if green:
                  max_blob1 = find_max(green)
                  img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
                  img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
      
      
                  print("绿色")
                  tim.deinit()
                  light_flag = 0
                  uart.writechar(0x55)
                  uart.writechar(0x02)
                  uart.writechar(0x02)
                  uart.writechar(0x02)
                  uart.writechar(0x00)
                  uart.writechar(0x00)
                  uart.writechar(0x04)
                  uart.writechar(0xBB)
              elif yellow:
      
                  max_blob1 = find_max(yellow)
                  img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
                  img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
      
                  print("黄色")
                  tim.deinit()
                  light_flag = 0
                  uart.writechar(0x55)
                  uart.writechar(0x02)
                  uart.writechar(0x02)
                  uart.writechar(0x03)
                  uart.writechar(0x00)
                  uart.writechar(0x00)
                  uart.writechar(0x05)
                  uart.writechar(0xBB)
              elif red:
      
                  max_blob1 = find_max(red)
                  img.draw_edges(max_blob1.min_corners(), color=(0,0,255))
                  img.draw_cross(max_blob1[5], max_blob1[6],color=(0,0,255))
                  print("红色")
      
                  tim.deinit()
                  light_flag = 0
                  uart.writechar(0x55)
                  uart.writechar(0x02)
                  uart.writechar(0x02)
                  uart.writechar(0x01)
                  uart.writechar(0x00)
                  uart.writechar(0x00)
                  uart.writechar(0x03)
                  uart.writechar(0xBB)
              #else:
                  #red  = img.find_blobs([red_color1],x_stride=3,y_stride=3)
                  #yellow = img.find_blobs([yellow_color1],x_stride=3,y_stride=3)
                  #green	= img.find_blobs([green_color1],x_stride=3,y_stride=3)
                  #if green:
                      #LED_Red.off()
                      #LED_Green.on()
                      #LED_Blue.off()
                      #tim.deinit()
                      #uart.writechar(0x55)
                      #uart.writechar(0x02)
                      #uart.writechar(0x02)
                      #uart.writechar(0x02)
                      #uart.writechar(0x00)
                      #uart.writechar(0x00)
                      #uart.writechar(0x04)
                      #uart.writechar(0xBB)
                  #elif yellow:
                      #LED_Red.on()
                      #LED_Green.on()
                      #LED_Blue.off()
                      #tim.deinit()
                      #uart.writechar(0x55)
                      #uart.writechar(0x02)
                      #uart.writechar(0x02)
                      #uart.writechar(0x03)
                      #uart.writechar(0x00)
                      #uart.writechar(0x00)
                      #uart.writechar(0x05)
                      #uart.writechar(0xBB)
                  #elif red:
                      #LED_Red.on()
                      #LED_Green.off()
                      #LED_Blue.off()
                      #tim.deinit()
                      #uart.writechar(0x55)
                      #uart.writechar(0x02)
                      #uart.writechar(0x02)
                      #uart.writechar(0x01)
                      #uart.writechar(0x00)
                      #uart.writechar(0x00)
                      #uart.writechar(0x03)
                      #uart.writechar(0xBB)
                  ##else:
                      ##print("meng yi ge")
                      ##uart.writechar(0x55)
                      ##uart.writechar(0x02)
                      ##uart.writechar(0x02)
                      ##uart.writechar(0x03)
                      ##uart.writechar(0x00)
                      ##uart.writechar(0x00)
                      ##uart.writechar(0x05)
                      ##uart.writechar(0xBB)
          if(light_flag == 2):
              for rdata in returnData2:
                  uart.writechar(rdata)
              light_flag = 0
      
                                                           #交通灯识别函数(注释的是 识别阈值更低级别 的识别)
                                                           #可以连用,但是要在串口发送之前关闭定时器跟标志位
      #===================================================================================================
      print("准备进入主循环")
      #55 02 91 01 00 00 00 BB 交通灯识别串口协议
      #55 02 92 01 00 00 00 BB 二维码识别串口协议
      while(True):
          img = sensor.snapshot()#.lens_corr(1.8)#如果二维码识别不到把注释去掉
          img.draw_rectangle( left_roi, color=(255,0,0))
          if(uart.any()):
              print("收到串口发送的数据")
              data = uart.read(8)
              print(data)
              if( len(data) >= 8):
                  if((data[0] == 0x55)&(data[1] == 0x02)&(data[7] == 0xBB)):
      
                      if(data[2] == 0x91):
                          print("识别交通灯")
                          if(data[3] == 0x01):
                              if(light_flag == 0):
                                  light_flag = 1
                                  traffic_number = 9
                                  print("开始识别")
                                  sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
                                  print(sensor.get_exposure_us())
                                  tim.callback(traffic_light_timer)
                              else:
                                  print("正在识别")
                                  for rdata in runData2:
                                      print(rdata)
                                      uart.writechar(rdata)
                          if(data[3] == 0x02):
                               print("停止识别")
                               light_flag = 2
                               tim.deinit()
      
      
      
                      if(data[2] == 0x92):
                          print("识别二维码")
                          if(data[3] == 0x01):
                              if(QR_flag == 0):
                                  QR_flag = 1
                                  qr_number = 9
                                  print("开始识别")
                                  #这里设置自动曝光
                                  sensor.set_auto_exposure(True)
                                  print("二维码的曝光参数%d"%sensor.get_exposure_us())
      
                                  tim.callback(QR_timer)
                              else:
                                  print("正在识别")
                                  for rdata in runData:
                                      print(rdata)
                                      uart.writechar(rdata)
                          if(data[3] == 0x02):
                              print("停止识别")
                              QR_flag = 2
                              tim.deinit()
                                                                                       #主循环函数
      #===================================================================================================
          traffic_Check(data)
          Color_Check(data)
          lcd.display(img)
                                                                                       #重点循环
      #===================================================================================================
      
      

      请在这里粘贴代码

      
      


    • 你的代码需要串口接受数据才能运行,所以我没有办法复现你的问题。
      如果你想要我复现问题的话,需要把串口部分闪掉,只保留运行错误的部分。

      建议1:每个定时器绑定一个回调函数。
      建议1:回调函数从开机到下电都不要停。deinit用不到的,反正你是1秒运行一次,又没有什么损耗。



    • 好了,给串口删了删,189行跟190行放了注释,因为不是从串口读了,会一直执行,所以直接给定了一个协议,不过切换功能需要手动改192行这个debug'的数组,救救娃qwq

      import sensor, image,time,lcd
      from pyb import UART,Timer,LED
      
      
      sensor.reset()
      print("初始化摄像头成功")
      sensor.set_pixformat(sensor.RGB565)
      print("设置彩图成功")
      sensor.set_framesize(sensor.QVGA)
      print("设置图像像素为QVGA")
      sensor.skip_frames(1)#里面如果是数字就是跳过几帧,如果是time=1000就是跳过1s的帧数
      print("设置跳帧成功,如果卡摄像头将这句话注释")
      sensor.set_auto_whitebal(False)
      print("关闭白平衡成功")
      sensor.set_auto_gain(False)
      print("关闭自动增益成功")
      clock = time.clock()
      print("记录帧率")
      lcd.init()
      print("初始化LCE屏幕成功")
      uart = UART(3,115200,8,None,1)#初始化串口
      print("初始化串口成功")
                                                                                        #进行初始化
      #===================================================================================================
      data = []#一个列表
      
      LED_Red = LED(1)
      LED_Green = LED(2)
      LED_Blue = LED(3)
      
      #设置了一个感兴趣区,如果框不住请调整 160宽度是最小的,但是不建议
      left_roi= [0,0,240,120]
      
      #第一个模糊阈值
      red_color0	= (0, 100, -26, 65, -25, 59)
      green_color0  = (0, 100, -73, 21, -40, 65)
      yellow_color0 = (43, 100, -37, 58, -22, 72)
      
      #第二个模糊阈值
      red_color1	= (55, 98, 35, 59, 0, 19)
      green_color1  = (0, 100, -128, -35, -128, 127)
      yellow_color1 = (94, 100, -18, 5, 2, 40)
      
      #交通灯的阈值(经过曝光处理)主要使用这个阈值
      red_threshold_01 = (29, 100, 38, 96, 20, 63)
      green_threshold_01 = (29, 91, -96, -18, 19, 67)
      yellow_threshold_01 = (34, 100, -31, 42, 48, 84)
      
                                                                                         #阈值
      #===================================================================================================
      
      Flaglight = 0 #标志灯
      tim = Timer(4,freq=1)
      tim.deinit()
      
      tim1=Timer(3,freq=1)
      tim1.deinit()
      
      
      QR_flag = 0
      light_flag = 0
      show_numTab = ["0","1","2","3","4","5","6","7","8","9"]
      qr_number = 0
      traffic_number = 0
                                                                                         #设置标志位
      #===================================================================================================
      
      
      #摄像头识别停止或者识别失败的的协议?
      returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
      #摄像头正在识别的协议
      runData	=    [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
      #俩控制摄像头循迹和舵机上下调节的协议
      returnData2 = [0x55,0x02,0x91,0x02,0x02,0x00,0x00,0xBB]
      runData2	= [0x55,0x02,0x91,0x03,0x02,0x00,0x00,0xBB]
      
                                                                                         #返回协议
      #===================================================================================================
      
      #二维码定时器
      def QR_timer(timer):
          global QR_flag,qr_number,returnData
          print("Timer Delaying")
          qr_number = qr_number-1
          if(qr_number == 0):
              qr_number = 2
              QR_flag = 2
              tim1.deinit()
      
      #交通灯定时器
      def traffic_light_timer(timer):
          global light_flag,traffic_number,returnData2
          print("Timer Delaying")
          traffic_number = traffic_number-1
          if(traffic_number == 0):
              traffic_number = 9
              light_flag = 2
              tim.deinit()
                                                                                          #定时器延时10s
      #===================================================================================================
      
      #发送函数
      def USART_Send(src,length):
          for i in range(length):
              uart.writechar(src[i])
                                                                                          #发送函数
      #===================================================================================================
      
      #二维码 #协议是回传数据 #QR_flag等于1回传,等于2回传识别失败
      def Color_Check(srcbuf):
          global QR_flag,qr_number
          if(QR_flag == 1):
      
              for code in img.find_qrcodes():
      
                  QR_flag = 0
                  tim1.deinit()#停止定时器
                  print(code)
                  qr_Tab = code.payload()
                  
          if(QR_flag == 2):
              
              QR_flag = 0
                                                                                          #二维码识别函数
      #===================================================================================================
      
      #交通灯的核对
      def traffic_Check(srcbuf):
          global light_flag,traffic_number
          if(light_flag == 1):
      
              LED_Red.on()
              LED_Green.on()
              LED_Blue.on()
      
      
              LED_Red.off()
              LED_Green.off()
              LED_Blue.off()
      
      
              #看参数根据距离更改
              #,x_stride =5,y_stride=5  增加识别的速度
              # pixels_threshold=30  最小的色块
              # area_threshold=30  最小边界框区域
              # merge=True 合并色块
              # margin=2 合并的时候边界距离
              yellow = img.find_blobs([yellow_threshold_01],roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
              green  = img.find_blobs([green_threshold_01] ,roi=left_roi,x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
              red    = img.find_blobs([red_threshold_01],  roi=left_roi, x_stride =5,y_stride=5, pixels_threshold=30, area_threshold=30, merge=True, margin=2)
      
              if green:
                  LED_Red.off()
                  LED_Green.on()
                  LED_Blue.off()
      
                  print("绿色")
                  tim.deinit()
                  light_flag = 0
                 
              elif yellow:
                  LED_Red.on()
                  LED_Green.on()
                  LED_Blue.off()
      
                  print("黄色")
                  tim.deinit()
                  light_flag = 0
      
              elif red:
                  LED_Red.on()
                  LED_Green.off()
                  LED_Blue.off()
                  print("红色")
      
                  tim.deinit()
                  light_flag = 0
      
        
          if(light_flag == 2):
              
              light_flag = 0
      
                                                           #交通灯识别函数(注释的是 识别阈值更低级别 的识别)
                                                           #可以连用,但是要在串口发送之前关闭定时器跟标志位
      #===================================================================================================
      print("准备进入主循环")
      
      #Debug 第1位02是交通灯 01是二维码
      #        第2位01是开始 02是关闭识别
      
      Debug = [0x55,0x01,0x01,0x00,0x00,0x00,0x00,0xBB]
      
      while(True):
          img = sensor.snapshot()#.lens_corr(1.8)#如果二维码识别不到把注释去掉
          img.draw_rectangle( left_roi, color=(255,0,0))
      
          if(len(Debug) >= 5):
      
              if((Debug[0] == 0x55)):
      
                  if( (Debug[1] == 0x02) ):
                      Debug[1]=0x99 #debug用(只执行一便),如果要切换交通灯跟二维码需要重启设备
                      print("识别交通灯")
                      if((Debug[2] == 0x01)):
                          if(light_flag == 0):
                              light_flag = 1
                              traffic_number = 9
                              print("开始识别")
                              sensor.set_auto_exposure(False, 1000)#这里设置曝光时间
                              print(sensor.get_exposure_us())
                              tim.callback(traffic_light_timer)
                          else:
                              print("正在识别")
      
                      if((Debug[2] == 0x02)):
                           print("停止识别")
                           light_flag = 2
                           tim.deinit()
      
      
      
                  if( (Debug[1] == 0x01) ):
                      Debug[1]=0x99 #debug用(只执行一便),如果要切换交通灯跟二维码需要重启设备
                      print("识别二维码")
                      if((Debug[2] == 0x01)):
                      
                          if(QR_flag == 0):
                              QR_flag = 1
                              qr_number = 9
                              print("开始识别")
                              #这里设置自动曝光
                              sensor.set_auto_exposure(True)
                              print("二维码的曝光参数%d"%sensor.get_exposure_us())
      
                              tim1.callback(QR_timer)
                          else:
                              print("正在识别")
      
                      if((Debug[2] == 0x02)):
                          print("停止识别")
                          QR_flag = 2
                          tim1.deinit()
                                                                                       #主循环函数
      #===================================================================================================
          traffic_Check(data)
          Color_Check(data)
          lcd.display(img)
                                                                                       #重点循环
      #===================================================================================================
      
      


    • @kidswong999uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。 中说:

      你的代码需要串口接受数据才能运行,所以我没有办法复现你的问题。
      如果你想要我复现问题的话,需要把串口部分闪掉,只保留运行错误的部分。

      建议1:每个定时器绑定一个回调函数。
      建议1:回调函数从开机到下电都不要停。deinit用不到的,反正你是1秒运行一次,又没有什么损耗。

      代码重新修改了一下



    • 我是用OpenMV4 H7 Plus,固件版本4.3.3,没有出现错误。



    • @kidswong999uncaught exception in Timer(4) interrupt handler 这定时器多少有点叛逆。 中说:

      我是用OpenMV4 H7 Plus,固件版本4.3.3,没有出现错误。

      我现在手里有两个设备,一个可以工作(型号看不出来,固件版本是4.多),另外一个型号是MV3R2(固件版本是3.3.1),这个R2的就一直报错,也能运行就是摄像头工作一段之后串口就开始丢数据,而且一直报定时器错误,会不会升级一下固件会好一点