我已经通过GPIO将DSLR连接到RPi;和MPU6050通过I2C连接到RPi。当我按下相机快门按钮时,我试图在一个小的时间窗口(通常小于50毫秒)记录陀螺仪和加速度计数据。我想要的采样率在500Hz到1000Hz之间。我使用FIFO来临时存储传感器数据。
当前的问题是代码有时过采样(与理论值相比偏移较大)。例如,当我将500Hz设置为100ms时—-我期望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("GTtGxtGytGztTemperaturetGyroxtGyroytGyrozn")
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...")
用SPI代替I2C解决。现在我可以以稳定的2000HZ采样率获得数据。