About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

Frequent but irregular thread crashing in readADCraw

1427 Views - Created 12/05/2016

12/05/2016

Posted by:
PeterB

Users Avatar

Location:
Philippines

I am developing on ArchLinuxArm, using Python 3.

I have arranged my code such that all I/O to the expansion boards is only performed in one thread (except, possibly, the RTC, which is handled by the OS.

I am experiencing a problem that the I/O thread crashes fairly often (sometimes within minutes of starting but at other times it will run for several hours) with the following traceback:

[root@automate ~]# Main.py
Exception in thread Inputs.Inputs:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/root/Inputs.py", line 23, in Inputs
    analogn.sense()
  File "/root/Analogs.py", line 21, in sense
    self.values.append(adc.readADCraw(self.channel))
  File "/root/ABElectronics_Python3_Libraries/ExpanderPi/ABE_ExpanderPi.py", line 61, in readADCraw
    r = self.__spiADC.xfer2([4 + (channel >> 2), (channel & 3) << 6, 0])
OSError: [Errno 22] Invalid argument

 

If I insert a print statement into my code, the problem appears to go away - the system will run reliably for more than a day.

The analogue inputs are being scanned once a second with this code:

# Analogs
# 2016-05-08  21:46

from ExpanderPi.ABE_ExpanderPi import ADC

adc=ADC()

class Analog:
    def __init__(self, pi, channel, avcnt):
        from Initialise import analogs
        self.channel=channel
        self.values=[]
        self.avcnt=avcnt
        analogs.append(self)
        self.value=0.0

    def sense(self):
#        print(self.channel,self.avcnt,self.values)
        if(len(self.values) >= self.avcnt):
            self.values.pop(0)
        self.values.append(adc.readADCraw(self.channel))
        self.value=sum(self.values)/len(self.values)


    def read(self):
        return self.value
        

I have no problem with the digital inputs, which are being scanned ten times a second.

 

Can anyone offer any advice or explanation for this problem?

12/05/2016

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hi Peter

There may be a bug with the py-spidev module that is causing your problem.  It appears that the Gadgetoid branch of py-spidev that we used in our SPI tutorial has not been updated for quite some time and has compatibility issues with the latest linux kernel.  There is another branch of the module that you could try installing and see if that solves the problem.

You can download and install the new version using:

git clone https://github.com/doceme/py-spidev.git
cd py-spidev
sudo python setup.py install
sudo python3 setup.py install

AB Electronics UK Tech Support

13/05/2016

Posted by:
PeterB

Users Avatar

Location:
Philippines

Thanks Andrew - you seem to have hit the nail on the head!  I installed the new py-spidev and the system ran overnight without any sign of a problem.  Now I can continue with my development.

I'm currently using an original RPi B for the development, but at some stage I will move on to a Pi2 or Pi3 and change the threading to multi-processing.

Thanks for the excellent support.

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.