-
Notifications
You must be signed in to change notification settings - Fork 1
/
gyro5.py
67 lines (51 loc) · 1.88 KB
/
gyro5.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
# Gyroscope L3GD20H Driver
# Returns Angle Values in dps
# Based on LSM303D driver from Fayetteville Free Library Robotics Group
# Group G - EME - University of Strathclyde
# import required libraries
from smbus:armhf import SMBus
# Defining any required functions
def twos_comp_combine(msb, lsb):
twos_comp = 256*msb + lsb
if twos_comp >= 32768:
return twos_comp - 65536
else:
return twos_comp
# set I2C bus
busNum = 1
bus = SMBus(busNum)
L3G = 0x6b # I2C register
L3G_WHOAMI = 0b11010111
# Defining the control registers from L3GD20H Datasheet
CTRL_GYRO_1 = 0x20 #Page 36 of Datasheet;
CTRL_GYRO_2 = 0x21 #Page 38
CTRL_GYRO_3 = 0x22 #Page 39
CTRL_GYRO_4 = 0x23 #Page 39
CTRL_GYRO_5 = 0x24 #Page 40
CTRL_GYRO_6 = 0x39 #Page 48
# Registers holding twos complemented gyro values
GYRO_X_LSB = 0x28
GYRO_X_MSB = 0x29
GYRO_Y_LSB = 0x2A
GYRO_Y_MSB = 0x2B
GYRO_Z_LSB = 0x2C
GYRO_Z_MSB = 0x2D
# Temperature Data
TEMP_Out = 0x26
# Writing required settings to Gyro
if bus.read_byte_data(L3G, 0x0f) == L3G_WHOAMI:
print ('L3GD20H successfully detected')
else:
print ('L3GD20H not detected')
bus.write_byte_data(L3G, CTRL_GYRO_1, 0b00001111) #enable x,y,z; choose frequency and bandwidth
bus.write_byte_data(L3G, CTRL_GYRO_2, 0x00) # sets high pass filter
bus.write_byte_data(L3G, CTRL_GYRO_6, 0b000000) # sets LOW_ODR
Gyro_Sens = 0.00875 # Gyro Sensitivity Values based on Defined Range.
# Reading 2's complemented results from gyroscope
gyrox = twos_comp_combine(bus.read_byte_data(L3G, GYRO_X_MSB), bus.read_byte_data(L3G, GYRO_X_LSB))
gyroy = twos_comp_combine(bus.read_byte_data(L3G, GYRO_Y_MSB), bus.read_byte_data(L3G, GYRO_Y_LSB))
gyroz = twos_comp_combine(bus.read_byte_data(L3G, GYRO_Z_MSB), bus.read_byte_data(L3G, GYRO_Z_LSB))
# Define Gyro Readings in dps
Gyro_X_dps = gyrox*Gyro_Sens
Gyro_Y_dps = gyroy*Gyro_Sens
Gyro_Z_dps = gyroz*Gyro_Sens