0

I am using callbacks to sense obstacles in a small robot. A left sensor and a right sensor. Therefore I add an event twice. Now when the callback is called I remove the event and then later add it again. This works nice if I only do this for ONE sensor But if I do them twice, the second time a segmentation error occurs.

    import RPi.GPIO as GPIO
    import time

    GPIO.setmode(GPIO.BCM)  

    GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  

    def my_callback(channel):  
        print("falling edge detected on ", channel)
        GPIO.remove_event_detect(17)
        #GPIO.remove_event_detect(20) 
        do_something()
        GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300) 
        #    GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)


    GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)  
#    GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)

Without the comments it fails. I am wondering why...

Note: One stranger thing. When run from the terminal the one-sensor programs runs ok (only one sensor) but in the rpi python environment it fails. The two sensor program always fail

  • Python and python modules shouldn't seg fault; this implies someone did not catch an error in the base C code -- perhaps a result of your error, but it still should have been caught and indicated with a python exception. – goldilocks Oct 14 '17 at 13:33
  • 1
    You mean if you run with GPIO 17 enabled, or GPIO 20 enabled it works? But when you have GPIO 17 and GPIO 20 enabled it crash? – MatsK Oct 14 '17 at 13:42
  • An error should be raised, but that is moot. It is not an intended usage. RPi.GPIO module callbacks are probably best left permanently enabled. You need to change your code to cope with that. – joan Oct 14 '17 at 14:59
  • @MatsK yeah, when I use only one (for example 17) and I call it from the terminal it works. But when I used both it crash. – KansaiRobot Oct 15 '17 at 1:33
  • 1
    @KansaiRobot I don't know; I don't use python. I was just pointing out that you should not be confused by the segfault, which is the result of someone else's mistake, even if yours triggered it. Unfortunately that means more direct information about your actual error is not being reported. – goldilocks Oct 15 '17 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.