Recording Data from MPU9250 to .csv File Consistent Timing

I am using the Navio+ board for recording IMU data from the MPU9250 and writing it to a .csv file. I’m having problems with it recording reliably. I tried a python script that was roughly based of the example provided in the GitHub repo, but I’m having problems with the delay. I have looked in the file for a setting to change the speed of data output on the actual IMU rather than after the data has been sent to the script, but I can’t seem to get it to work.

Is there a “best practices” way of recording data at a very consistent rate? I’d like to record at close to 1000 Hz for frequency analysis.

Specs: Navio+ Board on RPi 3 running Raspbian 8 Jessie

Code tested:

#pull the current UNIX time for the file name
currentUNIXTime = int(round(time.time() * 1000))
#create the file name from the time stamp and the IMU code
fileName = str(currentUNIXTime) + "_" + IMU_CODE + ".csv"
#open a file with the file name and write to it
fileToWrite = open(fileName, 'w')
#write the header string to the file
#set the counter to 1 because we have written 1 line to the file
counter = 1
#print the number of files written to the user
print "There have been {} files written NAVIO IMU".format(filesWritten)
#while we still have lines left to write to the file
while counter <= secondsPerFile * hertz:
	#get the accel, gyro, mag data from the IMU
	m9a, m9g, m9m = imu.getMotion9()

	#pull a time stamp for the data that we will write
	currentUNIXTime = int(round(time.time() * 1000))
	#create the string that we need to write to the file
	stringToWrite = str(currentUNIXTime) + "," + \
									str(m9a[0]) + "," + str(m9a[1]) + "," + str(m9a[2]) + "," + \
									str(m9g[0]) + "," + str(m9g[1])+ "," + str(m9g[2]) + "," + \
									str(m9m[0]) + "," + str(m9m[1]) + "," + str(m9m[2])  + "\n"		
	#write the data to the file

	#increment the counter by 1 because we have written a file
	counter = counter + 1
	#sleep so that the frequency of recording is correct
#once we have written enough data, close the file
#increment the files written counter by 1
filesWritten = filesWritten + 1

I had set this script to record at 800 Hz so freqOfRecording was 1/800 of a second for the delay time. This resulted in an actual recording rate of about 390 Hz.


UPDATE: I have experimented with the AHRS system written in C++, which already prints the frequency of recording. This solves the problem of not knowing what rate the sensor data is being recorded at. I can add in the IMU data and write all of the data to a .csv file from the script.

If I’m missing something glaring, please let me know. Thanks!

Better to stick with C/C++ when you need proper timings, Python is less predictable.

Thanks for the heads up!