0

現在、3 つのアクティブな Raspberry Pi があります。それらのそれぞれは、加速度計で x、y、および z を収集できる必要があります。ただし、最新の Raspberry Pi で次のスクリプトを実行すると、問題が発生します。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Example on how to read the ADXL345 accelerometer.
# Kim H. Rasmussen, 2014
import sys, math, os, spidev, datetime, ftplib

# Setup SPI
spi = spidev.SpiDev()
#spi.mode = 3    <-- Important: Do not do this! Or SPI won't work as intended, or even at all.
spi.open(0,0)
spi.mode = 3

# Read the Device ID (should be xe5)
id = spi.xfer2([128,0])
print 'Device ID (Should be 0xe5):\n'+str(hex(id[1])) + '\n'

# Read the offsets
xoffset = spi.xfer2([30 | 128,0])
yoffset = spi.xfer2([31 | 128,0])
zoffset = spi.xfer2([32 | 128,0])
accres = 2
accrate = 13
print 'Offsets: '
print xoffset[1]
print yoffset[1]
# print str(zoffset[1]) + "\n\nRead the ADXL345 every half second:"

# Initialize the ADXL345
def initadxl345():
    # Enter power saving state
    spi.xfer2([45, 0])
    # Set data rate to 100 Hz. 15=3200, 14=1600, 13=800, 12=400, 11=200, 10=100 etc.
    spi.xfer2([44, accrate])

    # Enable full range (10 bits resolution) and +/- 16g 4 LSB
    spi.xfer2([49, accres])

    # Enable measurement
    spi.xfer2([45, 8])

# Read the ADXL x-y-z axia
def readadxl345():
    rx = spi.xfer2([242,0,0,0,0,0,0])

    #
    out = [rx[1] | (rx[2] << 8),rx[3] | (rx[4] << 8),rx[5] | (rx[6] << 8)]
    # Format x-axis
    if (out[0] & (1<<16 - 1 )):
        out[0] = out[0] - (1<<16)
#   out[0] = out[0] * 0.004 * 9.82
    # Format y-axis
    if (out[1] & (1<<16 - 1 )):
        out[1] = out[1] - (1<<16)
#   out[1] = out[1] * 0.004 * 9.82
    # Format z-axis
    if (out[2] & (1<<16 - 1 )):
        out[2] = out[2] - (1<<16)
#   out[2] = out[2] * 0.004 * 9.82

    return out

# Initialize the ADXL345 accelerometer
initadxl345()

# Read the ADXL345 every half second
timetosend = 60
while(1):
    with open('/proc/uptime','r') as f: # get uptime
        uptime_start = float(f.readline().split()[0])
    uptime_last = uptime_start
    active_file_first = "S3-" + str(pow(2,accrate)*25/256) + "hz10bit" + str(accres) + 'g' + str(datetime.datetime.utcnow().strftime('%y%m%d%H%M')) $
    active_file = active_file_first.replace(":", ".")
    wStream = open('/var/log/sensor/' + active_file,'wb')
    finalcount = 0
    print "Creating " + active_file
    while uptime_last < uptime_start + timetosend:
        finalcount += 1
        time1 = str(datetime.datetime.now().strftime('%S.%f'))
        time2 = str(datetime.datetime.now().strftime('%M'))
        time3 = str(datetime.datetime.now().strftime('%H'))
        time4 = str(datetime.datetime.now().strftime('%d'))
        time5 = str(datetime.datetime.now().strftime('%m'))
        time6 = str(datetime.datetime.now().strftime('%Y'))
        axia = readadxl345()
        wStream.write(str(round(float(axia[0])/1024,3))+','+str(round(float(axia[1])/1024,3))+','+str(round(float(axia[2])/1024,3))+','+time1+','+ti$
    # Print the reading
    # print axia[0]
    # print axia[1]
    # print str(axia[2]) + '\n'
    # elapsed = time.clock()
    # current = 0
    # while(current < timeout):
    #   current = time.clock() - elapsed
        with open('/proc/uptime', 'r') as f:
                uptime_last = float(f.readline().split()[0])
    wStream.close()

def doftp(the_active_file):
    session = ftplib.FTP('192.0.3.6','sensor3','L!ghtSp33d')
    session.cwd("//datalogger//")
    file = open('/var/log/sensor/' + active_file, 'rb')   # file to send
    session.storbinary('STOR' + active_file, file)        # send the file
    file.close()
    session.quit

スクリプトを実行すると、他の 2 つの Raspberry Pi で次のように表示されます。

デバイス ID (0xe5 である必要があります):

0xe5

オフセット:

0

0

これは、スクリプトを実行する前に加速度計がどのように配置されていても、3 台目の Raspberry Pi でも同じであるはずです。ただし、何らかの理由で、新しい Raspberry Pi で次のような出力が得られます。

デバイス ID (0xe5 である必要があります):

0x1

オフセット:

1

1

場合によっては、まったく異なるデバイス ID とオフセットが表示されることがあります。

3 つの Raspberry Pi はすべて、 と の両方/etc/modulesでまったく同じ/boot/config.txtです。

ls /dev/*spi*両方で実行すると/dev/spidev0.0 /dev/spidev0.1、3 つの Raspberry Pi がすべて取得されます。

Raspberry Pi 間で MicroSD カードを交換した後、問題がハードウェアとは関係ないことが明らかになりました。それはソフトウェアに帰着します。

この問題を解決する方法を知っている人はいますか? 適切なデバイス ID とオフセットが表示されないという事実は、私が収集したデータをめちゃくちゃにして役に立たなくするだけです。前もって感謝します。

4

0 に答える 0