-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpu6050.py
executable file
·178 lines (128 loc) · 6.01 KB
/
mpu6050.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
import math
import smbus
import i2cutils as I2CUtils
class MPU6050(object):
PWR_MGMT_1 = 0x6b
FS_SEL = 0x1b
FS_250 = 0
FS_500 = 1
FS_1000 = 2
FS_2000 = 3
AFS_SEL = 0x1c
AFS_2g = 0
AFS_4g = 1
AFS_8g = 2
AFS_16g = 3
ACCEL_START_BLOCK = 0x3b
ACCEL_XOUT_H = 0
ACCEL_XOUT_L = 1
ACCEL_YOUT_H = 2
ACCEL_YOUT_L = 3
ACCEL_ZOUT_H = 4
ACCEL_ZOUT_L = 5
ACCEL_SCALE = { AFS_2g : [ 2, 16384.0], AFS_4g : [ 4, 8192.0], AFS_8g : [ 8, 4096.0], AFS_16g : [16, 2048.0] }
TEMP_START_BLOCK = 0x41
TEMP_OUT_H = 0
TEMP_OUT_L = 1
GYRO_START_BLOCK = 0x43
GYRO_XOUT_H = 0
GYRO_XOUT_L = 1
GYRO_YOUT_H = 2
GYRO_YOUT_L = 3
GYRO_ZOUT_H = 4
GYRO_ZOUT_L = 5
GYRO_SCALE = { FS_250 : [ 250, 131.0], FS_500 : [ 500, 65.5], FS_1000 : [1000, 32.8], FS_2000 : [2000, 16.4] }
def __init__(self, bus, address, name, fs_scale=FS_250, afs_scale=AFS_2g):
self.bus = bus
self.address = address
# We need to wake up the module as it start in sleep mode
I2CUtils.i2c_write_byte(self.bus, self.address, MPU6050.PWR_MGMT_1, 0)
self.name = name
self.fs_scale = fs_scale
self.afs_scale = afs_scale
self.raw_gyro_data = [0, 0, 0, 0, 0, 0]
self.raw_accel_data = [0, 0, 0, 0, 0, 0]
self.raw_temp_data = [0, 0]
self.gyro_raw_x = 0
self.gyro_raw_y = 0
self.gyro_raw_z = 0
self.gyro_scaled_x = 0
self.gyro_scaled_y = 0
self.gyro_scaled_z = 0
self.raw_temp = 0
self.scaled_temp = 0
self.accel_raw_x = 0
self.accel_raw_y = 0
self.accel_raw_z = 0
self.accel_scaled_x = 0
self.accel_scaled_y = 0
self.accel_scaled_z = 0
self.pitch = 0.0
self.roll = 0.0
# We need to wake up the module as it start in sleep mode
#I2CUtils.i2c_write_byte(self.bus, self.address, MPU6050.PWR_MGMT_1, 0)
# Set the gryo resolution
I2CUtils.i2c_write_byte(self.bus, self.address, MPU6050.FS_SEL, self.fs_scale << 3)
# Set the accelerometer resolution
I2CUtils.i2c_write_byte(self.bus, self.address, MPU6050.AFS_SEL, self.afs_scale << 3)
def read_raw_data(self):
self.raw_gyro_data = I2CUtils.i2c_read_block(self.bus, self.address, MPU6050.GYRO_START_BLOCK, 6)
self.raw_accel_data = I2CUtils.i2c_read_block(self.bus, self.address, MPU6050.ACCEL_START_BLOCK, 6)
self.raw_temp_data = I2CUtils.i2c_read_block(self.bus, self.address, MPU6050.TEMP_START_BLOCK, 2)
self.gyro_raw_x = I2CUtils.twos_compliment(self.raw_gyro_data[MPU6050.GYRO_XOUT_H], self.raw_gyro_data[MPU6050.GYRO_XOUT_L])
self.gyro_raw_y = I2CUtils.twos_compliment(self.raw_gyro_data[MPU6050.GYRO_YOUT_H], self.raw_gyro_data[MPU6050.GYRO_YOUT_L])
self.gyro_raw_z = I2CUtils.twos_compliment(self.raw_gyro_data[MPU6050.GYRO_ZOUT_H], self.raw_gyro_data[MPU6050.GYRO_ZOUT_L])
self.accel_raw_x = I2CUtils.twos_compliment(self.raw_accel_data[MPU6050.ACCEL_XOUT_H], self.raw_accel_data[MPU6050.ACCEL_XOUT_L])
self.accel_raw_y = I2CUtils.twos_compliment(self.raw_accel_data[MPU6050.ACCEL_YOUT_H], self.raw_accel_data[MPU6050.ACCEL_YOUT_L])
self.accel_raw_z = I2CUtils.twos_compliment(self.raw_accel_data[MPU6050.ACCEL_ZOUT_H], self.raw_accel_data[MPU6050.ACCEL_ZOUT_L])
self.raw_temp = I2CUtils.twos_compliment(self.raw_temp_data[MPU6050.TEMP_OUT_H], self.raw_temp_data[MPU6050.TEMP_OUT_L])
# We convert these to radians for consistency and so we can easily combine later in the filter
self.gyro_scaled_x = math.radians(self.gyro_raw_x / MPU6050.GYRO_SCALE[self.fs_scale][1])
self.gyro_scaled_y = math.radians(self.gyro_raw_y / MPU6050.GYRO_SCALE[self.fs_scale][1])
self.gyro_scaled_z = math.radians(self.gyro_raw_z / MPU6050.GYRO_SCALE[self.fs_scale][1])
self.scaled_temp = self.raw_temp / 340 + 36.53
self.accel_scaled_x = self.accel_raw_x / MPU6050.ACCEL_SCALE[self.afs_scale][1]
self.accel_scaled_y = self.accel_raw_y / MPU6050.ACCEL_SCALE[self.afs_scale][1]
self.accel_scaled_z = self.accel_raw_z / MPU6050.ACCEL_SCALE[self.afs_scale][1]
self.pitch = self.read_x_rotation(self.read_scaled_accel_x(),self.read_scaled_accel_y(),self.read_scaled_accel_z())
self.roll = self.read_y_rotation(self.read_scaled_accel_x(),self.read_scaled_accel_y(),self.read_scaled_accel_z())
def distance(self, x, y):
return math.sqrt((x * x) + (y * y))
def read_x_rotation(self, x, y, z):
return math.atan2(y, self.distance(x, z))
def read_y_rotation(self, x, y, z):
return -math.atan2(x, self.distance(y, z))
def read_raw_accel_x(self):
return self.accel_raw_x
def read_raw_accel_y(self):
return self.accel_raw_y
def read_raw_accel_z(self):
return self.accel_raw_z
def read_scaled_accel_x(self):
return self.accel_scaled_x
def read_scaled_accel_y(self):
return self.accel_scaled_y
def read_scaled_accel_z(self):
return self.accel_scaled_z
def read_raw_gyro_x(self):
return self.gyro_raw_x
def read_raw_gyro_y(self):
return self.gyro_raw_y
def read_raw_gyro_z(self):
return self.gyro_raw_z
def read_scaled_gyro_x(self):
return self.gyro_scaled_x
def read_scaled_gyro_y(self):
return self.gyro_scaled_y
def read_scaled_gyro_z(self):
return self.gyro_scaled_z
def read_temp(self):
return self.scaled_temp
def read_pitch(self):
return self.pitch
def read_roll(self):
return self.roll
def read_all(self):
self.read_raw_data()
return (self.pitch, self.roll, self.gyro_scaled_x, self.gyro_scaled_y, self.gyro_scaled_z, self.accel_scaled_x, self.accel_scaled_y, self.accel_scaled_z)