Page 1 of 1

Read Multiple Lines of Data At Once

Posted: Wed Jan 16, 2019 12:29 am
by AlexMer119
Hello all,

I am making a project for my design class with my Raspberry Pi Model 3 B+ and Sharp Sensors. This project connects sensors that track distances for a car's bumper (like a parking sensor). I am using the Sharp 2Y0A02F sensors. I currently have both of the sensors feeding into a MCP3008 ADC that then go to the GPIO. I can read a sensor's input in small decimal. I'm assuming it is the voltage reading but that's not my concern here. I'm trying to read from channel 0 and 1 of the ADC at the same time so I can have multiple sensors reading data. Can someone assist with how this would be possible?

Code: Select all

from __future__ import division

#import spidev for use
import spidev
from time import sleep

def bitstring(n):

    s = bin(n)[2:]

    return '0'*(8-len(s)) + s

#read from adc and match with spi channel
def read(adc_channel=0, spi_channel=0):

    conn = spidev.SpiDev(0, spi_channel)

    conn.max_speed_hz = 1350000 # 1.35 MHz

    conn.mode = 0

    cmd = 192 #start bit + single ended

    if adc_channel:

        cmd += 32 #set the ODD/SIGN bit to select channel number

    reply_bytes = conn.xfer2([cmd, 0])

    reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)

    reply = reply_bitstring[5:15]

    return int(reply, 2) / 2**10

if __name__ == '__main__':
    while True:
        print(read())
        sleep(1)

Re: Read Multiple Lines of Data At Once

Posted: Wed Jan 16, 2019 4:33 am
by Idahowalker
If you want your program to do 2 things at the same time you may consider multiprocessing on the RPi as one option.

You could, also, consider using several inexpensive STM32 Blue pills and assign one blue pill to each channel of the ADC and then have each STM32 report its findings to the RPi. This scheme leaves to RPi free to do other things till a thing happens amd STM32 Blue pills are inexpensive.

You could, also use a ESP32 with freeRTOS and not use the WiFI for complete access and control of each core, which would give you very near real time processing. If you can do assembly programming you can even use the 3rd core for GPIO operations.

Re: Read Multiple Lines of Data At Once

Posted: Wed Jan 16, 2019 8:24 am
by OutoftheBOTS
Ok I haven't used the MCP3008 ADC but do know of it. I mainly use the ADS1115 or the ADC pins of a MCU.

Now your trying to do this in a very usual way. Your code to read the sensors sets up the SPI and then reads the data then turns the data into a string. The data being returned is an integer why turn it into a string then you truncate the string and turn it back into an integer.

First of all setup the SPI bus only once then just use it for each read, this will speed it up a lot. Second converting int to string then back to int also makes for very slow code.

If you always want to read both channel 0 and 1 then do it in the same function and return a list with both reading in it that way the function only needs to be called once and this will also speed up it a lot.

I can't test the code but something like this might do the job for you

Code: Select all

from __future__ import division

#import spidev for use
import spidev
from time import sleep

def read():
    
	cmd = 192 #start bit + single ended	
	reply_bytes_channel0 = conn.xfer2([cmd, 0]) & 0b00001111111111  
	cmd += 32 #set the ODD/SIGN bit to select channel number	
	reply_bytes_channel1 = conn.xfer2([cmd, 0]) & 0b00001111111111    
	return (reply_bytes_channel0, reply_bytes_channel1)


#setup the SPI bus	
conn = spidev.SpiDev(0, 0)
conn.max_speed_hz = 1350000 # 1.35 MHz
conn.mode = 0

    while True:
        print(read())
        sleep(1)