-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gyro.ino
151 lines (122 loc) · 3.88 KB
/
Gyro.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//Internal register of Gyro sensor L3G4200D
#define L3G4200D_Address (105) // I2C address of the L3G4200D
#define CTRL_REG1 (0x20)
#define CTRL_REG2 (0x21)
#define CTRL_REG3 (0x22)
#define CTRL_REG4 (0x23)
#define CTRL_REG5 (0x24)
class GyroL3G4200D
{
public:
GyroL3G4200D()
{
X=Y=Z=0.0f;
Raw_x=Raw_y=Raw_z=0.0f;
Gyr_x_Drift=0.0f;
Gyr_y_Drift=0.0f;
Gyr_z_Drift=0.0f;
};
void init(int scale)
{
// Enable x, y, z and turn off power down:
i2c_writeRegister(L3G4200D_Address, CTRL_REG1, 0b11111111); //for 800 Hz and 110 cut off
// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
i2c_writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
i2c_writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
// CTRL_REG4 controls the full-scale range, among other things:
if(scale == 250)
{
i2c_writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}
else if(scale == 500)
{
i2c_writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}
else //2000 dps
{
i2c_writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}
// CTRL_REG5 controls high-pass filtering of outputs, use it
i2c_writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
initDrift(1);
}
/*
* calculate gyro drifft
*/
void initDrift(int Filter_Sampling)
{
for(unsigned long i=0;i<=(Filter_Sampling*1000);i++)
{
Gyr_x_Drift += (float) ((1.0f/(Filter_Sampling*1000))*_getRawX());
Gyr_y_Drift += (float) ((1.0f/(Filter_Sampling*1000))*_getRawY());
Gyr_z_Drift += (float) ((1.0f/(Filter_Sampling*1000))*_getRawZ());
togglePin(BOARD_LED_PIN);
delay(1);
}
digitalWrite(BOARD_LED_PIN, HIGH);
}
/*
* The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
*/
float _getRawX()
{
return (float)((signed short)(((byte) i2c_readRegister(L3G4200D_Address, 0x29)) << 8) | ((byte) i2c_readRegister(L3G4200D_Address, 0x28)));
};
/*
* The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
*/
float _getRawY()
{
return (float)((signed short)(((byte) i2c_readRegister(L3G4200D_Address, 0x2B)) << 8) | ((byte) i2c_readRegister(L3G4200D_Address, 0x2A)));
};
/*
* The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
*/
float _getRawZ()
{
return (float)((signed short)(((byte) i2c_readRegister(L3G4200D_Address, 0x2D)) << 8) | ((byte) i2c_readRegister(L3G4200D_Address, 0x2C)));
};
void ReadXYZ()
{
Raw_x=_getRawX();
Raw_y=_getRawY();
Raw_z=_getRawZ();
X=(float)(((Raw_x-Gyr_x_Drift)*70.0f)/1000.0f);
Y=(float)(((Raw_y-Gyr_y_Drift)*70.0f)/1000.0f);
Z=(float)(((Raw_z-Gyr_z_Drift)*70.0f)/1000.0f);
}
//return dps (degree per second)
float getX()
{
return (float)X;
}
float getY()
{
return (float)Y;
}
float getZ()
{
return (float)Z;
}
float getRawX()
{
return (float)Raw_x;
}
float getRawY()
{
return (float)Raw_y;
}
float getRawZ()
{
return (float)Raw_z;
}
private:
float X,Y,Z;
float Raw_x,Raw_y,Raw_z;
float Gyr_x_Drift;
float Gyr_y_Drift;
float Gyr_z_Drift;
};