-
Notifications
You must be signed in to change notification settings - Fork 3
/
ITG3200.c
203 lines (175 loc) · 5.53 KB
/
ITG3200.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "ITG3200.h"
#include "i2c2.h"
#include <stdio.h>
//#define ITG3200_PRINTF_ERROR 1
#define ITG3200_PRINTF_DEBUG 1
float gyroscope[3] = {0,0,0};
float gyroscope_offset[3] = {0, 0, 0};
float gyroscope_gain[3] = {1, 1, 1};
s8 volatile gyroscope_is_calibrated = 0;
s8 volatile gyroscope_is_calibration_checked = 0;
void ITG3200_setup() {
int return_value;
gyroscope_is_calibrated = 0;
gyroscope_is_calibration_checked = 0;
//Set internal clock to 1kHz with 188Hz LPF and Full Scale(+-2000) to 3 for proper operation
// return_value = i2c_write(ITG_ADDR, DLPF_FS, DLPF_FS_SEL_0|DLPF_FS_SEL_1);//|DLPF_CFG_0);
//return_value = i2c_write(ITG_ADDR, DLPF_FS, DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0|DLPF_CFG_1|DLPF_CFG_2);
return_value = i2c2_write(ITG_ADDR, DLPF_FS, DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0|DLPF_CFG_1);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_write(ITG_ADDR, DLPF_FS, DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_1): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
//Set sample rate divider for 333 Hz operation
return_value = i2c2_write(ITG_ADDR, SMPLRT_DIV, 3); //Fsample = Fint / (divider + 1) where Fint is 1kHz
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_write(ITG_ADDR, SMPLRT_DIV, 4): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
//Setup the interrupt to trigger when new data is ready.
return_value = i2c2_write(ITG_ADDR, INT_CFG, 0);// INT_CFG_RAW_RDY_EN | INT_CFG_ITG_RDY_EN);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_write(ITG_ADDR, INT_CFG, 0): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
//Select X gyro PLL for clock source
return_value = i2c2_write(ITG_ADDR, PWR_MGM, PWR_MGM_CLK_SEL_0);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_write(ITG_ADDR, PWR_MGM, PWR_MGM_CLK_SEL_0): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
}
void ITG3200_getValues() {
int return_value;
float gyroscope_tmp=0;
char value[2];
return_value = i2c2_read(ITG_ADDR, GYRO_XOUT_H, value, 2);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_read(ITG_ADDR, GYRO_XOUT_H, value, 2): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
if(return_value == 1) {
gyroscope_tmp = (float)(s16)(((s16)value[0]<<8) | value[1]);
if(gyroscope_is_calibrated) {
gyroscope_tmp -= gyroscope_offset[1];
gyroscope_tmp *= gyroscope_gain[1];
}
gyroscope[1] = gyroscope_tmp;
}
return_value = i2c2_read(ITG_ADDR, GYRO_YOUT_H, value, 2);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_read(ITG_ADDR, GYRO_YOUT_H, value, 2): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
if(return_value == 1) {
gyroscope_tmp = (float)(s16)(((s16)value[0]<<8) | value[1]);
if(gyroscope_is_calibrated) {
gyroscope_tmp -= gyroscope_offset[0];
gyroscope_tmp *= gyroscope_gain[0];
}
gyroscope[0] = gyroscope_tmp;
}
return_value = i2c2_read(ITG_ADDR, GYRO_ZOUT_H, value, 2);
#ifdef ITG3200_PRINTF_ERROR
if(return_value < 1) {
printf("%s (%d): i2c2_read(ITG_ADDR, GYRO_ZOUT_H, value, 2): return_value: %d\r\n", __FILE__, __LINE__, return_value);
}
#endif
if(return_value == 1) {
gyroscope_tmp = (float)(s16)(((s16)value[0]<<8) | value[1]);
if(gyroscope_is_calibrated) {
gyroscope_tmp -= gyroscope_offset[2];
gyroscope_tmp *= gyroscope_gain[2];
}
gyroscope[2] = gyroscope_tmp;
}
}
void ITG3200_calibrate() {
static s8 running = 0;
static int count=0;
s8 i;
// Start
if(!running) {
#ifdef ITG3200_PRINTF_DEBUG
printf("Starting calibration\r\n");
#endif
running = 1;
count=0;
gyroscope_is_calibration_checked = 0;
gyroscope_is_calibrated = 0;
for(i=0; i<3; i++) {
gyroscope_offset[i] = 0;
}
return;
}
// Calculating a simple average
for(i=0; i<3; i++) {
gyroscope_offset[i] += gyroscope[i];
}
count++;
// End
if(count >= 1000) {
for(i=0; i<3; i++) {
gyroscope_offset[i] /= 1000;
}
#ifdef ITG3200_PRINTF_DEBUG
printf("Stoping calibration: %d %d %d\r\n", (int)gyroscope_offset[0], (int)gyroscope_offset[1], (int)gyroscope_offset[2]);
#endif
gyroscope_is_calibrated = 1;
running = 0;
return;
}
}
void ITG3200_check_calibration() {
static s32 integral_tmp[3] = {0,0,0};
static s8 running = 0;
static int count=0;
s8 i;
// Start
if(!running) {
if(!gyroscope_is_calibrated) return;
#ifdef ITG3200_PRINTF_DEBUG
printf("Starting calibration check: %d %d %d\r\n", (int)gyroscope[0], (int)gyroscope[1], (int)gyroscope[2]);
#endif
running = 1;
count=0;
gyroscope_is_calibration_checked = 0;
for(i=0; i<3; i++) {
integral_tmp[i] = 0;
}
return;
}
// Calculating a simple average
for(i=0; i<3; i++) {
integral_tmp[i] += gyroscope[i];
}
count++;
// End
if(count >= CALIBRATION_CHECK_TIME/2) {
#ifdef ITG3200_PRINTF_DEBUG
printf("Stoping calibration check\r\n");
#endif
for(i=0; i<3; i++) {
if(!(-CALIBRATION_THRESHOLD < integral_tmp[i] && integral_tmp[i] < CALIBRATION_THRESHOLD)) {
gyroscope_is_calibrated = 0;
running = 0;
#ifdef ITG3200_PRINTF_DEBUG
printf("Bad calibration: %d %d %d\r\n", (int)integral_tmp[0], (int)integral_tmp[1], (int)integral_tmp[2]);
#endif
return;
}
}
#ifdef ITG3200_PRINTF_DEBUG
printf("Good calibration: %d %d %d\r\n", (int)integral_tmp[0], (int)integral_tmp[1], (int)integral_tmp[2]);
#endif
gyroscope_is_calibration_checked = 1;
running = 0;
return;
}
}