• 星瞳实验室APP,快速收到回复。扫描二维码或者点击 https://singtown.com/app/
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 每一个新的提问,单独发一个新帖子
  • 描述一定要详细 描述一定要详细 描述一定要详细
  • 请贴出具体的的目的,就是你要做什么。
  • 请贴出具体的代码,与报错提示
  • 代码一定要让别人可以运行的文本,不要贴图片
  • 关于错误:RuntimeError: Sensor Timeout!!



    • 问题描述:
      我现在想要编写一段程序,实现以下功能:程序初始化以后关闭感光元件,等待外部中断的到来,外部中断接收到以后启动定时器,12秒以后关闭定时器,并且再次关闭感光元件,而while循环主要就是通过一个变量判断是否进行采图工作。现在程序能实现接收外部中断,并启动定时器,而且能够在12秒以后在terminal中打印出sleep_flag = 1的语句(证明定时器能够正常工作),但打印完以后马上跳出来一个错误:

      Traceback (most recent call last):
      File "", line 96, in
      RuntimeError: Sensor Timeout!!

      请教一下各位,这是什么原因引起的呢?应该如何解决呢?谢谢!

      程序源码:

      import sensor, image, time, pyb
      
      
      # -----</Definition of variables>-----
      
      sleep_flag = 1
      index_i = 0
      time_1s = 0
      
      TIME_ONEMIN_INSEC = 12
      RED_LED_PIN = 1
      GREEN_LED_PIN = 2
      BLUE_LED_PIN = 3
      
      # <Definition of variables/>------------------------------------------------------------
      
      
      sensor.reset()                      # 重置并初始化传感器。
      sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
      sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
      sensor.skip_frames(time = 2000)     # Capture frames for 2000ms.
      pyb.LED(RED_LED_PIN).on()
      time.sleep(1000)
      pyb.LED(RED_LED_PIN).off()
      sensor.sleep(True)                  # 启用传感器睡眠模式(节省约40mA)
      
      
      
      # -----</Definition of Clock object>-----
      
      clock = time.clock()
      
      # </Definition of Clock object>---------------------------------------------------------
      
      
      # -----</Creation of mjpeg object>-----
      
      #???
      
      # <Creation of mjpeg object/>-----------------------------------------------------------
      
      
      # -----</Timer>-----
      def tick_4(timer):
          global time_1s
          global sleep_flag
          time_1s = time_1s + 1
          print('time_1s =', time_1s)
          if time_1s >= TIME_ONEMIN_INSEC:
              time_1s = 0
              sleep_flag = 1
              print("sleep_flag =", sleep_flag)
              sensor.sleep(True)
              timer.deinit()
      
      # defination of timer_4
      tim_4 = pyb.Timer(4, freq = 1)
      tim_4.deinit()
      #tim_4.callback(tick_4)
      # <Timer/>------------------------------------------------------------------------------
      
      
      # -----</External interrupt>-----
      
      def ext_callback(line):
          global index_i
          global sleep_flag
          global time_1s
          global tim_4
          print("line =", line)
          index_i = index_i + 1;
          print("No.", index_i)
          sensor.reset()                      # 重置并初始化传感器。
          sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
          sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
          sensor.skip_frames(time = 2000)     # Capture frames for 2000ms.
          time_1s = 0
          tim_4.init(freq = 1)
          tim_4.callback(tick_4)
          print("Timer_4 has been activated!")
          if sleep_flag == 1:
              sleep_flag = 0
          #else:
             # sleep_flag = 1
      
      # Falling edge triger
      extint = pyb.ExtInt(pyb.Pin('P8'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, ext_callback)
      
      # <External interrupt/>-----------------------------------------------------------------
      
      
      # Recording up to 5min, then sleep again
      
      while (True):
          if sleep_flag == 0:
              sensor.snapshot()
      


    • sensor.sleep(True)之后,就不能snapshopt()了,我把tick_4里面的sensor.sleep(True)注释掉可能就好了。

      PS:你这个代码也写的实在没必要,一秒一秒的定时,根本不需要自己用定时器,http://doc.singtown.com/openmv_zh/openmvcam/quickref.html#id2,直接time.ticks_diff(time.ticks_ms(), start),就行了。其实外部中断也没什么必要,直接while里检查就行了。

      比如整个代码只有:

      import sensor, image, utime, pyb
      from pyb import Pin
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      
      p8 = pyb.Pin('P8', Pin.IN, Pin.PULL_UP)
      
      while(True):
          if p8.value()==0:
              start = utime.ticks_ms()
              print("sensor active~~~")
              sensor.sleep(False)
              sensor.snapshot()
              print("snapshot")
              print("wait 12s")
              while utime.ticks_diff(utime.ticks_ms(), start)<12000:
                  pass
              print("sensor sleep~~~")
              sensor.sleep(True)
      


    • 啊,确实是好方法,谢谢!
      不过我最终想要实现的其实是接收到外部中断以后,通过翻转一个flag的值,然后在while循环里面进行一次录像操作(依据官方的例程,for i in 200那个循环,其实就是录像10s),所以想while里面到时候就放这一块录10s的代码,然后不断循环。
      但仔细想一想,其实你提供的程序里面通过utime.tick_diff也实现了这种操作。我设计初衷其实是想留出一个灵活的方法给上层调用的——第一次下降沿开始录像,如无意外录5min;但调用者也可以通过产生第二个下降沿的方法中断录像。