-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyro_tessting.ino
64 lines (51 loc) · 1.78 KB
/
gyro_tessting.ino
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
#include<Wire.h>
float ThetaX=0,ThetaY=0,ThetaZ=0;
float deltaTime=0;
float Taw=10;
void setup() {
Serial.begin(9600);
Wire.begin();
delay(20);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00); //power management register turn on code
Wire.endTransmission(true);
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(B00011000); //gyro full scale range +-1000rad/s
Wire.endTransmission(true);
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(B00010000); //accelerometer full scale range +-4g
Wire.endTransmission(true);
}
void loop()
{
unsigned long m1 = millis();
ReadGyroData();
Serial.print(ThetaX);Serial.print("|");Serial.print(ThetaY);Serial.print("|"); Serial.println(ThetaZ);
delay(100);
deltaTime=float(millis()-m1)/1000; //in seconds
}
void ReadGyroData()
{
//setting up to read from Acceleometer
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,14,true);
int Ax = (Wire.read()<<8|Wire.read());
int Ay = (Wire.read()<<8|Wire.read());
int Az = (Wire.read()<<8|Wire.read());
Wire.read();Wire.read(); //skipping temprature readings
int Gx = (Wire.read()<<8|Wire.read());
int Gy = (Wire.read()<<8|Wire.read());
int Gz = (Wire.read()<<8|Wire.read());
float ThetaGx = ThetaX+float(Gx)*(0.06103515624)*(deltaTime);
float ThetaGy = ThetaY+float(Gy)*(0.06103515624)*(deltaTime);
ThetaZ = (Taw/(Taw+deltaTime))*(ThetaZ+ (float(Gz)*(0.06103515624)*(deltaTime))); //HighPass filter
float ThetaAcX=atan(Ax/Az)*57.2957795131;
float ThetaAcY=atan(Ay/Az)*57.2957795131;
ThetaX=0.95*ThetaGx+0.05*ThetaAcX;
ThetaY=0.95*ThetaGy+0.05*ThetaAcY;
}