Read data from ADXL355 using python SPI interface on raspberry pi

有些话、适合烂在心里 提交于 2020-03-05 04:50:18

问题


I'm trying to read accelerometric data from the evaluation board EVAL-ADXL355-PMDZ. The board is connected to a raspberry pi 4 that runs on raspbian. The circuit is cabled into the standard SPI pins (4-wires) in the raspberry pi but the chip selection (CS) is connected into one of the free pins (12).

To test the board I wrote the following code that uses spidev to make the comunication with the chip and RPi.GPIO to make the chip selection:

#!/usr/bin/env python3.7

import time
import spidev
import RPi.GPIO as gpio

pin = 12

gpio.setwarnings(False) # stop warnings when the script runs multiple times
gpio.setmode(gpio.BCM)
gpio.setup(pin, gpio.OUT)

spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 3
spi.max_speed_hz = 5000000

READBIT = 0x01
WRITEBIT = 0x00

def check_adxl355(pin):
    '''gets true if DEVID_MST is 0x1D'''
    address  = 0x01 << 1 | READBIT
    gpio.output(pin, gpio.LOW)
    id_ = spi.xfer2([address ,0])
    gpio.output(pin, gpio.HIGH)
    return id_[1] == 0x1D

def configure_adxl355(pin):
    '''configure the accelerometer '''
    RANGE = 0x2C << 1 | WRITEBIT
    gpio.output(pin, gpio.LOW)
    o_ = spi.xfer2([RANGE, 0x01]) # RANGE_2G
    gpio.output(pin, gpio.HIGH)

    POWER_CTL = 0x2D << 1 | WRITEBIT
    gpio.output(pin, gpio.LOW)
    o_ = spi.xfer2([POWER_CTL, 0x06]) 
    gpio.output(pin, gpio.HIGH)

print("ADXL355 : {}".format(check_adxl355(pin)))
configure_adxl355(pin)

# read data from the ADXL355
AXIS_START = 0x08 << 1 | READBIT
while 1:
    gpio.output(pin, gpio.LOW)
    axisBytes = spi.xfer2([AXIS_START, 0, 0, 0, 0, 0, 0, 0, 0, 0])[1:] # read 9 bytes
    gpio.output(pin, gpio.HIGH)

    X = (axisBytes[0] << 16 | axisBytes[1] << 8 | axisBytes[2]) >> 4
    Y = (axisBytes[3] << 16 | axisBytes[4] << 8 | axisBytes[5]) >> 4
    Z = (axisBytes[6] << 16 | axisBytes[7] << 8 | axisBytes[8]) >> 4

    print(">> {} {} {}".format(X, Y, Z))

    time.sleep(1)

In general what it does is to configure the communication between the accelerometer and the raspberry pi, check if the ADXL355 is connected to the pin 12 by locking at the the mems id register (check_adxl355), configure the range of the accelerometer (configure_adxl355) and reading samples from the FIFO register. When running the previous code I have as the results:

pi@raspberrypi:~/adxl355spi $ ./adxl355.py
ADXL355 : True
>> 0 0 0
>> 0 0 0
...

the first block looks ok as it reports that the accelerometer was found, after that the configuration of the accelerometer is not checked, and finaly the the block that read the accelerometric data (X, Y, Z) returns only 0 even though the accelerometer is moving.

Can anyone spot a problem when reading the accelerometric data?


回答1:


In order to debug such a problem you need start by checking the hardware communication level because SPI protocol is sensitive to long wires [>1m] and you need to know if everything if as it should at this level. Use a logic analyzer like saleae.

Also be aware that ADXL sensors are known for having problems.

I've lost a couple of nights, years ago, with this two problems.




回答2:


I have worked with these sensors, they can be a real pain!

Have a look over here. Someone already went through the trouble of creating a library in python for using this sensor combined with the SPI bus on the RPi. Basically this is exactly what you need. You can copy your gpio.output(pin, gpio.LOW) and gpio.output(pin, gpio.HIGH) statements into the read_data function of that library or just connect it to pin 24 on the pi instead (like the library doc suggests) and see if that works.

Also from the adxl355.py file I can see that you are interpreting your data differently:

# Join data
x_data = (x_data[0] >> 4) + (x_data[1] << 4) + (x_data[2] << 12)
y_data = (y_data[0] >> 4) + (y_data[1] << 4) + (y_data[2] << 12)
z_data = (z_data[0] >> 4) + (z_data[1] << 4) + (z_data[2] << 12)

# Apply two complement
if x_data & 0x80000 == 0x80000:
    x_data = ~x_data + 1

if y_data & 0x80000 == 0x80000:
    y_data = ~y_data + 1

if z_data & 0x80000 == 0x80000:
    z_data = ~z_data + 1

Maybe you're just interpreting your data wrong, try the above and see if that fix it. Otherwise I would suggest switching to use that library. Even if it's temporary it can help you figure out what is wrong with your code.



来源:https://stackoverflow.com/questions/59160194/read-data-from-adxl355-using-python-spi-interface-on-raspberry-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!