在树莓派上使用python SPI接口从ADXL355读取数据 [英] Read data from ADXL355 using python SPI interface on raspberry pi

查看:426
本文介绍了在树莓派上使用python SPI接口从ADXL355读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从评估板EVAL-ADXL355-PMDZ读取加速度数据。该开发板已连接至在 raspbian 上运行的raspberry pi 4。该电路通过电缆连接到树莓派中的标准SPI引脚(4线),但是芯片选择( CS )连接到其中一个自由引脚(12)。

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).

为了测试该板,我编写了以下代码,该代码使用spidev进行与芯片的通信,并使用RPi.GPIO进行芯片选择:

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)

一般来说,它是配置加速度计和树莓派之间的通信,检查是否通过锁定内存ID寄存器( check_adxl355 )将ADXL355连接到引脚12,配置加速度计的范围( configure_adxl355 )并从FIFO寄存器中读取样本。运行前面的代码时,结果如下:

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
...

第一个程序段看起来不错,因为它报告说加速度计找到后,未检查加速度计的配置,最后读取加速度计数据的块( X,Y,Z )仅返回 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?

推荐答案

我已经使用了这些传感器,它们确实是一个痛苦!

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

在此处。有人已经遇到了用python创建一个库的麻烦,以便将该传感器与RPi上的SPI总线结合使用。基本上,这正是您所需要的。您可以复制 gpio.output(pin,gpio.LOW) gpio.output(pin,gpio.HIGH)对该库的 read_data 函数进行声明,或者直接将其连接至pi上的引脚24(如库文档所建议的那样),看看是否可行。

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.

也来自adxl355.py文件,我可以看到您对数据的解释有所不同:

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.

这篇关于在树莓派上使用python SPI接口从ADXL355读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆