beaglebone GPIO callback

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

beaglebone GPIO callback

Post by x13 »

I purchased adafruit brass flow meters to be interfaced using GPIO and event_detect with callback. The code works fine without reporting any errors but the callback procedure never executes. A meter reports that the the pin P8_14 does change.
Does callback work on the Beaglebone GPIO? Are there any examples on the Adafruit site using callback?

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

Hi, are you using the Adafruit BBIO Python library?

Could you please post your code so that I can get a better understanding?

Also, please post the output of these commands so that I know the version of the system software on your beaglebone:

Code: Select all

cat /etc/dogtag
cat /etc/issue
uname -a
Thanks
Drew

User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

Re: beaglebone GPIO callback

Post by x13 »

Hi Drew
#!/usr/bin/python
#pins.py
import Adafruit_BBIO.GPIO as GPIO
from time import sleep, strftime, time
####GPIO.cleanup()
def setupPins():
GPIO.setup("P8_8", GPIO.IN) # pressure switch
GPIO.setup("P8_7", GPIO.OUT) # circ pump
GPIO.output("P8_7", GPIO.LOW) # set low
GPIO.setup("P8_9", GPIO.OUT) # well on
GPIO.output("P8_9", GPIO.LOW)
GPIO.setup("P8_10", GPIO.OUT) # well off
GPIO.output("P8_10", GPIO.LOW)
GPIO.setup("P9_17", GPIO.OUT) #latching valve sols
GPIO.output("P9_17", GPIO.LOW)
GPIO.setup("P9_18", GPIO.OUT)
GPIO.output("P9_18", GPIO.LOW)
GPIO.setup("P9_21", GPIO.OUT)
GPIO.output("P9_21", GPIO.LOW)
GPIO.setup("P9_22", GPIO.OUT)
GPIO.output("P9_22", GPIO.LOW)
GPIO.setup("P9_23", GPIO.OUT)
GPIO.output("P9_23", GPIO.LOW)
GPIO.setup("P9_24", GPIO.OUT)
GPIO.output("P9_24", GPIO.LOW)
GPIO.setup("P8_12", GPIO.IN) #wall flow
GPIO.setup("P8_14", GPIO.IN) #heat flow

def countFlow(pin):
global ct
ct += 1
print ct

def flow(valve):
global ct
v = valve
if v == 'wall':
pin = "P8_12"
print 'v = wall'
GPIO.add_event_detect(pin, GPIO.FALLING, callback=countFlow)
ct = 0
sleep(5)
GPIO.setup("P8_12",GPIO.IN) #reset wall flow

elif v == 'heat':
pin = "P8_14"
print 'v = heat'
GPIO.add_event_detect(pin, GPIO.FALLING, callback=countFlow)
ct = 0
sleep(5)
GPIO.setup("P8_14",GPIO.IN) #reset heat flow

else:
print "Bad valve specification"
return ct

--------The main program-------

#!/usr/bin/python
#ytestflow.py
from time import sleep, strftime, time
import pins, os
pins.setupPins()
count = pins.flow('wall')
print "flow count ",count

-------I am only testing P8__12 with this test----------

root@beaglebone:~# cat /etc/dogtag
BeagleBoard.org Debian Image 2015-07-28

root@beaglebone:~# cat /etc/issue
Debian GNU/Linux 7 \n \l

BeagleBoard.org Debian Image 2015-07-28

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

The IP Address for eth0 is: 192.168.2.165
The IP Address for usb0 is: 192.168.7.2

root@beaglebone:~# uname -a
Linux beaglebone 3.8.13-bone72 #1 SMP Tue Jun 16 21:36:04 UTC 2015 armv7l GNU/Linux

For this test during the 5 s sleep time I cycled the P8__12 pin from 0 to 2.5 v measured on the pin. I have a mosfet level converter so I used a jumber from 0 to 5v on the mosfet drain to cycle the input to the pin.
Thanks for your help!
Neil Poulson (x13)
Last edited by x13 on Sat Jan 16, 2016 10:38 pm, edited 1 time in total.

User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

Re: beaglebone GPIO callback

Post by x13 »

I believe I have found an error in the code and will fix it and try again later today.
I suggest that anyone working on helping me put your effort on hold until I post my results.
Thanks.
Neil Poulson
Last edited by x13 on Sat Jan 16, 2016 10:35 pm, edited 1 time in total.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

UPDATE: looks like we were both typing at the same time :). Please let me know if you need further help

Thanks for posting the code.

If you haven't already, then could you please try using the latest version of adafruit-beaglebone-io-python (follow the Manual instructions in the README).

Justin Cooper's comments in the repo indicate that GPIO event callbacks do work, although the tutorial does not include them: Using the Adafruit_BBIO Library: GPIO.

There is example from that tutorial page:

Code: Select all

    GPIO.add_event_detect("P9_12", GPIO.FALLING)
    #your amazing code here
    #detect wherever:
    if GPIO.event_detected("P9_12"):
        print "event detected!"
Could you check if that works for you?

It would help to understand if the trouble is event detection, or just that the callback handler is not getting called.

thanks,
drew

User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

Re: beaglebone GPIO callback

Post by x13 »

Hi Drew
I fixed the error in my code edited my post; the callback procedure now gets called and counts. I eventually get a segmentation error.
How can I stop the event dectection? I installed a recent Debian image and Adafruit_GPIO so I may have the latest version.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

x13 wrote:Hi Drew
I fixed the error in my code edited my post; the callback procedure now gets called and counts. I eventually get a segmentation error.
How can I stop the event dectection? I installed a recent Debian image and Adafruit_GPIO so I may have the latest version.
Is there any additional error messages when the segmentation error occurs?

It did find this issue on the github repo:
GPIO callback causing segmentation fault #42

I will attempt to replicate the issue as well.

Thanks,
Drew

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

I created this test program:

Code: Select all

import Adafruit_BBIO.GPIO as GPIO
from time import sleep, strftime, time

ct = 0

def countFlow(pin):
  global ct
  ct += 1
 
GPIO.setup("P8_10", GPIO.OUT)
GPIO.setup("P8_12", GPIO.IN)
GPIO.add_event_detect("P8_12", GPIO.RISING, countFlow)
while 1:
  print "ct=%d" % ct
  GPIO.output("P8_10", GPIO.HIGH)
  sleep(1)
  GPIO.output("P8_10", GPIO.LOW)
  sleep(1)

GPIO.cleanup()
countFlow() is run when the event is detected:

Code: Select all

debian@beaglebone:~/support/callback$ sudo python test4.py
ct=0
ct=0
ct=14
ct=44
ct=44
ct=44
^CTraceback (most recent call last):
  File "test4.py", line 17, in <module>
    sleep(1)
KeyboardInterrupt
I was doing this by connecting the P8_12 through a resistor to the 3.3V pin (P9_4). This causes P8_12 to go high which will produce a rising edge event.

I noticed though that countFlow() is called often. I believe this is because there is no deboucning. I removed the print statement from countFlow() because it takes too long to run.

I didn't encounter any segmentation errors. Are you still having that issue?

User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

Re: beaglebone GPIO callback

Post by x13 »

Hi Drew
I had the same trouble with using a shorting jumper. On a RPI_GPIO post I found:
bouncetime=100
GPIO.remove_event_detect(pin)
I added these to my code and have not produced any errors.
I wonder if I was adding an interrupt each time I called the procedure.
The above items don't seem to be documented in the Adafruit_GPIO for beaglebone
but they seem to work. Are they really functional.
Thanks
Neil

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

Thanks for the tip about bouncetime. I see that now in py_gpio.c:

Code: Select all

static int add_py_callback(char *channel, unsigned int gpio, unsigned int bouncetime, PyObject *cb_func)
Is your program now working as expected?

User avatar
x13
 
Posts: 6
Joined: Fri Jan 15, 2016 11:25 am

Re: beaglebone GPIO callback

Post by x13 »

Hi Drew
Yes, it is. Thanks for your help, I finished the calibration of the brass flow meters last evening.

Is there a documentation on your site which describes all of the functions of the Adafruit_GPIO for beaglebone black?

Neil Poulson

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

Glad to hear it!

There is this tutorial: Using the Adafruit_BBIO Library. I will talk to Justin about adding more to it, especially for event callback, as you have highlighted.

I'll also created a pull request with event callback documentation for the adafruit-beaglebone-io-python README

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: beaglebone GPIO callback

Post by drewfustini »

Glad to hear it!

There is this tutorial: Using the Adafruit_BBIO Library. I will talk to Justin about adding more to it, especially for event callback, as you have highlighted.

I'll also created a pull request with event callback documentation for the adafruit-beaglebone-io-python README.

thanks,
drew

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”