-
Notifications
You must be signed in to change notification settings - Fork 0
/
imu.py
55 lines (47 loc) · 1.31 KB
/
imu.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
# IMU - Inertial measurement unit
# This interfaces with the SparkFun 9 Degrees of Freedom Sensor Stick
# https://www.sparkfun.com/products/10724
class SEN10724IMU(object):
def __init__(self):
from i2clibraries import i2c_hmc5883l
from i2clibraries import i2c_adxl345
from i2clibraries import i2c_itg3205
i2c_port = 0 # different versions of the pi use different ports
self.accelerometer = i2c_adxl345.i2c_adxl345(i2c_port)
self.accelerometer.setScale(2)
self.gyroscope = i2c_itg3205.i2c_itg3205(i2c_port, addr=0x68)
self.magnetometer = i2c_hmc5883l.i2c_hmc5883l(i2c_port)
self.magnetometer.setContinuousMode()
self.magnetometer.setDeclination(1,43) # magnetic declination in degrees west (degrees, minute)
print("SEN10724 IMU initialised")
@property
def acceleration(self):
try:
return self.accelerometer.getAxes()
except OSError as e:
print(e)
return None
@property
def rotation(self):
try:
return self.gyroscope.getDegPerSecAxes()
except OSError as e:
print(e)
return None
@property
def magfield(self):
try:
return self.magnetometer.getAxes()
except OSError as e:
print(e)
return None
class FakeIMU(object):
@property
def acceleration(self):
return (0,0,0)
@property
def rotation(self):
return (0,0,0)
@property
def magfield(self):
return (0,0,0)