2 Replies Latest reply on Apr 29, 2016 2:36 AM by BrechtW

    Interrupt service routine with time-out mraa

    BrechtW

      I wan't to use an interrupt but with a time-out of a given amount, let's say 0.5 seconds.

       

      mraa/hello_isr.py at master · intel-iot-devkit/mraa · GitHub

       

      import mraa
      import time
      import sys
      
      class Counter:
        count = 0
      
      c = Counter()
      
      # inside a python interrupt you cannot use 'basic' types so you'll need to use
      # objects
      def test(gpio):
        print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read()))
        c.count+=1
      
      pin = 6;
      try:
        x = mraa.Gpio(pin)
        print("Starting ISR for pin " + repr(pin))
        x.dir(mraa.DIR_IN)
          # https://github.com/intel-iot-devkit/mraa/blob/master/examples/python/hello_isr.py
        x.isr(mraa.EDGE_RISING, test, x)
        var = raw_input("Press ENTER to stop")
        x.isrExit()
      except ValueError as e:
          print(e)
      
      
        • 1. Re: Interrupt service routine with time-out mraa
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi,

           

          The GPIO class on MRAA doesn’t count with a function to set a timeout. If you want to set a timeout I suggest you to create a subroutine that checks the activity on the pin. For example with a loop structure; if the subroutine doesn’t detect any trigger on the pin for a time the subroutine can close the loop.

           

          Regards,
          Charlie

          1 of 1 people found this helpful
          • 2. Re: Interrupt service routine with time-out mraa
            BrechtW

            This method uses polling with a time-out:

             

            Change the @timeout(0.020) based on your application. Maximal recommended value is 0.035.

            @timeout(0.020)

             

            timeout.py

            from functools import wraps
            import errno
            import os
            import signal
            # http://stackoverflow.com/questions/11901328/how-to-timeout-function-in-python-timeout-less-than-a-second
            
            class TimeoutError(Exception):
                pass
            
            def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
                def decorator(func):
                    def _handle_timeout(signum, frame):
                        raise TimeoutError(error_message)
            
                    def wrapper(*args, **kwargs):
                        signal.signal(signal.SIGALRM, _handle_timeout)
                        signal.setitimer(signal.ITIMER_REAL,seconds) #used timer instead of alarm
                        try:
                            result = func(*args, **kwargs)
                        finally:
                            signal.alarm(0)
                        return result
            
                    return wraps(func)(wrapper)
            
                return decorator
            
            
            
            
            
            
            

             

            hcsr04.py

            import mraa
            import time
            from timeout import timeout
            
            trig = mraa.Gpio(3)
            echo = mraa.Gpio(4)
            
            trig.dir(mraa.DIR_OUT)
            echo.dir(mraa.DIR_IN)
            
            # http://stackoverflow.com/questions/11901328/how-to-timeout-function-in-python-timeout-less-than-a-second
            
            @timeout(0.020)
            def distanceUS():
                tZero = time.time()
                # reading sensor in Python takes about 6 ms (US = Utrasonic Sensor)
                # http://stackoverflow.com/questions/32300000/galileo-and-ultrasonic-error-when-distance-less-than-4cm
                # http://playground.arduino.cc/Main/UltrasonicSensor
            
                trig.write(0)
                time.sleep(0.000004) # in Arduino 2 microseconds, double this value to be sure
            
                trig.write(1)
                time.sleep(0.00001) # in Arduino 5 microseconds, double this value to be sure
                trig.write(0)
            
                sig = None
                nosig = None
                etUS = None
            
                while echo.read() == 0:
                        nosig = time.time()
            
                while echo.read() == 1:
                        sig = time.time()
            
                if sig == None or nosig == None:
                    return 0
                 
                # et = Elapsed Time
                etUS = sig - nosig
            
                distance =  etUS * 17150
            
                return distance
            
            while True:
                try:
                    print distanceUS()
                except Exception, e:
                    # print 'time out!'
                    continue
            
            
            
            
            
            
            
            1 of 1 people found this helpful