-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpu6050.py
58 lines (46 loc) · 1.33 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
# MPU6050
class mpu6050():
def __init__(self,con,devaddr):
self.con=con
self.devaddr=devaddr
self.wake()
# read from device
def _read(self, count, memaddr):
result = self.con.readfrom_mem (self.devaddr, memaddr, count)
return result
# write to device
def _write(self, data, memaddr):
result = self.con.writeto_mem(self.devaddr, memaddr, data)
return result
def signedIntFromBytes(self,x):
y = int.from_bytes(x)
if (y >= 0x8000):
return -((65535 - y) + 1)
else:
return y
def wake(self):
try:
self._write(b'\x01', 0x6B)
except:
print("wake Err??")
def sleep(self):
try:
self._write(b'\x40', 0x6B)
except:
print("sleep Err")
def readTemp(self):
try:
erg = self._read(2,65)
raw_temp = self.signedIntFromBytes(erg)
except:
print("read Temp Err??")
return float("NaN")
actual_temp = (raw_temp / 340) + 36.53
return round(actual_temp,2)
def startM(self):
# wake it up
self.wake()
self.accel_range(1)
self._ar = self.accel_range()
self.gyro_range(0)
self._gr = self.gyro_range()