use of time.avg() in a function

Discussion related to "under the hood" OpenMV topics.
User avatar
chrisPyres66
Posts: 61
Joined: Mon Jul 16, 2018 9:22 am
Location: France

use of time.avg() in a function

Postby chrisPyres66 » Thu Nov 22, 2018 10:08 am

Dear all,

I try to get the time between two interrupts from a push on a button with the code :

Code: Select all

import time, pyb

testClock = time.clock()

def test(line) :
   print( testClock.avg() )
   testClock.tick()
   return

testClock.tick()
pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
   time.sleep(10)
In openmvIDE, i have the error :
Uncaught exception in ExtInt interrupt handler line 12
MemoryError:

Any help could be pleased, Thanks,
User avatar
iabdalkader
Posts: 654
Joined: Sun May 24, 2015 3:53 pm

Re: use of time.avg() in a function

Postby iabdalkader » Fri Nov 23, 2018 3:34 pm

You can't call any functions that allocate memory in an interrupt handler. Try something like this:

Code: Select all

import time, pyb

def test(arg):    
    global ticks_passed, last_tick, extint, switch
    switch = True
    extint.disable()
    ticks_passed = time.ticks() - last_tick
    last_tick = time.ticks()
    
switch = False
last_tick = time.ticks()
ticks_passed = 0
extint = pyb.ExtInt('P7', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, test)

while(True):
    if (switch):
        switch = False
        print(ticks_passed)
        extint.enable()

Return to “Technical Discussion”

Who is online

Users browsing this forum: Bing [Bot] and 3 guests