badtaste
Posts: 2
Joined: Thu Nov 02, 2017 1:53 pm

Creat counter with RPi.GPIO add_event_detect callback performance

Thu Nov 02, 2017 2:40 pm

Hello,
it is not clear to me what happens when new event is(should be) detected by add_event_detect() before the callback function from previous event is finished? I expect quite high frequency of input impulses (1kHz). I want to count impulses in time intervals.

I guess the same problem can be with Python GPIO, pigpio library or in C with equivalent libraries.

Code: Select all

def increaseCounter(channel):
    global counter
    counter = counter +1
    # something more to do ...
    
GPIO.add_event_detect(channel, GPIO.RISING, callback=increaseCounter)

while True:
       sleep(interval)
       # Read counter and add time of reading ...
Thanks Michal

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Creat counter with RPi.GPIO add_event_detect callback performance

Thu Nov 02, 2017 9:07 pm

My understanding is that it will call another thread of the new call back function whether the last 1 is finished or not. If having mutli threads of same fuction is a problem then you can disable the call back at the beginning of the function the re enable it at the end of the function that way it can't be called while 1 instance of the thread is already running.

badtaste
Posts: 2
Joined: Thu Nov 02, 2017 1:53 pm

Re: Creat counter with RPi.GPIO add_event_detect callback performance

Thu Nov 02, 2017 10:22 pm

Hello,
thanks for your explanation (opinion).

1) I am OK with multi threads callbacks if they share global variable "counter" so all events (new high impulse) increase counter by 1. But I am not sure about it.

2) If I disable callback I would expect that new events (new high impulses) will be ignored which is what I do not want

3) Meantime I have done test with professional impulse generator and I can see temporally 0.5% error. Frequency counted from data shows 995Hz instead of 1kHz. That is OK CPU can be quite busy but I would expect compensation after that in frequency about 1005Hz. But this is not the case. So some signals have to be lost and not counted. Is it possible?

Thanks Michal

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Creat counter with RPi.GPIO add_event_detect callback performance

Thu Nov 02, 2017 10:57 pm

I think I've seen a few comments about the callback system missing occasional edges. What do you get if you just run your counter as a thread with a loop forever and a time.sleep(0.0002), incrementing when last_val != 0 and val == 0?

PS quick test on a RPi3 I can read at 230kHz so reading at 5kHz should be fine. Something along the lines of:

Code: Select all

def increaseCounter(channel):
    global counter
    last_reading = False
    while True: # or use a global variable to stop thread on quitting
        reading = GPIO.input(channel)
        if reading and not last_reading:
            counter = counter +1
            # something more to do ...
        last_reading = reading
        sleep(0.0002)

t = threading.Thread(target=increaseCounter, args=(channel, ))
t.start()

while True:
       sleep(interval)
       # Read counter and add time of reading ...
PPS see comment from @joan below. pigpio was the first thing that occurred to me when I saw this thread but I forgot to mention it (in my curiosity about the callback issue)!
Last edited by paddyg on Fri Nov 03, 2017 9:21 am, edited 1 time in total.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Creat counter with RPi.GPIO add_event_detect callback performance

Fri Nov 03, 2017 2:31 am

(My) pigpio samples in the background. It may give the results you prefer.

Return to “Python”