I have an ADXL345 Accelerometer attached via SPI pins to my Raspberry Pi. I use the following Python script to collect data:
#!/usr/bin/python # -*- coding: utf-8 -*- import sys, math, os, spidev, datetime, ftplib # Setup SPI spi = spidev.SpiDev() 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)) + '\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 = 15 print 'Offsets: ' print xoffset print yoffset # Initialize the ADXL345 def initadxl345(): # Enter power saving state spi.xfer2([45, 0]) # Set data rate (accrate = 15 -> 3200 Hz, 14=1600 Hz, 13=800 Hz, 12=400 Hz 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 | (rx << 8),rx | (rx << 8),rx | (rx << 8$ # Format x-axis if (out & (1<<16 - 1 )): out = out - (1<<16) # Format y-axis if (out & (1<<16 - 1 )): out = out - (1<<16) # Format z-axis if (out & (1<<16 - 1 )): out = out - (1<<16) 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()) uptime_last = uptime_start active_file_first = "S1-" + str(pow(2,accrate)*25/256) + "hz10bit" + $ 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')) #time6 = str(datetime.datetime.now().strftime('%Y')) axia = readadxl345() wStream.write(str(round(float(axia)/1024,11))+','+time1+','+time2+',\n') with open('/proc/uptime', 'r') as f: uptime_last = float(f.readline().split()) wStream.close() def doftp(the_active_file): session = ftplib.FTP('18.104.22.168','sensor1','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() # close file and FTP session.quit
As you can tell from the script I can give my ADXL345 Accelerometer a Sample Rate (e.g. 3200 Hz). However when I go through the logged data (no matter what Sample Rate I chose) there always seems to be a problem.
It feels like the Raspberry Pi or SPi pins have a Sample Rate of their own (?) around (2000-2400 Hz) which ruins the data collected. That Sample Rate even varies throughout the tests which results in different amount of data points over e.g. 10 seconds. Sometimes I get more data points than the previous test and sometimes fewer.
Is it something in my script causing this issue? Does the SPI pins have a Sample Rate of their own or what's going on here?
OBS! I'm new to Python and the abovementioned script is one I found online. I have only done edits.