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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* $Date: 2007/08/07 16:06:00 $
* $Revision: 1.1 $
*/
/*
* This software program is licensed subject to the GNU General Public License
* (GPL).Version 2,June 1991, available at http://www.fsf.org/copyleft/gpl.html
* (C) Copyright 2010 Bosch Sensortec GmbH
* All Rights Reserved
*/
/*! \file bma020calib.c
\brief This file contains all function implementatios for the BMA020/BMA150 calibration process
*/
#include <linux/fs.h>
#include "bma020.h"
#include "bma020calib.h"
/** calculates new offset in respect to acceleration data and old offset register values
\param orientation pass orientation one axis needs to be absolute 1 the others need to be 0
\param *offset_x takes the old offset value and modifies it to the new calculated one
\param *offset_y takes the old offset value and modifies it to the new calculated one
\param *offset_z takes the old offset value and modifies it to the new calculated one
*/
int bma020_calc_new_offset(bma020acc_t orientation, bma020acc_t accel,
unsigned short *offset_x, unsigned short *offset_y, unsigned short *offset_z)
{
short old_offset_x, old_offset_y, old_offset_z;
short new_offset_x, new_offset_y, new_offset_z;
unsigned char calibrated =0;
old_offset_x = *offset_x;
old_offset_y = *offset_y;
old_offset_z = *offset_z;
printk("[%s] before: x = %d, y = %d, z = %d\n", __func__, accel.x, accel.y, accel.z);
accel.x = accel.x - (orientation.x * 256);
accel.y = accel.y - (orientation.y * 256);
accel.z = accel.z - (orientation.z * 256);
printk("[%s] after: x = %d, y = %d, z = %d\n", __func__, accel.x, accel.y, accel.z);
if ((accel.x > 4) | (accel.x < -4)) { /* does x axis need calibration? */
if ((accel.x <8) && accel.x > 0) /* check for values less than quantisation of offset register */
new_offset_x= old_offset_x -1;
else if ((accel.x >-8) && (accel.x < 0))
new_offset_x= old_offset_x +1;
else
new_offset_x = old_offset_x - (accel.x/8); /* calculate new offset due to formula */
if (new_offset_x <0) /* check for register boundary */
new_offset_x =0; /* <0 ? */
else if (new_offset_x>1023)
new_offset_x=1023; /* >1023 ? */
*offset_x = new_offset_x;
calibrated = 1;
}
if ((accel.y > 4) | (accel.y<-4)) { /* does y axis need calibration? */
if ((accel.y <8) && accel.y > 0) /* check for values less than quantisation of offset register */
new_offset_y= old_offset_y -1;
else if ((accel.y >-8) && accel.y < 0)
new_offset_y= old_offset_y +1;
else
new_offset_y = old_offset_y - accel.y/8; /* calculate new offset due to formula */
if (new_offset_y <0) /* check for register boundary */
new_offset_y =0; /* <0 ? */
else if (new_offset_y>1023)
new_offset_y=1023; /* >1023 ? */
*offset_y = new_offset_y;
calibrated = 1;
}
if ((accel.z > 4) | (accel.z<-4)) { /* does z axis need calibration? */
if ((accel.z <8) && accel.z > 0) /* check for values less than quantisation of offset register */
new_offset_z= old_offset_z -1;
else if ((accel.z >-8) && accel.z < 0)
new_offset_z= old_offset_z +1;
else
new_offset_z = old_offset_z - (accel.z/8);/* calculate new offset due to formula */
if (new_offset_z <0) /* check for register boundary */
new_offset_z =0; /* <0 ? */
else if (new_offset_z>1023)
new_offset_z=1023;
*offset_z = new_offset_z;
calibrated = 1;
}
return calibrated;
}
/** reads out acceleration data and averages them, measures min and max
\param orientation pass orientation one axis needs to be absolute 1 the others need to be 0
\param num_avg numer of samples for averaging
\param *min returns the minimum measured value
\param *max returns the maximum measured value
\param *average returns the average value
*/
int bma020_read_accel_avg(int num_avg, bma020acc_t *min, bma020acc_t *max, bma020acc_t *avg )
{
long x_avg=0, y_avg=0, z_avg=0;
int comres=0;
int i;
bma020acc_t accel; /* read accel data */
x_avg = 0; y_avg=0; z_avg=0;
max->x = -512; max->y =-512; max->z = -512;
min->x = 512; min->y = 512; min->z = 512;
for (i=0; i<num_avg; i++) {
comres += bma020_read_accel_xyz(&accel); /* read 10 acceleration data triples */
accel.y = -accel.y;
accel.z = -accel.z;
if (accel.x>max->x)
max->x = accel.x;
if (accel.x<min->x)
min->x=accel.x;
if (accel.y>max->y)
max->y = accel.y;
if (accel.y<min->y)
min->y=accel.y;
if (accel.z>max->z)
max->z = accel.z;
if (accel.z<min->z)
min->z=accel.z;
x_avg+= accel.x;
y_avg+= accel.y;
z_avg+= accel.z;
bma020_pause(10);
}
avg->x = x_avg /= num_avg; /* calculate averages, min and max values */
avg->y = y_avg /= num_avg;
avg->z = z_avg /= num_avg;
return comres;
}
/** verifies the accerleration values to be good enough for calibration calculations
\param min takes the minimum measured value
\param max takes the maximum measured value
\param takes returns the average value
\return 1: min,max values are in range, 0: not in range
*/
int bma020_verify_min_max(bma020acc_t min, bma020acc_t max, bma020acc_t avg)
{
short dx, dy, dz;
int ver_ok=1;
dx = max.x - min.x; /* calc delta max-min */
dy = max.y - min.y;
dz = max.z - min.z;
if (dx> 10 || dx<-10)
ver_ok = 0;
if (dy> 10 || dy<-10)
ver_ok = 0;
if (dz> 10 || dz<-10)
ver_ok = 0;
return ver_ok;
}
/** overall calibration process. This function takes care about all other functions
\param orientation input for orientation [0;0;1] for measuring the device in horizontal surface up
\param *tries takes the number of wanted iteration steps, this pointer returns the number of calculated steps after this routine has finished
\return 1: calibration passed 2: did not pass within N steps
*/
int bma020_calibrate(bma020acc_t orientation, int *tries)
{
unsigned short offset_x, offset_y, offset_z;
unsigned short old_offset_x, old_offset_y, old_offset_z;
unsigned short changed_offset_x, changed_offset_y, changed_offset_z;
int need_calibration=0, min_max_ok=0;
int ltries;
int retry = 30;
bma020acc_t min,max,avg;
printk("[%s] +\n", __func__);
bma020_set_range(BMA020_RANGE_2G);
bma020_set_bandwidth(BMA020_BW_25HZ);
bma020_set_ee_w(1);
bma020_get_offset(0, &offset_x);
bma020_get_offset(1, &offset_y);
bma020_get_offset(2, &offset_z);
old_offset_x = offset_x;
old_offset_y = offset_y;
old_offset_z = offset_z;
ltries = *tries;
printk("[%s] old offset_x = %d, offset_y = %d, offset_z = %d\n", __func__, old_offset_x, offset_y, offset_z);
orientation.x = 0;
orientation.y = 0;
orientation.z = 1;
do {
bma020_read_accel_avg(10, &min, &max, &avg); /* read acceleration data min, max, avg */
min_max_ok = bma020_verify_min_max(min, max, avg);
if(!min_max_ok)
{
retry--;
if(retry <= 0)
return (-1);
}
/* check if calibration is needed */
if (min_max_ok)
need_calibration = bma020_calc_new_offset(orientation, avg, &offset_x, &offset_y, &offset_z);
if (*tries==0) /*number of maximum tries reached? */
break;
if (need_calibration) {
/* when needed calibration is updated in image */
printk("[%s] need calibration. tries = %d. changed offset x = %d, y = %d, z = %d\n", __func__, *tries, offset_x, offset_y, offset_z);
(*tries)--;
bma020_set_offset(0, offset_x);
bma020_set_offset(1, offset_y);
bma020_set_offset(2, offset_z);
bma020_pause(20);
}
printk("\n");
} while (need_calibration || !min_max_ok);
if (*tries>0 && *tries < ltries) {
printk("[%s] eeprom is updated. new offset x=%d, y=%d, z=%d\n",
__func__, offset_x, offset_y, offset_z);
if (old_offset_x!= offset_x)
bma020_set_offset_eeprom(0, offset_x);
if (old_offset_y!= offset_y)
bma020_set_offset_eeprom(1,offset_y);
if (old_offset_z!= offset_z)
bma020_set_offset_eeprom(2, offset_z);
}
printk("[%s] tries = %d\n", __func__, *tries);
bma020_set_ee_w(0);
bma020_pause(20);
*tries = ltries - *tries;
printk("[%s] -\n", __func__);
return !need_calibration;
}
|