Problem with IRQ events.

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ahaw
Posts: 3
Joined: Tue Jun 19, 2018 6:48 pm

Problem with IRQ events.

Post by ahaw » Tue Jun 19, 2018 6:59 pm

Hello,
I'm trying to use IR sensor for obstacle detection (Fc-51) but I have problem.
For some reason callback from pin pin.irq(trigger=Pin.IRQ_FALLING, handler=self.obstacleDetected) is being called many times.
I read that I should use machine.disable_irq(). It improved (before that I was called dozens of times, and after just around 3)
I even tried to use variable to stop processing events but it still doesn't work as expected.
Am I missing something?

My code:

[code]
import machine
import utime
class LightController:
isLightOn = False
isProcessingEvent = False
def __init__(self, pin):

from machine import Pin
pin = Pin(pin, Pin.IN)
pin.irq(trigger=Pin.IRQ_FALLING, handler=self.obstacleDetected)
def obstacleDetected(self, pin):
if self.isProcessingEvent:
print('Cancelling. Event is being processed.')
return
self.isProcessingEvent = True
irqstate = machine.disable_irq()
self.toggleLight()
machine.enable_irq(irqstate)
utime.sleep_ms(100)
self.isProcessingEvent = False

def toggleLight(self):
if self.isLightOn:
print("Turning off light")
self.isLightOn = False
else:
print("Turning on light")
self.isLightOn = True
controller = LightController(5)
[/code]

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem with IRQ events.

Post by pythoncoder » Thu Jun 21, 2018 8:08 am

You shouldn't need to disable interrupts in an interrupt service routine (ISR) and you certainly shouldn't have

Code: Select all

utime.sleep_ms(100)
in one. ISR's should be designed to complete in the shortest possible time. I strongly recommend reading this guide in the official docs.

If, after following these guidelines, you find that your ISR is repeatedly being called the best bet is to examine the output of the device with an oscilloscope or logic analyser. Perhaps the line is changing state rapidly.
Peter Hinch
Index to my micropython libraries.

ahaw
Posts: 3
Joined: Tue Jun 19, 2018 6:48 pm

Re: Problem with IRQ events.

Post by ahaw » Thu Jun 21, 2018 11:21 am

[quote=pythoncoder post_id=28271 time=1529568489 user_id=265]
Perhaps the line is changing state rapidly.
[/quote]
That's why I decided to disable IRQ ad sleep. Without that IR sensor have been calling callback method dozens of times.

An freeze during IRQ procesing will not be an issue because I want to create light switch.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with IRQ events.

Post by Roberthh » Thu Jun 21, 2018 6:51 pm

if the sensor signal is too erratic, you could also use a timer interrupt and sample the IR sensor input. That allows you to use any signal filtering you like, for instance a low pass filter with threshold, or a simple up7down counter with limits & threshold, etc.

ahaw
Posts: 3
Joined: Tue Jun 19, 2018 6:48 pm

Re: Problem with IRQ events.

Post by ahaw » Mon Jun 25, 2018 5:44 am

I did something line that.
On first callback Im disabling irq processing and on the end of callback I'm creating timer with 1s delay to enable processing it again:)

Post Reply