0%

IMU:MPU6050

1. Intro

MPU6050 is a IMU that can measure acceleration and orientation

This is part of the localization system, could be combined with encoder and lidar to provide robot a high-accuracy localization system

IMG_7084

2. Setup

Communicate with a board by I2C/IIC

  • SDA: data
  • SCL: clock

Check if the system detected

1
2
3
4
5
6
7
8
9
10
11
sudo i2cdetect -r -y 1
## output
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Which means the MPU6050’s address is 68

3. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# read_word and read_word_2c from http://blog.bitify.co.uk/2013/11/reading-data-from-mpu-6050-on-raspberry.html
def read_word(adr):
high = bus.read_byte_data(ADDR, adr)
low = bus.read_byte_data(ADDR, adr+1)
val = (high << 8) + low
return val

def read_word_2c(adr):
val = read_word(adr)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val


# Read the acceleration vals
accel_x = read_word_2c(ACCEL_XOUT_H) / 16384.0
accel_y = read_word_2c(ACCEL_YOUT_H) / 16384.0
accel_z = read_word_2c(ACCEL_ZOUT_H) / 16384.0
# Read the gyro vals
gyro_x = read_word_2c(GYRO_XOUT_H) / 131.0
gyro_y = read_word_2c(GYRO_YOUT_H) / 131.0
gyro_z = read_word_2c(GYRO_ZOUT_H) / 131.0