简体   繁体   English

使用相机快门在树莓派上触发MPU6050

[英]Using camera shutter to trigger MPU6050 on raspberry pi

I've connected DSLR to RPi by GPIO; 我已经通过GPIO将DSLR连接到RPi; and MPU6050 to RPi using I2C. 使用I2C将MPU6050转换为RPi。 I'm trying to record the gyro and accelerometer data in a small time window (typically less than 50ms) when I press camera shutter button. 当我按下相机快门按钮时,我试图在一个小的时间窗口(通常少于50ms)中记录陀螺仪和加速度计数据。 My desired sampling rate is somewhere in between 500Hz to 1000Hz. 我想要的采样率在500Hz至1000Hz之间。 And I use FIFO to temporarily store the sensor data. 我使用FIFO临时存储传感器数据。

The current problem is that the code sometimes oversamples (big offset comparing to the theoretical value). 当前的问题是代码有时会过采样(与理论值相比偏移较大)。 For example, when I set 500Hz for 100ms--I would expect 50 samples but it turned out 62 samples. 例如,当我将500Hz设置为100ms时,我希望有50个样本,但结果却有62个样本。 I don't have this problem if running MPU6050 without syncing the camera. 如果在不同步摄像机的情况下运行MPU6050,则不会出现此问题。

Also, I'm confused what is 另外,我很困惑

Status = mpu6050.readStatus()
if (Status & 0x10) == 0x10 :
    print "Overrun Error! Quitting.\n"

for? 对于?

I'd really appreciate if someone can help. 如果有人可以提供帮助,我将不胜感激。 Thanks! 谢谢!

Python code: 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...")

Solved it by using SPI instead of I2C. 通过使用SPI而不是I2C解决了该问题。 Now I can get data at stable 2000HZ sampling rate. 现在,我可以以稳定的2000HZ采样率获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM