-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGyro.cpp
More file actions
67 lines (53 loc) · 1.21 KB
/
Gyro.cpp
File metadata and controls
67 lines (53 loc) · 1.21 KB
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
#include "Gyro.h"
Gyro::Gyro() {
}
void Gyro::initialize() {
Wire.begin(); // i2c begin
if (!gyro.init()) // gyro init
{
Serial.println("Failed to autodetect gyro type!");
while (1);
}
gyro.enableDefault(); // gyro init. default 250/deg/s
delay(1000);// allow time for gyro to settle
gdriftx = 0;
gyro_xcumulative = 0;
gerrx = 0;
setGyroOffset();
//setGyroDrift();
prevTime = millis();
}
float Gyro::getGyro() {
return gyro_xcumulative;
}
void Gyro::updateGyro() {
Dt = (float)(millis()-prevTime)/1000.0;
gyro.read(); // read gyro
gyro_x=(float)(gyro.g.x-gerrx)*G_gain;
gyro_x = (gyro_x)*Dt;
//gyro_x -= gdriftx*Dt;
gyro_xcumulative += gyro_x;
prevTime=millis(); //reset prevTime
}
void Gyro::setGyroOffset() {
for(int i=0;i<100;i++){ // takes 100 samples of the gyro
gyro.read();
gerrx+=gyro.g.x;
delay(25);
}
gerrx = gerrx/100;
}
void Gyro::setGyroDrift() {
// Get drift rate
long stime = millis();
prevTime = millis();
for(int i =0;i<500;i++){
updateGyro();
delay(25);
}
long etime = millis();
gdriftx = gyro_xcumulative/(etime-stime)*1000;
Serial.println(gyro_xcumulative);
gyro_xcumulative = 0;
Serial.println(gdriftx);
}