I am using the Raspberry Pi 3 Model B to read in two MPU6050 sensors at the same time. I faced some initial problems in setting up everything but now I am able to read in data relatively fast.
My concern is that, both of my MPU6050 units are outputting roughly the same data. Is this supposed to happen or have I made a mistake while setting them up? To provide context I'm attaching a screenshot of my data and also a quick excel chart which shows the comparison.
My question is: Is this behaviour normal? If not, then is there an issue with the hardware or something lacking in the code?
The code is as follows:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Acquire data from two accelerometers
#and two gyroscope sensors
import smbus
import math # Mathmatics
import time # Time Module
import datetime
import numpy as np
#
# Define
#
# Slave address
DEV_ADDR1 = 0x68
DEV_ADDR2 = 0x69
# Device Address
# Register address
ACCEL_XOUT = 0x3b
ACCEL_YOUT = 0x3d
ACCEL_ZOUT = 0x3f
TEMP_OUT = 0x41
GYRO_XOUT = 0x43
GYRO_YOUT = 0x45
GYRO_ZOUT = 0x47
PWR_MGMT_1 = 0x6b # PWR_MGMT_1
PWR_MGMT_2 = 0x6c # PWR_MGMT_2
ACCEL_CONFIG = 0x1C
GYRO_CONFIG = 0x1B
bus = smbus.SMBus(1)
# Sleep released.
bus.write_byte_data(DEV_ADDR1, PWR_MGMT_1, 0)
bus.write_byte_data(DEV_ADDR2, PWR_MGMT_1, 0)
## GYROSENSOR SELF TEST
bus.write_byte_data(DEV_ADDR1, GYRO_CONFIG, 0b11100000)
bus.write_byte_data(DEV_ADDR2, GYRO_CONFIG, 0b11100000)
## +/-2g
bus.write_byte_data(DEV_ADDR1, ACCEL_CONFIG, 0b00000000)
bus.write_byte_data(DEV_ADDR2, ACCEL_CONFIG, 0b00000000)
## +/-4g
##bus.write_byte_data(DEV_ADDR1, ACCEL_CONFIG, 0b00001000)
##bus.write_byte_data(DEV_ADDR2, ACCEL_CONFIG, 0b00001000)
#
# Sub function
#
# 1byte read sensor accel 1
def read_byte(adr):
return bus.read_byte_data(DEV_ADDR1, adr)
# 2byte read
def read_word(adr):
high = bus.read_byte_data(DEV_ADDR1 , adr)
low = bus.read_byte_data (DEV_ADDR1 , adr+1)
val = (high << 8) + low
return val
# Sensor data read
def read_word_sensor(adr):
val = read_word(adr)
if (val >= 0x8000): # Minus
return -(( 65535 - val) + 1 )
else: # Plus
return val
# 1byte read sensor accel 2
def read_byte1(adr):
return bus.read_byte_data(DEV_ADDR2, adr)
# 2byte read
def read_word1(adr):
high1 = bus.read_byte_data(DEV_ADDR2 , adr)
low1 = bus.read_byte_data (DEV_ADDR2 , adr+1)
val1 = (high1 << 8) + low1
return val1
# Sensor data read
def read_word_sensor1(adr):
val1 = read_word1(adr)
if (val1 >= 0x8000): # Minus
return -(( 65535 - val1) + 1 )
else: # Plus
return val1
nis = 1
# Get accel data sensor 0
def get_accel_data_lsb():
x = read_word_sensor(ACCEL_XOUT)
y = read_word_sensor(ACCEL_YOUT)
z = read_word_sensor(ACCEL_ZOUT)
return [x, y, z]
# Get accel data scaled sensor 0
def get_accel_data_g():
x, y, z = get_accel_data_lsb()
x = x / 16384.0
y = y / 16384.0
z = z / 16384.0
return [x, y, z]
# Get gyro data sensor 0
def get_gyro_data():
gyro_x = read_word_sensor(GYRO_XOUT)
gyro_y = read_word_sensor(GYRO_YOUT)
gyro_z = read_word_sensor(GYRO_ZOUT)
return [gyro_x, gyro_y, gyro_z]
## Get temperature sensor 0
#def get_temp_data():
# temp = read_word_sensor(TEMP_OUT)
# return [temp]
# Get accel data sensor 1
def get_accel_data_lsb1():
xa = read_word_sensor1(ACCEL_XOUT)
ya = read_word_sensor1(ACCEL_YOUT)
za = read_word_sensor1(ACCEL_ZOUT)
return [xa, ya, za]
# Get accel data scaled sensor 1
def get_accel_data_g1():
xa, ya, za = get_accel_data_lsb1()
xa = xa / 16384.0
ya = ya / 16384.0
za = za / 16384.0
return [xa, ya, za]
# Get gyro data sensor 1
def get_gyro_data1():
gyro_xa = read_word_sensor1(GYRO_XOUT)
gyro_ya = read_word_sensor1(GYRO_YOUT)
gyro_za = read_word_sensor1(GYRO_ZOUT)
return [gyro_xa, gyro_ya, gyro_za]
## Get temperature sensor 1
#def get_temp_data1():
# temp1 = read_word_sensor1(TEMP_OUT)
# return [temp1]
# (Calculation of the slope of the 1 axis) slope calculation for accel data
# If inclined only one axis can be calculated by this function.
## def calc_slope_for_accel_1axis(x , y , z): # Radian
## if x >1: x =1
## elif x < - 1: x = - 1
## slope_x = math.asin (x / 1 )
# X-, when tilted Z axis fixed. Y-axis.
## if y > 1: y = 1
## elif y < - 1: y = - 1
## slope_y = math.asin (y / 1 )
## # X-, when inclined by a Y-axis fixed. Z-axis.
## if z > 1: z = 1
## elif z < - 1: z = - 1
## slope_z = math.asin(z / 1 )
## return [slope_x, slope_y, slope_z]
#####################################################################################
# Main function
#####################################################################################
f = open('/home/pi/projects/accelerometer/data/accel/22_03_17_carotide_A1.txt','w')
#f1 = open('/home/pi/projects/accelerometer/data/accel/22_03_17_carotide_B1.txt','w')
while 1:
now = datetime.datetime.now()
timestamp1 = now.strftime("%H;%M;%S;%f")
accel_x1, accel_y1, accel_z1 = get_accel_data_g()
accel_x2, accel_y2, accel_z2 = get_accel_data_g1()
gyro_x, gyro_y, gyro_z = get_gyro_data()
gyro_xa, gyro_ya, gyro_za = get_gyro_data1()
now = datetime.datetime.now()
timestamp2 = now.strftime("%H;%M;%S;%f")
# temp = get_temp_data()
# temp1 = get_temp_data1()
## slope_x1, slope_y1, slope_z1 = calc_slope_for_accel_1axis (accel_x1, accel_y1, accel_z1)
## slope_x1 = math.degrees(slope_x1)
## slope_y1 = math.degrees(slope_y1)
## slope_z1 = math.degrees (slope_z1)
## slope_x2, slope_y2, slope_z2 = calc_slope_for_accel_1axis (accel_x2, accel_y2, accel_z2)
## slope_x2 = math.degrees(slope_x2)
## slope_y2 = math.degrees(slope_y2)
## slope_z2 = math.degrees (slope_z2)
data = nis, accel_x1, accel_y1, accel_z1
value1 = "%6.5f" %accel_x1
value2 = "%6.5f" %accel_y1
value3 = "%6.5f" %accel_z1
value4 = "%6.5f" %accel_x2
value5 = "%6.5f" %accel_y2
value6 = "%6.5f" %accel_z2
outstring = str(timestamp1) + " ;" + value1 + " ;" + value2 + " ;" + value3 + " ;" + ("%5d" % gyro_x) + " ;" + ("%5d" % gyro_y) + " ;" + ("%5d" % gyro_z) + "\n"
# outstring1 =str(timestamp2) + " ;" + value4 + " ;" + value5 + " ;" + value6 + " ;" + ("%5d" % gyro_xa) + " ;" + ("%5d" % gyro_ya) + " ;" + ("%5d" % gyro_za) + "\n"
# outstring = str(timestamp) + " ;" + value1 + " ;" + value2 + " ;" + value3 + " ;" + ("%5d" % gyro_x) + " ;" + ("%5d" % gyro_y) + " ;" + ("%5d" % gyro_z) + " ;" + ("%5.2d" % temp) + "\n"
# outstring1 =str(timestamp) + " ;" + value4 + " ;" + value5 + " ;" + value6 + " ;" + ("%5d" % gyro_xa) + " ;" + ("%5d" % gyro_ya) + " ;" + ("%5d" % gyro_za) + " ;" + temp1 + "\n"
# print 'inclination [theta]',
# print 'X: %06.3f' % slope_x1,
# print 'X: %06.3f' % slope_x2,
# print #newline.
# print 'Y: %06.3f' % slope_y1,
# print 'Y: %06.3f' % slope_y2,
# print #newline.
# print 'Z: %06.3f' % slope_z1,
# print 'Z: %06.3f' % slope_z2,
# print 'Z: %6.3f' % accel_z1,
# print 'Z: %6.3f' % accel_z2,
# print #newline.
f.write(outstring+'\n')
# f1.write(outstring1+'\n')
nis = nis+1
# END