Using camera shutter to trigger MPU6050 on raspberry pi

515 views Asked by At

I've connected DSLR to RPi by GPIO; and MPU6050 to RPi using I2C. 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. My desired sampling rate is somewhere in between 500Hz to 1000Hz. And I use FIFO to temporarily store the sensor data.

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. I don't have this problem if running MPU6050 without syncing the camera.

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:

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...")
1

There are 1 answers

1
Yi Zhang On BEST ANSWER

Solved it by using SPI instead of I2C. Now I can get data at stable 2000HZ sampling rate.