picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

event detection response

Wed Feb 26, 2020 9:12 pm

Using pi3B, buster with GPIO. I set up an event response:

GPIO.add_event_detect(13, GPIO.RISING, callback=mysensor)

It works...when GPIO goes high, mysensor is run. However using a scope, it appears that from the time gpio goes high, to the time mysensor executes is a very long time (500-600 microseconds). What priority does the event have (and can it be raised)?...Does the program drop everything it is doing to respond immediately? Or does something have to complete first before the response begins? If so, what needs completed first?

PhatFil
Posts: 1672
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: event detection response

Wed Feb 26, 2020 10:14 pm


User avatar
neilgl
Posts: 2102
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: event detection response

Wed Feb 26, 2020 11:19 pm

What is it that needs a very fast response?

Heater
Posts: 15834
Joined: Tue Jul 17, 2012 3:02 pm

Re: event detection response

Wed Feb 26, 2020 11:29 pm

I presume that is Python we are looking at. In which case I would say you are doing pretty well. Python is slow as molasses for this kind of thing.

Suggest working in C/C++, Pascal, Rust or whatever compiles down to real executatable instructions. If that is not fast enough there are ways to squeeze more speed out by isolating a core from Linux scheduling and running your code on that core. See "isolcpus" and "taskset".

What latency are you aiming for anyway?
Memory in C++ is a leaky abstraction .

picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

Re: event detection response

Wed Feb 26, 2020 11:55 pm

The answers sort of ignore the question...the question is

When (what conditions) does the event handler take over? If you are in the middle of some calculation, does the calculation get interrupted & then resume afterwards? What if the calculation was a loop that took 2 minutes---would it require 2 minutes before the mysensor code runs? In other words, is the interruption immediate, or does something else have to happen? If so, what?

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

Re: event detection response

Thu Feb 27, 2020 8:54 am

No point guessing. We would have to see all the code.

The response seems slow. I'd expect circa 50 µs latency between level change and Python being notified. Between Python and callback is anyone's guess, but your times seem extreme.

picandies
Posts: 240
Joined: Wed Nov 26, 2014 5:13 pm

Re: event detection response

Thu Feb 27, 2020 10:24 pm

I am also doing some gui graphics for motor control & wondering when the interrupt is serviced? Do the interrupts have to wait for a code block to complete (such as a python def block)? Or perhaps the event reaction is actually being polled & the polling only occurs infrequently?

What is add_event_detect really doing?

User avatar
neilgl
Posts: 2102
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: event detection response

Fri Feb 28, 2020 2:39 pm

I don't know how that python callback works in detail. On my cheap 2 channel USB scope I am seeing 370 µs between the rising edge of a pushbutton and the output to an LED in the assocated callback funtion. Using Pi3B Stretch GUI and python3.

Return to “Python”