我已经通过 GPIO 将 DSLR 连接到 RPi;和 MPU6050 使用 I2C 到 RPi。当我按下相机快门按钮时,我试图在一个小时间窗口(通常小于 50 毫秒)内记录陀螺仪和加速度计数据。我想要的采样率在 500Hz 到 1000Hz 之间。我使用 FIFO 来临时存储传感器数据。
当前的问题是代码有时会过采样(与理论值相比有很大的偏移)。例如,当我将 500Hz 设置为 100 毫秒时——我希望有 50 个样本,但结果却是 62 个样本。如果在不同步相机的情况下运行 MPU6050,我没有这个问题。
另外,我很困惑什么是
Status = mpu6050.readStatus()
if (Status & 0x10) == 0x10 :
print "Overrun Error! Quitting.\n"
为了?
如果有人可以提供帮助,我将不胜感激。谢谢!
Python代码:
import datetime
import MPU6050
import math
import time
import numpy
import RPi.GPIO as GPIO
import time
import sys
GPIO.setmode(GPIO.BCM)
TargetSampleTime= 20 #int(sys.argv[1])
TargetRate= 500 #float(sys.argv[2])
GPIO.setup(24,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
mpu6050 = MPU6050.MPU6050()
mpu6050.setup()
mpu6050.setGResolution(2)
mpu6050.setSampleRate(TargetRate)
mpu6050.enableFifo(False)
time.sleep(0.01)
print "Capturing in {0} ms at {1} samples/sec".format(TargetSampleTime, mpu6050.SampleRate)
mpu6050.resetFifo()
mpu6050.enableFifo(True)
time.sleep(0.01)
Values = []
Total = 0
def my_callback(channel):
print "Rising edge detected on 24"
GPIO.remove_event_detect(24)
GPIO.cleanup()
a = datetime.datetime.now()
# read MPU6050 from FIFO
while True:
Values.extend(mpu6050.readDataFromFifo())
b = datetime.datetime.now()
dt = int((b-a).microseconds/1000)
if dt >= TargetSampleTime:
break;
b = datetime.datetime.now()
Total = len(Values)/14
print "Capture in {0} ms".format((b-a).microseconds/1000)
print "Captured {0} samples".format(Total)
if Total > 0:
Status = mpu6050.readStatus()
if (Status & 0x10) == 0x10 :
print "Overrun Error! Quitting.\n"
quit()
# writing IMU data to txt
print "Saving RawData.txt file."
FO = open("RawData.txt","w")
FO.write("GT\tGx\tGy\tGz\tTemperature\tGyrox\tGyroy\tGyroz\n")
fftdata = []
for loop in range (Total):
SimpleSample = Values[loop*14 : loop*14+14]
I = mpu6050.convertData(SimpleSample)
CurrentForce = math.sqrt( (I.Gx * I.Gx) + (I.Gy * I.Gy) +(I.Gz * I.Gz))
fftdata.append(CurrentForce)
FO.write("{0:6.3f}\t{1:6.3f}\t{2:6.3f}\t{3:6.3f}\t".format(CurrentForce, I.Gx , I.Gy, I.Gz))
FO.write("{0:5.1f}\t{1:6.3f}\t{2:6.3f}\t{3:6.3f}\n".format(I.Temperature,I.Gyrox,I.Gyroy,I.Gyroz))
FO.close()
quit()
# detect shutter button press
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback, bouncetime=100)
raw_input("Listening...")