-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpu.py
More file actions
40 lines (37 loc) · 1.43 KB
/
mpu.py
File metadata and controls
40 lines (37 loc) · 1.43 KB
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
import smbus
import logging
class MPU:
def __init__(self):
self.PWR_MGMT_1 = 0x6B
self.SMPLRT_DIV = 0x19
self.CONFIG = 0x1A
self.GYRO_CONFIG = 0x1B
self.INT_ENABLE = 0x38
self.ACCEL_XOUT_H = 0x3B
self.ACCEL_YOUT_H = 0x3D
self.ACCEL_ZOUT_H = 0x3F
self.GYRO_XOUT_H = 0x43
self.GYRO_YOUT_H = 0x45
self.GYRO_ZOUT_H = 0x47
self.BUS = smbus.SMBus(1)
self.DEVICE_ADDRESS = 0x68
self.mpu_init()
def mpu_init(self):
try:
self.BUS.write_byte_data(self.DEVICE_ADDRESS, self.SMPLRT_DIV, 7)
self.BUS.write_byte_data(self.DEVICE_ADDRESS, self.PWR_MGMT_1, 1)
self.BUS.write_byte_data(self.DEVICE_ADDRESS, self.CONFIG, 0)
self.BUS.write_byte_data(self.DEVICE_ADDRESS, self.GYRO_CONFIG, 24)
self.BUS.write_byte_data(self.DEVICE_ADDRESS, self.INT_ENABLE, 1)
except Exception as e:
logging.exception("An exception occurred in MPU_Init: %s", e)
def read_raw_data(self, addr):
try:
high = self.BUS.read_byte_data(self.DEVICE_ADDRESS, addr)
low = self.BUS.read_byte_data(self.DEVICE_ADDRESS, addr + 1)
value = (high << 8) | low
if value > 32768:
value = value - 65536
return value
except Exception as e:
logging.exception("An exception occurred in read_raw_data: %s", e)