Page 1 of 2

IRQ on Pin throwing TypeError: can't convert to int

Posted: Sun Apr 17, 2016 2:13 pm
by Gabi
Hi guys,
I am currently playing with my HC-SR04 ultrasonic sensor to be able to detect wether my entry door is closed or not.

Now, I have done:

Code: Select all

import time
import machine
from machine import Pin, RTC

RTC_USEC_POS = 7
RTC_SEC_POS = 6
RTC_MIN_POS = 5

MIN_TO_SEC = 60
SEC_TO_USEC = 1000

TRIGGER_TIME = 10

TRIGGER_PIN = 'GP11'
ECHO_PIN = 'GP12'

LOW = 0
HIGH = 1

delay = 0
rtc = machine.RTC()
raising_time = rtc.now()
falling_time = rtc.now()
def changingEdge(pin):
	print("Changing Edge %s"%pin)
	flags = pin.irq().flags()
	if flags & Pin.IRQ_RISING:
		raising_time = rtc.now()
		print("rtc raising: %s" % raising_time)

	elif flags & Pin.IRQ_FALLING:
		falling_time = rtc.now()
		print("rtc falling: %s" % falling_time)

		ellapsed_minutes = falling_time[RTC_MIN_POS] - raising_time[RTC_MIN_POS]
		ellapsed_seconds = falling_time[RTC_SEC_POS] - raising_time[RTC_SEC_POS]
		ellapsed_useconds = falling_time[RTC_USEC_POS] - raising_time[RTC_USEC_POS]

		delay = ellapsed_minutes * MIN_TO_SEC * SEC_TO_USEC + ellapsed_seconds * SEC_TO_USEC + ellapsed_useconds
		print("Delay: " % delay)	


triggerPin = Pin(TRIGGER_PIN, mode = Pin.OUT)
echoPin = Pin(ECHO_PIN, mode = Pin.IN)

echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)

echoPin.irq()()
while delay==0:
	input("Press Enter to trigger...")
	triggerPin.value(HIGH)
	time.sleep_us(TRIGGER_TIME)
	triggerPin.value(LOW)
And I have uploaded it on my WiPy:
MicroPython v1.6-89-g440d33a on 2016-02-27; WiPy with CC3200

When I launch it, I have an error:
execfile('HC-SR04.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "HC-SR04.py", line 48, in <module>
TypeError: can't convert to int

So, I was wondering if I was doing something wrong in my code to have this error thown.

Thank you for the help ;)

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Sun Apr 17, 2016 4:35 pm
by Roberthh
Isn't line 48 obsolete?

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Sun Apr 17, 2016 4:59 pm
by Gabi
Well, I have checked that alreadyn but correct me if I am wrong.
The line 48 is

Code: Select all

echoPin.irq()()
Now, this line is supposed to trigger the callback: http://micropython.org/resources/docs/e ... rq#pin.irq

I want to do that to test the interruption, to validate the behavior before going any further in my devolpement.

Thank you anyway :)

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Mon Apr 18, 2016 6:20 am
by pythoncoder
That doc contradicts itself. If you look at the definition:

Code: Select all

pin.irq(*, trigger, priority=1, handler=None, wake=None)
the code should throw an error if you issue

Code: Select all

pin_int.irq()()
This is because the mandatory trigger argument is missing. But the error should be that it's missing a required keyword only argument, not the error message you're seeing. Perhaps @danicampora can elucidate.

Secondly, if the definition is correct, issuing it without a handler argument will remove the handler.

A final comment. Issuing print statements in an interrupt handler is usually ill-advised. See http://micropython.org/resources/docs/e ... rules.html.

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Mon Apr 18, 2016 4:04 pm
by Gabi
Hi,
I am not sure any parameter is missing, it is just the prototype of the function and it requires some parameters optional or not. I have put them in the init of the IRQ. But, as you said, let's wait for the confirmation from @danicampora.

Here, as per my understanding,

Code: Select all

pin_int.irq()()
is only triggering the callback, not sure if it re-init everything nor if I should pass some parameter to it (like the flag on the Pin (RISING_EDGE or FALING_EDGE in my case)). Again, as you said, @danicampora should be able to help :)

I know that this is very bad to call manually the interruption, it is only to validate the behaviour of the handler and the flag triggering the IRQ before going any step forward to avoid losing time wondering where is the error in a huge piece of code: I have milestones each step of my coding so when one milestone is passed, I am sure it is OK ;)

And thank you for helping me on that :D

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Mon Apr 18, 2016 7:17 pm
by Roberthh
I did not try that, but as code I would then have expected something like:

Code: Select all

mycallback = echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)
mycallback()
But, like pythoncoder said, I would have my doubts of calling print() in an ISR handler. In the trial call it may work, but during the real interrupt service, it may fail due to a different stack & memory environment.

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Tue Apr 19, 2016 9:13 am
by pythoncoder
@Roberthh As a matter of interest do you agree that the doc contradicts itself? As far as I can see the call

Code: Select all

pin_int.irq()()
which it specifies does not conform with its own function definition.

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Tue Apr 19, 2016 10:51 am
by Roberthh
@pyhtoncoder: I did not see this in the documentation at first glance. It is in the examples. The syntax of that is confusing. I had the understanding that the pin.irq call creates a callback. It looks like using it w/o an argument has a different behaviour, as returing a previously created callback; but that should be specified. Or - the example is wrong. That would not be the first time.

About the initial error: I have the suspicion that this is caused inside the function changingEdge(). At least the indices for accessing the results of rtc.now seem to be wrong; 1-based instead of 0-based. And I don't know, if printing a tuple of integers with the %s format specifier works. Depends on what exactly is returned by rtc.now(). I have to try it on WiPy

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Tue Apr 19, 2016 8:07 pm
by Gabi
Hi,
When you said
Roberthh wrote:And I don't know, if printing a tuple of integers with the %s format specifier works. Depends on what exactly is returned by rtc.now(). I have to try it on WiPy
You were right, I tested it on the REPL and I got an error.

So I have removed the format %s:

Code: Select all

>>> print(raising_time)
(2015, 1, 1, 0, 0, 6, 248000, None)
Also,
Roberthh wrote:About the initial error: I have the suspicion that this is caused inside the function changingEdge(). At least the indices for accessing the results of rtc.now seem to be wrong; 1-based instead of 0-based.
You were also right, I must have been very tired when coding this, a plain and simple rookie error ^^.

So here again, I have updated my code.


Now,
Roberthh wrote:I did not try that, but as code I would then have expected something like:

Code: Select all

mycallback = echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)
mycallback()
But, like pythoncoder said, I would have my doubts of calling print() in an ISR handler. In the trial call it may work, but during the real interrupt service, it may fail due to a different stack & memory environment.
pythoncoder wrote:@Roberthh As a matter of interest do you agree that the doc contradicts itself? As far as I can see the call

Code: Select all

pin_int.irq()()
which it specifies does not conform with its own function definition.
I have tested it:

Code: Select all

echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)()
Here I did not bother to save the callback returned as I only wanted to test ;)
When I tested it, I have:

Code: Select all

execfile('HC-SR04.py')
Uncaught exception in callback handler
TypeError: 
The error seems to be with the parameter "pin" of the function. I am looking into it and I'll let you know the output of my investigation :)

Thank you for that :)

Re: IRQ on Pin throwing TypeError: can't convert to int

Posted: Tue Apr 19, 2016 8:40 pm
by Gabi
Sorry for the double post m(_*_)m

The issue is in the access of the flag

Code: Select all

flags = pin.irq().flags()
I got this line from here: https://github.com/micropython/micropyt ... e-API#irqs

But the doc is not good...indeed when I try to do

Code: Select all

callback = echoPin.irq(trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = changingEdge)
print(callback.flags())
It works (it printed '0')

So now I am wondering how to get the flags that triggered my handler inside my handler to execute the good part of code, if you have any idea, I would gladfully take it ;)