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]
Problem with IRQ events.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Problem with IRQ events.
You shouldn't need to disable interrupts in an interrupt service routine (ISR) and you certainly shouldn't have
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.
Code: Select all
utime.sleep_ms(100)
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.
Index to my micropython libraries.
Re: Problem with IRQ events.
[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.
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.
Re: Problem with IRQ events.
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.
Re: Problem with IRQ events.
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:)
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:)