-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gyro Updated Code.txt
130 lines (106 loc) · 2.96 KB
/
Gyro Updated Code.txt
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
#include <Wire.h>
// For storing the raw values from gyro - rotational velocity
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
byte L3G4200D_address = 0b1101001; // 105
void setup() {
Serial.begin(9600);
Wire.begin();
setupGyro50(250);
Serial.println("Sensor Initiated");
delay(1500);
}
void loop() {
gyroValues(2000);
printData();
delay(250);
}
int setupGyro50(int scale){
// Listing of the 8 bit registers embedded in the device, and the related addresses
// For the following please refer from page 29 on knowing how to assign the value
writeRegister(L3G4200D_address, 0x20, 0b00001111);
writeRegister(L3G4200D_address, 0x21, 0b00000000); // must be set to 0 to ensure proper operation of device
if (scale==250){
writeRegister(L3G4200D_address, 0x23, 0b00000000);
}
else if (scale==500){
writeRegister(L3G4200D_address, 0x23, 0b00010000);
}
else{
writeRegister(L3G4200D_address, 0x23, 0b00110000);
}
}
void writeRegister(byte deviceAddress, byte address, byte val){
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.write(val);
Wire.endTransmission();
}
long gyroValues(int scale){
// Refer page 27
byte xMSB = readRegister(L3G4200D_address, 0x29);
byte xLSB = readRegister(L3G4200D_address, 0x28);
Serial.print("xMSB = "); Serial.println(xMSB);
Serial.print("xLSB = "); Serial.println(xLSB);
gyroX = ((xMSB << 8) | xLSB); // bitshift left and bitwise or
Serial.print("gyroX value= ");
Serial.println(gyroX);
byte yMSB = readRegister(L3G4200D_address, 0x2B);
byte yLSB = readRegister(L3G4200D_address, 0x2A);
gyroY = ((yMSB << 8) | yLSB);
byte zMSB = readRegister(L3G4200D_address, 0x2D);
byte zLSB = readRegister(L3G4200D_address, 0x2C);
gyroZ = ((zMSB << 8) | zLSB);
processGyroData(scale);
}
int readRegister(byte deviceAddress, byte address){
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1);
while(!Wire.available()){
}
v = Wire.read();
Serial.print("v = "); Serial.println(v);
return v;
}
// Below sensitivity values can be obtained from the datasheet
float processGyroData(int dps){
if (dps==250){
rotX = gyroX*8.5 / 1000;
rotY = gyroY*8.5 / 1000;
rotZ = gyroZ*8.5 / 1000;
}
else if (dps==500){
rotX = gyroX*17.5 / 1000;
rotY = gyroY*17.5 / 1000;
rotZ = gyroZ*17.5 / 1000;
}else{
rotX = gyroX*70 / 1000;
rotY = gyroY*70 / 1000;
rotZ = gyroZ*70 / 1000;
}
void printData(){
float low = -0.6; // can be varied
float high = 0.6; // can be varied
Serial.print("Gyro (deg)");
Serial.print(" X=");
if((rotX>low)&&(rotX<high)){
Serial.print("0.00");
} else{
Serial.print(rotX);
}
Serial.print(" Y=");
if((rotY>low)&&(rotY<high)){
Serial.print("0.00");
} else{
Serial.print(rotY);
}
Serial.print(" Z=");
if((rotZ>low)&&(rotZ<high)){
Serial.print("0.00");
} else{
Serial.print(rotZ);
}
}