logo

关于运行时Extint中断中的MemoryError错误

2018-04-14 12:46
4
Donkey_F

问题描述:

完成程序提供如下,程序目的是在接收到外部中断以后先读取文件,然后依据读取到文件中的序号创建名如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")
2018-04-14 15:18
I
网红小智智 星瞳科技

太长了懒得看了

你的编程思路在OpenMV 上是不合适的

- 尽量不要用中断

- 非要用的话,中断服务函数越短越好

I
网红小智智 星瞳科技

请对比一下两种编程思路:

<https://forum.singtown.com/topic/371>

2018-04-16 09:31
4
Donkey_F

@kidswong999

我确实觉得你的实现是简单有效的,我思考了一下,用状态机转换的思想其实还是可以完全替代定时器+外部中断的,确实我发现一个很致命的地方,OpenMV管脚连上杜邦线以后,只要杜邦线有物理上的抖动,很容易引起OpenMV管脚上接收到电平的变化。

2018-04-16 09:36
I
网红小智智 星瞳科技

你这个问题和这个帖子无关,新的问题请新开一个帖子。