@kidswong999
我确实觉得你的实现是简单有效的,我思考了一下,用状态机转换的思想其实还是可以完全替代定时器+外部中断的,确实我发现一个很致命的地方,OpenMV管脚连上杜邦线以后,只要杜邦线有物理上的抖动,很容易引起OpenMV管脚上接收到电平的变化。
Donkey_F 发布的帖子
-
RE: 关于运行时Extint中断中的MemoryError错误
-
关于运行时Extint中断中的MemoryError错误
问题描述:
完成程序提供如下,程序目的是在接收到外部中断以后先读取文件,然后依据读取到文件中的序号创建名如video_1.mjpeg的mjpeg对象,当我将外部中断中的“File operation部分放到程序初始化的部分即‘/Creation of mjpeg object & read file operation’”时,程序运行没有问题;当然地,我需要在不断电的情况下录制多个视频,我就应该将这段代码放置到外部中断的callbackfunction中,但这样一来运行时就会弹出错误:
line = 13
No. 1
Uncaught exception in ExtInt interrupt handler line 13
MemoryError:请问应该如何解决这个/类错误呢?
代码:
import sensor, image, time, pyb, mjpeg # -----</Definition of variables>----- sleep_flag = 1 index_i = 0 time_1s = 0 TIME_ONEMIN_INSEC = 60 TIME_FIVEMIN_INSEC = TIME_ONEMIN_INSEC * 5 TIMER_THREE_WORKING_FLAG = 0 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 & read file operation>----- f = open('/number.txt', 'r') context = f.read() index = int(context) print("Current index is", index) f.close() m = None # <Creation of mjpeg object & read file operation/>----------------------------------------------------------- # -----</Timer>----- # Timer_3 callback function def tick_3(timer): global time_1s global TIMER_THREE_WORKING_FLAG time_1s = time_1s + 1 print('time_1s =', time_1s) if time_1s >= 10: time_1s = 0 sensor.sleep(True) TIMER_THREE_WORKING_FLAG = 0 print("TIMER_THREE_WORKING_FLAG =", TIMER_THREE_WORKING_FLAG) timer.deinit() # Configuration of timer_3 tim_3 = pyb.Timer(3, freq = 1) tim_3.deinit() # Timer_4 callback function def tick_4(timer): global time_1s global sleep_flag global TIMER_THREE_WORKING_FLAG time_1s = time_1s + 1 print('time_1s =', time_1s) if time_1s >= TIME_FIVEMIN_INSEC: time_1s = 0 TIMER_THREE_WORKING_FLAG = 1 #If possible, stop and save the recording firstly sleep_flag = 1 print("sleep_flag =", sleep_flag) tim_3.init(freq = 1) tim_3.callback(tick_3) print("Timer_3 has been activated!") print("TIMER_THREE_WORKING_FLAG =", TIMER_THREE_WORKING_FLAG) #m.close(clock.fps()) #Uncomment it later timer.deinit() # Configuration 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 index global sleep_flag global time_1s global tim_4 global tim_3 global TIMER_THREE_WORKING_FLAG global m print("line =", line) index_i = index_i + 1; print("No.", index_i) # Use the idea of state machine to think about these codes, there are only three states within this machine if sleep_flag == 1 and TIMER_THREE_WORKING_FLAG == 0: # Sensor is sleeping and Timer_3 is not working sensor.sleep(False) time_1s = 0 # File operation f = open('/number.txt', 'r') context = f.read() index = int(context) print("Current index is", index) index = index + 1 f.close() f = open('/number.txt', 'w') print("%d is gonna to be written into the file" %index) f.write("%d" %index) f.close() m = mjpeg.Mjpeg("video_%d.mjpeg" %index) tim_4.init(freq = 1) # To design a stoppable recording, I need to design carefully here tim_4.callback(tick_4) print("Timer_4 has been activated!") sleep_flag = 0 elif TIMER_THREE_WORKING_FLAG == 1: #sleep_flag == 0 or TIMER_THREE_WORKING_FLAG == 1 pass else: # Timer_4 is still working time_1s = 0 sleep_flag = 1 print("sleep_flag =", sleep_flag) tim_3.init(freq = 1) tim_3.callback(tick_3) TIMER_THREE_WORKING_FLAG = 1 print("Timer_3 has been activated!") print("TIMER_THREE_WORKING_FLAG =", TIMER_THREE_WORKING_FLAG) #m.close(clock.fps()) #We should put this one in the while loop tim_4.deinit() # 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: for i in range(200): # This vedio lasts exactly ten seconds clock.tick() m.add_frame(sensor.snapshot()) #print(clock.fps()) if TIMER_THREE_WORKING_FLAG == 1: # Which means we should store the video now m.close(clock.fps()) print("Bye~See u next time") print("Owned by Donkey_F, all rights reserved")
-
RE: 关于错误:RuntimeError: Sensor Timeout!!
啊,确实是好方法,谢谢!
不过我最终想要实现的其实是接收到外部中断以后,通过翻转一个flag的值,然后在while循环里面进行一次录像操作(依据官方的例程,for i in 200那个循环,其实就是录像10s),所以想while里面到时候就放这一块录10s的代码,然后不断循环。
但仔细想一想,其实你提供的程序里面通过utime.tick_diff也实现了这种操作。我设计初衷其实是想留出一个灵活的方法给上层调用的——第一次下降沿开始录像,如无意外录5min;但调用者也可以通过产生第二个下降沿的方法中断录像。 -
关于错误: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()
-
在OpenMV的main.py中写入了休眠函数,导致无法进行USB连接
问题描述:
我不小心将未经测试的休眠函数pyb.stop()写到了程序的基本上是最前面了……然后自然而然地,OpenMV上电以后自动运行此函数,导致电脑无法检测到M7的USB连接,请问要怎么做才能擦除/重初始化M7的main.py……谢谢! -
RE: 关于OpenMV M7读取外部输入电平失灵/抖动的问题
啊!如你所说,我认为应该是STM32IO控制有问题,M7接下拉以后接到STM32的Vcc管脚,能检测到电平变化,确实用万用表再仔细测量发现,IO输出无电平变化,现在在查找原因,后续我会说明解决方法的,谢谢你
-
RE: 关于OpenMV M7读取外部输入电平失灵/抖动的问题
是这样子的,STM32开发板上我设置为按一下按键,该IO口的电平翻转,并且LED灯状态翻转,用万用表测试过,确实电平会改变。
-
关于OpenMV M7读取外部输入电平失灵/抖动的问题
问题描述:
我的OpenMV M7现在遇到了一个问题——用杜邦线将其中一个IO口与STM32开发板某一个IO口进行连接以后(杜邦线连接稳定,无松动)改变STM32开发板该端口的输出电平,M7无法正常读取其电平值,但当我按下STM32开发板的复位键时,M7能检测到该管脚那一瞬间的电平变化,这又是为什么呢?细节:
STM32该端口采用推挽输出,M7的Pin配置为输入模式,接下拉电阻M7的测试代码:
import sensor, pyb, time pin7_in = pyb.Pin('P8', pyb.Pin.IN, pyb.Pin.PULL_DOWN) while (True): time.sleep(100) print(pin7_in.value())