I need to acquire a signal at a minimum sample rate of 10 kHz and processing it real-time using a Python code. Since Raspberry Pi only reads digital signals I get an ADC (High Precision AD/DA Board from Waveshare, with ADS1256 8 CHANNELS 24 BITS, SPI protocol). I'm using the source code provided by Waveshare in order to read channels' value. How can I process ADC data at high sample rate in real-time? It seems that acquisition is interrupted when Raspberry is doing another task such as writing the values in a text file and the sampling frequency is not constant. I've read about buffers but my ADC does not implement these. Can I use threading and queues?
1 Answers
Linux is not a real-time operating system.
You will not be able to achieve consistent sampling times using the standard Linux environment.
I have used DMA bit banging to get accurately timed SPI samples.
By using bit-banging it is possible to get 12 bit ADC samples from one or more MCP3202s simultaneously. Rates of 25ksps per ADC can be achieved. Conventional SPI only allows one device to talk at a time. By using bit-banging you can share one CLK, MOSI, and SS (slave select) lines between many ADCs but give each its own MISO.
An advantage of bit-banging is that you have tight control of when the samples are taken, in the case given every 40 μs, not as and when the SPI driver gets around to it.
To bit-bang a chain of DMA blocks is used to switch the CLK, MOSI, and SS lines on/off according to the SPI requirements. The DMA blocks also contain gpio reads to capture the data transmitted by the ADC MISO lines. All gpios are read simultaneously (that's the way the Broadcom SOC works) so as many ADCs can be read as can be connected. In my experiment I used 2 ADCs as that is all I have.
The waveforms are constructed by a modified version of the pigpio library. Each waveform is made up of a series of pulses which define its characteristics. Multiple waveforms are generated so that samples can be held in a cyclic buffer in memory (giving enough time to extract the readings). The waveform to capture one sample is given below.
https://www.raspberrypi.org/forums/viewtopic.php?t=71089
Also see http://abyz.me.uk/rpi/pigpio/examples.html#C_rawMCP3202_c
- 71,852
- 5
- 76
- 108