3 Replies Latest reply on Dec 6, 2014 11:51 AM by arfoll

    ADC stops reading on 510th iteration on Python

    ihacivelioglu

      I wrote a basic test code to try the functionality of the python mraa class on my edison arduino board. The problem that I run into is adc function stops reading when the iteration arrives 510th level. Why do this problem occur?

       

      Test Code:

      import mraa

      import time

      i=0

      print (mraa.getVersion())

      while i<520:

              i=i+1

              try:

                      time.sleep(0.5)

                      print(i),

                      print (mraa.Aio(0).read())

              except:

                      print ("Are you sure you have an ADC?")

      *****************************************

      Output of the test code.

      root@myedison:~# python test1.py

      v0.5.4-5-g1ee0548

      1 1019

      2 1023

      3 1023

      .....

      508 1023

      509 1023

      510 Are you sure you have an ADC?

      511 Are you sure you have an ADC?

        • 1. Re: ADC stops reading on 510th iteration on Python
          KurtE

          I am only guessing here as I have not tried out the ADC on the edison yet, So as such have not tried it with MRAA and at best I have only tinkered a little bit with Python.....

           

          So I don't know how:   print (mraa.Aio(0).read())

          which is in a loop is implemented.  Does it keep around one instance of an MRAA_AIO object associated with pin A0, or does it create a MRAA object do the read and destroy it...  My guess may be the later.

           

          So my guess is maybe you are running out of system resources like file handles.  I can see in the function: mraa_aio_init(unsigned int aio)

          It calls the function: aio_get_valid_fp

          Which opens a file handle to: "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"

          And I don't see anywhere where this file handle is closed.

           

          again guessing, the close function should be updated to maybe something like:

           

          mraa_result_t
          mraa_aio_close(mraa_aio_context dev)
          {
              if (NULL != dev) {
                  if (dev->adc_in_fp != -1)
                      close(dev->adc_in_fp);
                  free(dev);
              }
          
              return(MRAA_SUCCESS);
          }
          
          

          Warning, I manually typed in the changes here so may have missed something syntax wise...  To try out these changes you would have to build MRAA...

          Also if you have not already done so, you should raise an issue with MRAA up on github.  Project(intel-iot-devkit/mraa) under github.com

          • 2. Re: ADC stops reading on 510th iteration on Python
            KurtE

            Since I already had a clue what was going on, I made the change to my MRAA build.  Before my change, I had the same error.  After my change:

            508 992

            509 0

            510 0

            511 0

            512 0

            513 0

            514 0

            515 992

            516 0

            517 0

            518 0

            519 0

            520 0

            root@Edison:~#

             

            note: I have nothing hooked up the Analog pin so just floating...

            I created a new branch on my github fork and I have issued a Pull Request, so hopefully the MRAA people will review and do the pull...

             

            Kurt

            • 3. Re: ADC stops reading on 510th iteration on Python
              arfoll

              KurtE is absolutely correct, I'll merge the patch in ASAP. Careful with floating ADC readings, it's generally bad for the ADC and not recommended.

               

              However in your case opening a new Aio() object is really not what you should be doing. Open it once before your loop and then keep calling .read() on the object. Something like this:

              import mraa

              import time

              i=0

              print (mraa.getVersion())

              x = mraa.Aio(0)

              while i<520:

                      i=i+1

                      try:

                              time.sleep(0.5)

                              print(i),

                              print (x.read())

                      except:

                              print ("Are you sure you have an ADC?")