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

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

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

Post by Gabi » Sun Apr 17, 2016 2:13 pm

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 ;)

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

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

Post by Roberthh » Sun Apr 17, 2016 4:35 pm

Isn't line 48 obsolete?

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

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

Post by Gabi » Sun Apr 17, 2016 4:59 pm

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 :)

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

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

Post by pythoncoder » Mon Apr 18, 2016 6:20 am

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.
Peter Hinch
Index to my micropython libraries.

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

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

Post by Gabi » Mon Apr 18, 2016 4:04 pm

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

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

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

Post by Roberthh » Mon Apr 18, 2016 7:17 pm

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.

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

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

Post by pythoncoder » Tue Apr 19, 2016 9:13 am

@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.
Peter Hinch
Index to my micropython libraries.

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

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

Post by Roberthh » Tue Apr 19, 2016 10:51 am

@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

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

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

Post by Gabi » Tue Apr 19, 2016 8:07 pm

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 :)

Gabi
Posts: 18
Joined: Sat Mar 05, 2016 3:29 pm

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

Post by Gabi » Tue Apr 19, 2016 8:40 pm

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 ;)
Last edited by Gabi on Wed Apr 20, 2016 6:41 pm, edited 1 time in total.

Post Reply